JUnit 的不同风格?
我正在尝试更新我的 Java 并学习如何使用 Maven 和 JUnit。遵循 Maven 快速入门 我运行了以下命令在控制台中:
mvn archetype:generate \
-DgroupId=com.mycompany.app \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
然后我在适当的文件夹中得到了一个简单的 App.java 和一个 AppTest.java 。我现在正在查看 AppTest.java 并试图弄清楚如何使用这个 JUnit 东西。问题是我不明白它,而且我看起来与我在 JUnit 食谱。例如,我从Maven获得的版本有不同的包名称,并且没有测试方法的注释。
这是怎么回事? Maven 是否使用了常规 JUnit 之外的其他东西?或者它只是在做一些奇特的事情?
更多信息
Apache Maven 3.0.2(r1056850;2011-01-09 01:58:10+0100)
Java 版本:1.6.0_23,供应商:Sun Microsystems Inc.
AppTest.java
package com.mycompany.app;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest extends TestCase {
/**
* Create the test case
*
* @param testName
* name of the test case
*/
public AppTest(String testName) {
super(testName);
}
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite(AppTest.class);
}
/**
* Rigorous Test :-)
*/
public void testApp() {
assertTrue(true);
}
}
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I'm trying to fresh up my Java and also to learn how to use Maven and JUnit. Following the Maven quick start I ran the following in the console:
mvn archetype:generate \
-DgroupId=com.mycompany.app \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
I then got a simple App.java and an AppTest.java in their proper folders. I'm now looking at the AppTest.java and trying to figure out how to use this JUnit stuff. The problem is that I don't understand it, and I looks quite different from what I see for example in the JUnit Cookbook. For example the version I got from Maven has different package names and there is no annotation of the test method.
What's going on here? Is Maven using something else than regular JUnit? Or is it just doing something fancy?
More info
Apache Maven 3.0.2 (r1056850; 2011-01-09 01:58:10+0100)
Java version: 1.6.0_23, vendor: Sun Microsystems Inc.
AppTest.java
package com.mycompany.app;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest extends TestCase {
/**
* Create the test case
*
* @param testName
* name of the test case
*/
public AppTest(String testName) {
super(testName);
}
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite(AppTest.class);
}
/**
* Rigorous Test :-)
*/
public void testApp() {
assertTrue(true);
}
}
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有 Maven 使用常规的 Junit。
但这里使用的是旧版本 3.8.1(无需注释即可工作,因此不需要 java 5 或更高版本)。
只需将 junit 依赖项更新到最新的 junit 版本 (4.8.2),然后您就可以像习惯使用注释一样编写测试。
no maven uses regular Junit.
But here the old version 3.8.1 is used (that works without annotations, in so does not require java 5 or higher).
Simply update you junit dependency to newest junit version (4.8.2) and then you can write tests as you are used to wirh annotations.