Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to add and use properties files in  maven and unit test

Posted on 2014-02-02
4
Medium Priority
?
225 Views
Last Modified: 2014-03-06
1. Following is my pom.xml;

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <packaging>jar</packaging>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
<build>
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </testResource>
  </testResources>
</build>
</project>

2. Following is location of files:

[root@vp2-dsk my-app]# ls -al src/test/java/com/mycompany/app/AppTest.java
-rwxrwxrwx 1 root root 1883 Feb  2 13:49 src/test/java/com/mycompany/app/AppTest.java
[root@vp2-dsk my-app]#
[root@vp2-dsk my-app]# ls -al src/test/resources/test.properties
-rwxrwxrwx 1 root root 19 Feb  2 12:23 src/test/resources/test.properties
[root@vp2-dsk my-app]#


3. In my junit class:
I am referring to my properties files as =>
                InputStream is = AppTest.class.getResourceAsStream("test.properties");
And it fails upon running it as  a junit test case.

4. Also when i try maven test it fails.


Can some one help me how to make it work both from maven  and junit test case
0
Comment
Question by:pvinodp
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 750 total points
ID: 39828633
Does the following work for you?

InputStream is = AppTest.class.getResourceAsStream("/test.properties");

Ref: http://devblog.virtage.com/2013/07/how-to-get-file-resource-from-maven-srctestresources-folder-in-junit-test/
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 750 total points
ID: 39829262
Or

InputStream is = AppTest.class.getResourceAsStream("/resources/test.properties");

Open in new window


(difficult to know without knowing your package structure). If neither of those work, please start by posting the result of

[root@vp2-dsk my-app]# find .

Open in new window

0
 

Author Closing Comment

by:pvinodp
ID: 39908447
thanks all for your solutions
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39908610
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question