gpt4 book ai didi

java - JUnit 测试类在没有 main 方法的情况下运行。这是如何完成/可能的?

转载 作者:行者123 更新时间:2023-11-30 07:13:38 25 4
gpt4 key购买 nike

比如这里...

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

public class StaticCalculationTest {

@Test
public void addSubTest() {
// assertEquals(String message, long expected, long actual)
assertEquals("Error in add()!", 3, StaticCalculation.add(1, 2));
assertEquals("Error in sub()!", 1, StaticCalculation.sub(2, 1));
}

@Test
public void mulDivTest() {
assertEquals("Error in mul()!", 6, StaticCalculation.mul(2, 3));
// assertEquals(String message, double expected, double actual, double delta)
assertEquals("Error in div()!", 0.5, StaticCalculation.div(1, 2), 1e-8);
}
}

以上示例取自:http://www3.ntu.edu.sg/home/ehchua/programming/java/JUnit_Ant.html

最佳答案

没有 main(String[]) 方法,任何 Java 应用程序都无法运行。它是单一入口点。对于 JUnit 测试,此方法通常隐藏在插件后面。对于 Eclipse,这似乎是 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner

在您发布的链接中,他们建议使用 java org.junit.runner.JUnitCore 设置您的 @Test 注释方法和 Suites 并运行它们。以类似的方式,Ant 具有 JUnitTask,它在某处(如果您查看其源代码)创建一个运行器来运行您的测试。那好像是JUnitTestRunner ,由于 Ant 任务,它实际上在自己的 JVM 中运行。

关于java - JUnit 测试类在没有 main 方法的情况下运行。这是如何完成/可能的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19259730/

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