Solved

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

Posted on 2014-02-02
4
194 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 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now