JUnit类应该只有一个公共构造函数。

huangapple go评论63阅读模式
英文:

JUnit class should have exactly one public constructor

问题

在VS Code中,有一个具体的Java项目,其中包含以下JUnit测试:

import static org.junit.Assert.fail;

import org.junit.Test;

class NgramTest {
    
    @Test 
    public void test() {
        fail("Not yet implemented");
    }

}

如果运行它,会出现一个初始化错误,提示:

java.lang.Exception: 测试类应该有且仅有一个公共构造函数

如何解决这个问题?

英文:

In VS Code got a specific Java project with the following JUnit test

import static org.junit.Assert.fail;

import org.junit.Test;

class NgramTest {
    
    @Test 
    public void test() {
        fail("Not yet implemented");
    }

}

If I run it, I get an initializationError saying

> java.lang.Exception: Test class should have exactly one public
> constructor

JUnit类应该只有一个公共构造函数。

What can be done to solve it?

答案1

得分: 1

需要在类NgramTest之前写上public关键字修改代码如下

import static org.junit.Assert.fail;
import org.junit.Test;

public class NgramTest {

    @Test 
    public void test() {
        fail("Not yet implemented");
    }

}
这样做可以使其正常工作并且您将会得到预期的结果

> java.lang.AssertionError: Not yet implemented

注意:由于您要求只返回翻译好的部分,因此我只提供了代码的翻译部分。如果您有任何其他问题或需要进一步的帮助,请随时问我。

英文:

You need to write public before class NgramTest. Change the code to

import static org.junit.Assert.fail;

import org.junit.Test;

public class NgramTest {
    
    @Test 
    public void test() {
        fail("Not yet implemented");
    }

}

That way it'll work fine and you'll get, as expected, the result

> java.lang.AssertionError: Not yet implemented

JUnit类应该只有一个公共构造函数。

huangapple
  • 本文由 发表于 2020年9月14日 21:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63885400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定