我刚刚开始使用maven,我无法弄清楚如何解决我遇到的问题。似乎我的java文件已经编译并且测试开始但没有显示结果。
Demo.java:
package tutorial2;
public class Demo {
public boolean getBool(){
return false;
}
}
TestDemo.java:
package tutorial2;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestDemo {
@Test
public void shouldBe(){
Demo demo = new Demo();
assertTrue(demo.getBool());
}
}
的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>lets.develop.withme</groupId>
<artifactId>tutorial2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Tutorial 02</name>
<description>My second tutorial</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project
我在控制台上得到这个:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Tutorial 02 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ tutorial2 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ tutorial2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ tutorial2 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ tutorial2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ tutorial2 ---
[INFO] Surefire report directory: /home/adam/workspaceJavaEE/tutorial2/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running tutorial2.TestDemo
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.943 s
[INFO] Finished at: 2016-10-14T00:34:25+02:00
[INFO] Final Memory: 10M/212M
[INFO] ------------------------------------------------------------------------
我自己解决了问题。未正确配置xml文件中的依赖项。 junit依赖的版本是错误的,所以它不能由maven运行。
您应该始终通过“插入依赖项”菜单添加依赖项。当光标位于xml文件中的<dependency>
和</dependency>
之间时,可以使用快捷键“crtl”+“space”显示它。当您在“选择依赖关系”菜单中,在“输入...”文本框中输入“junit”并选择您的junit版本。单击“确定”后,将自动添加依赖项。这有助于避免手动输入错误版本的问题。
答案 0 :(得分:0)
默认情况下,Surefire(在mvn test
期间运行的插件)会查找以target/test-classes
(从src/test/java
编译,可能是其他地方编译)的类,这些类以Test
结尾}。您的班级名称为TestDemo
,不会以Test
结尾,也不会被投放。可以将其重命名为DemoTest
(最好),或者在<plugins>
中手动配置Surefire(当您使用具有备用命名约定的框架时,例如Spock&#39; {{1 }})。
答案 1 :(得分:0)
根据maven文档avalible on:
默认情况下,Surefire插件会自动包含所有测试 具有以下通配符模式的类:
“/ Test * .java” - 包括其所有子目录和所有Java 以“Test”开头的文件名。 “/* Test.java” - 包括所有 它的子目录和所有以“Test”结尾的Java文件名。 “/* TestCase.java” - 包括其所有子目录和所有Java 以“TestCase”结尾的文件名。