我有一个简单的项目: 这是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";
}
}
我的测试课程是:
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));
}
}
我的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>`
当我按mvn test时,结果为:
-------------------------------------------------------
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]
你有什么想法吗?起初,我改变了我的测试方法名称,因为我知道maven正在寻找“测试”,但知道我没有任何其他想法..
答案 0 :(得分:1)
假设您的测试类位于src/test
,您应该添加此
<testSourceDirectory>src</testSourceDirectory>
但我强烈建议您遵循默认的maven项目布局:
sources: src/main/java
test sources: src/test/java
在这种情况下,您不需要定义<sourceDirectory>
或<testSourceDirectory>
,因为您遵循约定优于配置原则。
答案 1 :(得分:0)
测试类应始终命名为* Test.java!将TestCompareNumbers.java
重命名为CompareNumbersTest.java
,然后执行测试。
当然要确保测试类在src/test/java
之下,就像其他海报所说的那样。