Just write a small introduction and simply output text. First, create a MessageUtil class for testing (recommended learning: java course)
package com.test.jiao; public class MessageUtil { private String message; public MessageUtil(String message){ this.message = message; } public String printMessage(){ System.out.println(message); return message; } }
Then, create a TestCase class, named TestJunit, and use assertEquals () to execute during testing
package com.test.jiao; import org.junit.Test; import static org.junit.Assert.assertEquals; public class TestJunit { String message = "Hello Jiao, Junit is working!"; MessageUtil messageUtil = new MessageUtil(message); @Test public void testPrintMessage(){ //Junit 的 assertEquals API 执行测试 assertEquals(message,messageUtil.printMessage()); } }
Finally, create a startup class TestRunner, which uses the runClasses method of JUnit's JUnitCore class to test
package com.test.jiao; 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){ //运用JUnit的JUnitCore类的runClasses方法来测试 Result result = JUnitCore.runClasses(TestJunit.class); //获取测试结果 for(Failure failure : result.getFailures()){ System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
OK, now run TestRunner to see the results (●'◡'●)
Success!
The above is the detailed content of The simplest use of java test unit JUnit. For more information, please follow other related articles on the PHP Chinese website!