Solved

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

Posted on 2014-02-02
4
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

627 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