Knowledge Point-assertion
Assertions are the core implementation of writing test cases, that is, what the expected value is, and how much is the result of the test to determine whether the test passed.
1. Assert the core approach
Assertarrayequals (expecteds, actuals) |
See if two arrays are equal. |
Assertequals (expected, actual) |
See if two objects are equal. The Equals () method, similar to the use of string comparisons |
Assertnotequals (first, second) |
See if two objects are not equal. |
Assertnull (object) |
View whether the object is empty. |
Assertnotnull (object) |
View whether the object is not empty. |
Assertsame (expected, actual) |
See if the references to two objects are equal. Similar to using "= =" to compare two objects |
Assertnotsame (Unexpected, actual) |
See if the references to two objects are not equal. Similar to using "! =" to compare two objects |
Asserttrue (condition) |
View whether the run result is true. |
Assertfalse (condition) |
See if the run result is false. |
Assertthat (Actual, Matcher) |
See if the actual value meets the specified criteria |
Fail () |
Let the test fail |
2. Example
- Package test;
- Import static org.hamcrest.corematchers.*;
- Import static org.junit.assert.*;
- Import Java.util.Arrays;
- Import Org.hamcrest.core.CombinableMatcher;
- Import Org.junit.Test;
- Public class Asserttests {
- @Test
- public void Testassertarrayequals () {
- byte[] Expected = "trial". GetBytes ();
- byte[] actual = "trial". GetBytes ();
- Org.junit.Assert.assertArrayEquals ("failure-byte arrays not Same", expected, actual);
- }
- @Test
- public void Testassertequals () {
- Org.junit.Assert.assertEquals ("failure-strings not Same", 5l, 5l);
- }
- @Test
- public void Testassertfalse () {
- Org.junit.Assert.assertFalse ("failure-should be false", false);
- }
- @Test
- public void Testassertnotnull () {
- Org.junit.Assert.assertNotNull ("should not is null", new Object ());
- }
- @Test
- public void Testassertnotsame () {
- Org.junit.Assert.assertNotSame ("should not being same object", new Object (), new Object ());
- }
- @Test
- public void Testassertnull () {
- Org.junit.Assert.assertNull ("should be null", null);
- }
- @Test
- public void Testassertsame () {
- Integer anumber = integer.valueof (768);
- Org.junit.Assert.assertSame ("should be Same", anumber, Anumber);
- }
- //JUnit matchers assertthat
- @Test
- public void Testassertthatbothcontainsstring () {
- Org.junit.Assert.assertThat ("albumen", Both (containsstring ("a")). and (Containsstring ("B"));
- }
- @Test
- public void Testassertthathasitemscontainsstring () {
- Org.junit.Assert.assertThat ("One", "Arrays.aslist", " three"), Hasitems ("One", "three");
- }
- @Test
- public void Testassertthateveryitemcontainsstring () {
- Org.junit.Assert.assertThat (Arrays.aslist (new string[] { "fun", "ban", "NET"}), Everyitem ( Containsstring ("n")));
- }
- //Core hamcrest matchers with Assertthat
- @Test
- public void Testassertthathamcrestcorematchers () {
- Assertthat ("Good", AllOf (Equalto ("good"), StartsWith ("good"));
- Assertthat ("Good", Not (AllOf (Equalto ("bad"), Equalto ("good")));
- Assertthat ("Good", AnyOf (Equalto ("bad"), Equalto ("good"));
- Assertthat (7, not (combinablematcher.<integer> either (Equalto (3)). or (Equalto (4)));
- Assertthat (new Object (), not (Sameinstance (new Object ())));
- }
- }
Knowledge points--annotations
1. Description
@Before |
Initialize method |
@After |
Freeing resources |
@Test |
Test method, where you can test for expected exceptions and time-outs |
@Ignore |
Ignored test methods |
@BeforeClass |
For all tests, execute only once and must be static void |
@AfterClass |
For all tests, execute only once and must be static void |
@RunWith |
Specifies that a test class uses a runner |
@Parameters |
To specify a test data collection for a test class |
@Rule |
Allows flexibility to add or redefine the behavior of each test method in a test class |
@FixMethodOrder |
Specify the order in which test methods are executed |
2. Execution order
A Test class unit test is executed in the following order:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
The order in which each test method is called is:
@Before –> @Test –> @After
3. Example
- Package test;
- Import static org.junit.assert.*;
- Import org.junit.*;
- Public class Jdemotest {
- @BeforeClass
- public static void Setupbeforeclass () throws Exception {
- System.out.println ("in beforeclass================");
- }
- @AfterClass
- public static void Teardownafterclass () throws Exception {
- System.out.println ("in afterclass=================");
- }
- @Before
- public Void before () {
- System.out.println ("in Before");
- }
- @After
- public Void after () {
- System.out.println ("in after");
- }
- @Test (timeout = 10000)
- public void Testadd () {
- Jdemo a = new Jdemo ();
- Assertequals (6, A.add (3, 3));
- System.out.println ("in Test----Add");
- }
- @Test
- public void Testdivision () {
- Jdemo a = new Jdemo ();
- Assertequals (3, A.division (6, 2));
- System.out.println ("in Test----Division");
- }
- @Ignore
- @Test
- public void Test_ignore () {
- Jdemo a = new Jdemo ();
- Assertequals (6, A.add (1, 5));
- System.out.println ("in Test_ignore");
- }
- @Test
- public void Teest_fail () {
- Fail ();
- }
- }
- Class Jdemo extends Thread {
- int result;
- public int Add (int A, int b) {
- try {
- Sleep (1000);
- result = a + B;
- } catch (Interruptedexception e) {
- }
- return result;
- }
- Public int Division (int A, int b) {
- return result = A/b;
- }
- }
Execution Result:
- In beforeclass================
- In Before
- In Test----ADD
- In after
- In Before
- In Test----Division
- In after
- In afterclass=================
The upper left red box in the figure represents the JUnit run result, 5 successes (one ignore), 1 errors, and 1 failures. (Note that errors and failures are not the same, the error indicates that the code has an error, and failure indicates that the test method test failed)
The lower left red box indicates the running state of each test method, and you can see that the respective icons for success, error, failure, and failure are different, and you can see the run time.
The right part is the exception stack, which allows you to view exception information.
JUnit uses first bullet