4

我在 netbeans 6.9.1 中有一个 maven 项目,里面有一个 junit 4.4 测试类。

在 netbeans 上下文菜单中,我可以“清理和构建”我的项目,并且在输出中我可以看到,我的测试类是由 surefire 找到并运行的。

现在我从上下文菜单“调试测试文件”中选择,在输出中它就像

--- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ <project-name> --- 编译2个源文件到<project-path>\target\test-classes

--- maven-surefire-plugin:2.7.2:test (default-cli) @ <project-name> --- Surefire 报告目录:<project-path>\target\surefire-reports


测试


没有要运行的测试。

到目前为止我检查过的内容:

  • 构建项目找到测试文件,但仍然存在 <testSourceDirectory> 并且正确

  • *.pom 依赖项中只有一个 junit - 4.4

  • 类文件本身看起来像

    导入 junit.framework.TestCase;

    导入 org.junit.Test;

    公共类 SomeTest 扩展 TestCase { @Test public void testFoo() throws Exception { / --- /} }

  • 调试的 netbeans 动作描述看起来像

    执行目标:test-compile surefire:test

    设置属性: jpda.listen=true maven.surefire.debug=-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} jpda.stopclass=${packageClassName} failIfNoTests=false // 这是我的补充,没有它仍然失败 forkMode=once test=${className}

  • 项目 *.pom 文件中的任何地方都没有安全插件部分

4

1 回答 1

3

这不是一个真正的netbeans问题。

您已经定义了 JUnit 3 测试,但您正在尝试使用 JUnit 4 运行程序运行它们。Surefire 使用以下规则来确定使用哪个运行器(来自maven-surefire-plugin Junit

if the JUnit version in the project >= 4.7 and the parallel attribute has ANY value
    use junit47 provider
if JUnit >= 4.0 is present
    use junit4 provider
else
    use junit3.8.1

你的项目中有junit 4.4,所以它使用junit4。由于您只有 2 个测试类,因此最好的选择是将您的测试重新定义为 JUnit 4 测试。删除扩展的 TestCase,将 @Test/@Before/@After 注释添加到您的测试中,并完全删除 JUnit 3 的东西。

有关进行迁移的更多信息,请参阅将测试从 JUnit 3 自动迁移到 JUnit 4 的最佳方式?

于 2011-11-30T12:21:19.390 回答