Solved

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

Posted on 2014-02-02
4
191 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks all for your solutions
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

763 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

10 Experts available now in Live!

Get 1:1 Help Now