gpt4 book ai didi

java - 需要帮助来理解 JUnit RunListener

转载 作者:行者123 更新时间:2023-11-29 07:39:46 25 4
gpt4 key购买 nike

请耐心等待,这会很长。我写了下面的TestRunner.java

import java.util.ArrayList;
import java.util.List;
import org.junit.runner.JUnitCore;
import org.junit.runner.notification.RunListener;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

class TestRunner {
public static void main (String args[]) {
List<Class<?>> classes = new ArrayList<Class<?>>();
for (String each : args) {
try {
classes.add(Class.forName(each));
} catch (ClassNotFoundException e) {
System.out.println("Class Not found");
}
}

JUnitCore core = new JUnitCore();
RunListener listener = new MyListener();
core.addListener(listener);
core.run(classes.toArray(new Class[0]));
}
}

class MyListener extends RunListener {
public void testRunStarted(Description description) throws Exception {
System.out.println("Number of tests to execute: " + description.testCount());
}

public void testRunFinished(Result result) throws Exception {
System.out.println("Number of tests executed: " + result.getRunCount());
}

public void testStarted(Description description) throws Exception {
System.out.println("Starting: " + description.getMethodName());
}

public void testFinished(Description description) throws Exception {
System.out.println("Finished: " + description.getMethodName());
}

public void testFailure(Failure failure) throws Exception {
System.out.println("Failed: " + failure.getDescription().getMethodName());
}

public void testAssumptionFailure(Failure failure) {
System.out.println("Failed: " + failure.getDescription().getMethodName());
}

public void testIgnored(Description description) throws Exception {
System.out.println("Ignored: " + description.getMethodName());
}
}

我有 Abc.java

class Abc {
public int add(int a, int b) {
return a + b;
}
}

我有 AbcTest.java

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class AbcTest {
@Test
public void testAdd() {
System.out.println("add");
int a = 0;
int b = 0;
Abc instance = new Abc();
int expResult = 0;
int result = instance.add(a, b);
assertEquals(expResult, result);
}
}

我正在用命令运行测试

java -cp .:/path/to/junit4.jar TestRunner AbcTest

我看到的输出是

Number of tests to execute: 1
Starting: testAdd
add **<- This is my problem**
Finished: testAdd
Number of tests executed: 1

我想知道为什么在我完全构建了自己的 RunListner 类后 JUnit 仍然打印第 3 行?以及如何绕过 JUnit 的默认打印语句?

最佳答案

JUnit 没有打印第 3 行,你是。它在您的测试代码中:

 @Test 
public void testAdd() {
System.out.println("add");

关于java - 需要帮助来理解 JUnit RunListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31572171/

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