gpt4 book ai didi

java - Junit - Java 如何处理具有断言的类方法

转载 作者:行者123 更新时间:2023-12-01 11:12:11 24 4
gpt4 key购买 nike

我对 Junit 还很陌生。我有一个关于 Java 如何运行 Junit 类的问题。我有这样的代码

public class TestJunit1 {

String message = "Srinivas";
MessageUtil messageutil = new MessageUtil(message);

@Test
public void testPrintMessage() {
System.out.println("Inside testPrintMessage");
assertEquals(message, messageutil.printMessage());
}

}

public class TestJUnit5 extends TestCase {
protected double fValue1;
protected double fValue2;

@Before
public void setUp() throws Exception {
fValue1 = 2.0;
fValue2 = 3.0;
}

@Test
public void testAdding() {
System.out.println("No of test cases =" + this.countTestCases());

String name = this.getName();
System.out.println("Test Case name is: "+name);

this.setName("methodNewAdd");
String newName = this.getName();
System.out.println("New name of the test case is:"+newName);
System.out.println(this.getClass());
}

@After
public void tearDown() throws Exception {
}

public class TestSuiteDemo {


public static void main(String[] args) {

TestSuite ts = new TestSuite(TestJunit1.class, TestJunit2.class, TestJUnit5.class);
TestResult tr = new TestResult();

ts.run(tr);
System.out.println("Number of test cases is:"+tr.runCount());


}

}

当我在 eclipse 中将 TestSuiteDemo 作为 Java 应用程序运行时,它会从 TestJUnit5 的 println 语句生成输出,而不是从 TestJunit1 生成输出。有人可以解释一下为什么会发生这种情况吗?

问候斯里尼瓦斯

最佳答案

因为 TestJUnit5 扩展了 TestCase(而不是 TestJunit1),因此 JUnit 可以看到测试用例并运行它。

来自 junit.framework.TestSuite 代码,任何继承 Test 类或扩展 TestCase 可以通过 TestSuite 构造函数注册。

private void addTestsFromTestCase(final Class<?> theClass) {
fName = theClass.getName();
try {
getTestConstructor(theClass); // Avoid generating multiple error messages
} catch (NoSuchMethodException e) {
addTest(warning("Class " + theClass.getName() + " has no public constructor TestCase(String name) or TestCase()"));
return;
}

if (!Modifier.isPublic(theClass.getModifiers())) {
addTest(warning("Class " + theClass.getName() + " is not public"));
return;
}

Class<?> superClass = theClass;
List<String> names = new ArrayList<String>();
while (Test.class.isAssignableFrom(superClass)) {
for (Method each : MethodSorter.getDeclaredMethods(superClass)) {
addTestMethod(each, names, theClass);
}
superClass = superClass.getSuperclass();
}
if (fTests.size() == 0) {
addTest(warning("No tests found in " + theClass.getName()));
}
}

要让 TestJunit1 工作,您必须创建一个注释性 Suite

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
TestJunit1.class,
TestJunit2.class,
TestJunit5.class
})

public class JunitTestSuite {
}

并在本地运行它:

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class JunitTestSuiteRunner {

public static void main(String[] args) {

Result result = JUnitCore.runClasses(JunitTestSuite.class);
for (Failure fail : result.getFailures()) {
System.out.println(fail.toString());
}
if (result.wasSuccessful()) {
System.out.println("All tests finished successfully...");
}
}
}

我希望这会有所帮助。

来源 reference

关于java - Junit - Java 如何处理具有断言的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32219749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com