标签:
@Ignore 用法很简单, 如果你的测试用例还没有准备好而不想被执行, 又不想删掉或注释掉, 可以使用 @Ignore 标注来忽略测试。
方法一旦用 @Ignore 注解了将不会被执行.
如果一个类用 @Ignore 注解了 他下面的所有测试方法将不会被执行.
看个应用
Create a java class to be tested say MessageUtil.java in C:\ > JUNIT_WORKSPACE
/*
* This class prints the given message on console.
*/
public class MessageUtil {
private String message;
//Constructor
//@param message to be printed
public MessageUtil(String message){
this.message = message;
}
// prints the message
public String printMessage(){
System.out.println(message);
return message;
}
// add "Hi!" to the message
public String salutationMessage(){
message = "Hi!" + message;
System.out.println(message);
return message;
}
}
Create a java test class say TestJunit.java.
Add a test methods testPrintMessage(),testSalutationMessage() to your test class.
Add an Annotaion @Ignore to method testPrintMessage().
Create a java class file name TestJunit.java in C:\ > JUNIT_WORKSPACE
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
public class TestJunit {
String message = "Robert";
MessageUtil messageUtil = new MessageUtil(message);
@Ignore
@Test
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
message = "Robert";
assertEquals(message,messageUtil.printMessage());
}
@Test
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Robert";
assertEquals(message,messageUtil.salutationMessage());
}
}
Create a java class file name TestRunner.java in C:\ > JUNIT_WORKSPACE to execute Test case(s)
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Compile the MessageUtil, Test case and Test Runner classes using javac
C:\JUNIT_WORKSPACE>javac MessageUtil.java TestJunit.java TestRunner.java
Now run the Test Runner which will not run testPrintMessage() test case defined in provided Test Case class.
C:\JUNIT_WORKSPACE>java TestRunner
Verify the output. testPrintMessage() test case is not tested.
Inside testSalutationMessage()
Hi!Robert
true
Now update TestJunit in C:\ > JUNIT_WORKSPACE to ignore all test cases. Add @Ignore at class level
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
@Ignore
public class TestJunit {
String message = "Robert";
MessageUtil messageUtil = new MessageUtil(message);
@Test
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
message = "Robert";
assertEquals(message,messageUtil.printMessage());
}
@Test
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Robert";
assertEquals(message,messageUtil.salutationMessage());
}
}
Compile the Test case using javac
C:\JUNIT_WORKSPACE>javac TestJunit.java
Keep your Test Runner unchnaged as follows
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Now run the Test Runner which will not run any test case defined in provided Test Case class.
C:\JUNIT_WORKSPACE>java TestRunner
Verify the output. No test case is tested.
true
标签:
原文地址:http://www.cnblogs.com/fayzter/p/4562839.html