Maven doen`t find test methods

advertisements

I have a simple project : This is Main.java `

package main;

public class Main {
    public static void main(String[] args){
        System.out.println(new CompareNumbers(7, 6).compara());
    }
}

` CompareNumbers.java

    package main;

public class CompareNumbers {
    private int x,y;

    public CompareNumbers(int x, int y){
        this.x=x;
        this.y=y;
    }

    public String compara(){
        if(x==y){
            return "x egal y";
        }
        if(x<y){
            if(x<y-5){
                return "x mai mic cel putin 5";
            }
            if(x>y-2){
                return "x mai mic cel mult 2";
            }
        }

        if(y<x){
            if(y<x-5){
                return "y mai mic cel putin 5";
            }
            if(y>x-2){
                return "y mai mic cel mult 2";
            }
        }
        return "indecizie";
    }

}

And my test class is:

   package test;

import main.CompareNumbers;

import org.junit.*;

public class TestCompareNumbers {
    @Test
    public void testScenario_1_Test(){
        CompareNumbers c = new CompareNumbers(1, 7);
        String s = c.compara();
        Assert.assertTrue("x mai mic cel putin 5".equals(s));

    }

    @Test
    public void testScenario_2_Test(){
        CompareNumbers c = new CompareNumbers(6, 7);
        String s = c.compara();
        Assert.assertTrue("x mai mic cel mult 2".equals(s));

    }

    @Test
    public void testScenario_3_Test(){
        CompareNumbers c = new CompareNumbers(4, 7);
        String s = c.compara();
        Assert.assertTrue("indecizie".equals(s));

    }

    @Test
    public void testScenario_4_Test(){
        CompareNumbers c = new CompareNumbers(7,4);
        String s = c.compara();
        Assert.assertTrue("indecizie".equals(s));

    }

}

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>coverage</groupId>
  <artifactId>coverage</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>`

When i press mvn test the result is :

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] BUILD SUCCESS

[INFO] Total time: 1.034s
[INFO] Finished at: Thu Mar 19 12:33:23 EET 2015
[INFO] Final Memory: 8M/149M
[INFO]

Do you have any ideas? At first,I changed my test methods names because i know that maven is looking for "Test", but know i dont have any other idea..


Assuming your test class is in src/test you should add this

<testSourceDirectory>src</testSourceDirectory>

But I would highly recommend to follow the default maven project layout:

sources: src/main/java

test sources: src/test/java

In this case you don't need to define either <sourceDirectory> or <testSourceDirectory> since you follow convention over configuration principle.