Sidharth Rath
Sidharth Rath

Reputation: 189

How to run Junit Test in Maven when a test file is not in mavenise structure

I have so many java files and those java files test.java is not in src/test/java structure. for ex: i have java file named abc.java in src/java/abc but the the test file of this java file named as abcTest.java is in src/java/junit/abc. so how i will do the junit testing of this java file through maven pom.xml as maven wants normal java file in src/main/java and test files in src/test/java so how i will do the test through maven?

I have added the junit dependency and surefire plugin in my pom.xml .

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

and the surefire plugin is

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
               <configuration> 
               <testSourceDirectory>${basedir}/../src/java/junit/*.java</testSourceDirectory> 
               <testClassesDirectory>${project.build.directory}/classe‌​s/</testClassesDirectory>
<includes>
            <include>abcTest.java</include>
          </includes>
                </configuration> 
            </plugin>

i have given mvn test command but still it is giving zero tests in log. Can anyone help me on this how to run junit test.java files through maven?

Upvotes: 0

Views: 1351

Answers (2)

Mark Bramnik
Mark Bramnik

Reputation: 42431

Assuming you can't really the structure of the project, you'll have to customize the Surefire plugin.

Adding a Junit dependency won't be sufficient - its only allow to compile your test classes.

So, you're in the right direction - you'll have to customize a Surefire plugin of Maven responsible for running the tests.

Now, in order to make Surefire plugin running the tests that don't end with *Test.java you'll have to change inclusions. Namely, you'll have to specify regular expressions that will reflect the names of tests you wish to run.

This page describes just this.

Upvotes: 0

Sidharth Rath
Sidharth Rath

Reputation: 189

i have added these two below lines in my pom.xml after basedirectory tag and junit test cases ran successfully . and now i am running mvn install and all my testcases in test.java are running although i don't have maven like structure.

<testSourceDirectory>${basedir}/../src/java/junit</testSourceDirectory> 
        <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>

and after that i have added surefire plugin and thats all my junit testcases are running.

Upvotes: 1

Related Questions