简体   繁体   English

Maven找不到测试方法

[英]Maven doen`t find test methods

I have a simple project : This is Main.java ` 我有一个简单的项目:这是Main.java

package main;

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

` CompareNumbers.java `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: 我的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 : 当我按MVN测试的结果是:

-------------------------------------------------------
 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.. 起初,我更改了测试方法的名称,因为我知道Maven正在寻找“ Test”,但是我没有其他想法。

Assuming your test class is in src/test you should add this 假设您的测试类在src/test ,则应添加此内容

<testSourceDirectory>src</testSourceDirectory>

But I would highly recommend to follow the default maven project layout: 但我强烈建议您遵循默认的Maven项目布局:

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. 在这种情况下,您无需定义<sourceDirectory><testSourceDirectory>因为您遵循约定优于配置原则。

Test classes should always be named *Test.java! 测试类应始终命名为* Test.java! Rename TestCompareNumbers.java to CompareNumbersTest.java and the tests will be executed. TestCompareNumbers.java重命名为CompareNumbersTest.java ,然后将执行测试。

And of course make sure that the test classes are under src/test/java like the other posters said. 当然确保测试类下src/test/java像其他海报说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM