gpt4 book ai didi

junit4 - Junit - java.lang.Exception : No runnable methods

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

我正在做一个关于使用驱动程序类执行服务器测试用例的教程。 Eclipse 错误 Pane 显示错误消息:java.lang.Exception: No runnable methods。错误消息发生在
驱动程序类尝试运行异常测试类。

基于 pm 这个帖子:java.lang.exception no runnable methods junit .似乎应该在测试类中使用 @Test 注释或将 Junit Jar 文件更新到最新版本将解决该问题。

Junit Jar 文件版本:4.11

请检查我的代码并告诉我我应该做哪些修改以避免错误消息。谢谢!

驱动类:

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

@RunWith(Suite.class)
@Suite.SuiteClasses({
basicAnnotation.class,
personTest.class,
exceptionTesting.class
})
public class UnitTestDriver {

}

异常测试类
import org.junit.*;
import org.junit.Test;


public class exceptionTesting
{
public class junitTest2{
@Test (expected = ArithmeticException.class)
public void divisionWithException(){
int i = 1/0;
}

}
}

最佳答案

问题是junitTest2 嵌套在exceptionTesting 中。你有两个选择来解决它:

1)摆脱在顶级类中进行测试的内部类:

import org.junit.*;
import org.junit.Test;


public class exceptionTesting
{
@Test (expected = ArithmeticException.class)
public void divisionWithException(){
int i = 1/0;
}
}

2)让您的顶级类指定封闭的测试并使您的内部类静态:
import org.junit.*;
import org.junit.Test;

@RunWith(Enclosed.class)
public class exceptionTesting
{
public static class junitTest2{
@Test (expected = ArithmeticException.class)
public void divisionWithException(){
int i = 1/0;
}

}
}

除非您有某种理由需要内部类,否则 #1 是更好的选择。另请注意,在 Java 中,约定是以大写名称开头的类。因此,exceptionTesting 将成为 ExceptionTesting。

关于junit4 - Junit - java.lang.Exception : No runnable methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16814365/

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