Create a Java project and a directory structure
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/75/wKioL1Q902qj5A9lAABreFWsIIw032.jpg "Title =" j.png "alt =" wkiol1q902qj5a9laabrefwsiiw032.jpg "/>
T. Java
Package COM. umgsai. junit4; public class t {public int add (int x, int y) {return X + Y;} public static void main (string [] ARGs) {system. out. println (New T (). add (2, 5); // unit test is to write a class for use by others, test whether there will be a bug // do not use the main method to test the reason: cannot run together, in most cases, you need to manually observe the output to determine whether it is correct // stage: requirement analysis, development, testing, deployment, maintenance, and upgrade (the highest cost. About 60% )}}
Ttest. Java
Package COM. umgsai. junit4.test; // static introduction. The syntax after JDK 5 is import static Org. JUnit. assert. *; import Org. JUnit. after; import Org. JUnit. afterclass; import Org. JUnit. before; import Org. JUnit. beforeclass; import Org. JUnit. ignore; import Org. JUnit. test; import static Org. hamcrest. matchers. *; import COM. umgsai. junit4.t; public class ttest {@ beforeclass // run the command before all methods of the test class start. For example, you can establish a database connection in advance. Public static void beforeclass () {system. Out. println ("beforeclass...") ;}@ afterclass // run after all methods of the test class are completed. For example, shut down the database connection. Public static void afterclass () {system. out. println ("afterclass... ") ;}@ before // run the public void before () {system. out. println ("before... ") ;}@ testpublic void testadd () {// After static introduction, you can directly use the following method // fail (" not yet implemented "); int z = new T (). add (5, 3); assertequals (8, Z); // keep the bar green to keep the code clean. // asserttrue (z> 3); // asserttrue ("Z too small", z> 10); // Add the error message // assertthat (Z, is (8); // assertthat (z, allof (greaterthan (1), lessthan (10); // conditions in allof must all meet // assertthat (Z, anyof (greaterthan (56), lessthanorequalto (11); // you can meet one of the conditions. // assertthat (z, anything ()); /// assertthat ("umgsai", is ("umgsai"); // assertthat ("umgsai", not ("umgsai1 ")); // assertthat ("umgsai", containsstring ("sa"); // assertthat ("umgsai", endswith ("sai"); // assertthat ("umgsai ", startswith ("umg"); // assertthat ("umgsai", failed to ("umgsai11"); // assertthat ("umgsai", using toignoringcase ("umgsai ")); // assertthat ("umgsai", equaltoignoringwhitespace ("umgsai"); // assertthat (3.2, closeto (3.0, 0.2); // interval (2.8, 3.2) // assertthat (3.2, greaterthan (3.0); // assertthat (3.2, lessthan (12.0); // assertthat (5.0, greaterthanorto to (5.0 )); // assertthat (5.0, lessthanorequalto (5.0); // assertthat (MAP, hasentry ("key", "value"); // assertthat (iterable, hasitem ("item"); // assertthat (MAP, haskey ("key"); // assertthat (MAP, hasvalue ("value ")); // error indicates a program error // failure indicates a test failure} // @ ignore // do not execute this test method @ test (expected = Java. lang. arithmeticexception. class, timeout = 100) // an exception is expected to be thrown. and the public void testdivide () {int z = new T () is ended within 100 milliseconds (). divide (8, 0); assertthat (z, is (4) ;}@ after // execute public void after () {system. out. println ("after... ");}}
Test multiple classes at the same time. Right-click a package and choose run as> RUN confugrations> Run all tests in the selected project ....
This article is from the "Avatar" blog, please be sure to keep this source http://shamrock.blog.51cto.com/2079212/1564182
JUnit Study Notes-getting started