【问题标题】:Different flavors of JUnit?不同风格的 JUnit?
【发布时间】:2011-01-18 13:27:55
【问题描述】:

我正在尝试更新我的 Java 并学习如何使用 Maven 和 JUnit。在 Maven quick start 之后,我在控制台中运行了以下命令:

mvn archetype:generate \
 -DgroupId=com.mycompany.app \
 -DartifactId=my-app \
 -DarchetypeArtifactId=maven-archetype-quickstart \
 -DinteractiveMode=false

然后,我在其适当的文件夹中获得了一个简单的 App.java 和一个 AppTest.java。我现在正在查看 AppTest.java 并试图弄清楚如何使用这个 JUnit 东西。问题是我不明白,而且我看起来与我在JUnit Cookbook 中看到的完全不同。比如我从 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>

【问题讨论】:

    标签: java junit maven


    【解决方案1】:

    没有 maven 使用常规的 Junit。

    但这里使用的是旧版本 3.8.1(无需注释即可工作,因此不需要 java 5 或更高版本)。

    只需将您的 junit 依赖更新到最新的 junit 版本 (4.8.2),然后您就可以编写测试,因为您习惯于使用注释。

    【讨论】:

    • 啊哈。现在试图只更改我的 pom 文件中的版本号,并且似乎确实做到了。谢谢!
    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2013-03-17
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    相关资源
    最近更新 更多