Rito
Rito

Reputation: 3290

Identifying the Test Directories in a Java Maven Project

I have not followed the general Maven project structure in my Project. This is how my project structure looks like -

ProjectName
 |- src
    |- app
       |- models
       |- services
    |- test
       |- unit
          |- services
       |- integration
          |- services

For the test I am using Junit and Mockito. My pom.xml file looks like this -

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ProjectName</groupId>
  <artifactId>ProjectName</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.10.19</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

The problem that I am facing now is, when I am running my tests it is not able to find the dependency packages junit and mockito. I know it is because I have declared the scope of these dependecies as test.

What I want to know is, what change should I make in my pom.xml file so that Maven can indentify my test directory?

Upvotes: 2

Views: 6765

Answers (2)

Rishikesh Darandale
Rishikesh Darandale

Reputation: 3310

I would also suggest to follow the conventions for maven projects as others did.

Still, things should work by overriding following maven properties as below:

<properties>
  <!-- Considering src/app will have all your sourcr java code -->
  <project.build.sourceDirectory>src/app</project.build.sourceDirectory>
  <!-- Considering all your junit tests are in src/test/unit -->
  <project.build.testSourceDirectory>src/test/unit</project.build.testSourceDirectory>
  ...
</properties>

If you want to add integration test with maven, then I would suggest to go over this article.

You can remove <sourceDirectory> and <resources> from <build> section.

Upvotes: 3

SilverNak
SilverNak

Reputation: 3381

The Maven surefire plugin will look for tests in the directory the property project.build.testSourceDirectory is pointing to.

So you can add this to your pom.xml to change the value of this property:

<build>
    <testSourceDirectory>${project.basedir}/src/test</testSourceDirectory>
</build>

This will execute all tests (unit and integration) in the test phase. If you dont want to execute the integration tests, you can set the property to ${project.basedir}/src/test/unit.

Upvotes: 4

Related Questions