Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

713 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