今天第一次使用Junit来测试代码,在调试过程中出现了以下错误:
No tests found with test runner JUnit4
错误原因:测试类中没有继承TestCase类,又没有@Test注解。
在代码中,有@Before和@After注解,而在测试方法中没有@Test注解。
在测试方法中,加上@Test即可。。
如果继承了TestCase,则注解可以不要。
测试代码如下:
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StuTest {
Stu stu = null;
@Before
public void setUp() throws Exception {
System.out.println("测试开始");
stu = new Stu();
}
@After
public void tearDown() throws Exception {
System.out.println("测试结束...");
}
@Test
public void testSetMethod() {
stu.setName("wsh");
assertEquals("wsh", stu.getName());
System.out.println("测试ok...");
}
@Test
public void testSetMethod1()
{
stu.setName("xxx");
assertEquals("xxxx",stu.getName());
System.out.println("测试error");
}
}