Ii. Core-Assertions
Assertions are the core implementation method for writing test cases, that is, the expected value and test result, to determine whether the test is successful.
1. Core assertion method
Assertarrayequals (expecteds, actuals) |
Check whether the two arrays are equal. |
Assertequals (expected, actual) |
Check whether two objects are equal. Similar to the equals () method used for string comparison |
Assertnotequals (first, second) |
Check whether the two objects are not equal. |
Assertnull (object) |
Check whether the object is empty. |
Assertnotnull (object) |
Check whether the object is not empty. |
Assertsame (expected, actual) |
Check whether the references of the two objects are equal. Similar to comparing two objects with "=" |
Assertnotsame (unexpected, actual) |
Check whether the references of the two objects are not equal. Similar to = "Compares two objects |
Asserttrue (condition) |
Check whether the running result is true. |
Assertfalse (condition) |
Check whether the running result is false. |
Assertthat (actual, matcher) |
Check whether the actual value meets the specified conditions. |
Fail () |
Failed test |
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 be null", new Object()); } @Test public void testAssertNotSame() { org.junit.Assert.assertNotSame("should not be 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(Arrays.asList("one", "two", "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()))); }}
Iii. Core-Comment
1. Description
@ Before |
Initialization Method |
@ After |
Release resources |
@ Test |
Test method. Here we can test the expected exception and timeout. |
@ Ignore |
Test method ignored |
@ Beforeclass |
All tests are executed only once and must be static void |
@ Afterclass |
All tests are executed only once and must be static void |
@ Runwith |
Specify a runner for the test class |
@ Parameters |
Test data set of the specified test class |
@ Rule |
Allow flexible addition or redefinition of the behavior of each test method in the test class |
@ Fixmethodorder |
Specify the execution sequence of the test method |
2. execution sequence
The execution sequence of a test unit test is:
@ Beforeclass-> @ before-> @ test-> @ After-> @ afterclass
The call sequence of each test method is as follows:
@ Before-> @ test-> @ after
3. Example
package test;import static org.junit.Assert.*;import org.junit.*;public class JDemoTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {System.out.println("in BeforeClass================");}@AfterClasspublic static void tearDownAfterClass() throws Exception {System.out.println("in AfterClass=================");}@Beforepublic void before() {System.out.println("in Before");}@Afterpublic 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");}@Testpublic void testdivision() {JDemo a = new JDemo();assertEquals(3, a.division(6, 2));System.out.println("in Test ----Division");}@Ignore@Testpublic void test_ignore() {JDemo a = new JDemo();assertEquals(6, a.add(1, 5));System.out.println("in test_ignore");}@Testpublic 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 Beforein Test ----Addin Afterin Beforein Test ----Divisionin Afterin AfterClass=================
In the upper-left red box in the figure, the result of running JUnit is displayed. Five are successful (one is ignored), one is incorrect, and one is failed. (Note that errors and failures are not the same. errors indicate code errors. Failure indicates that the test method fails)
In the lower-left red box, the running status of each test method is displayed. The icons for success, error, failure, and failure are different, and the running time is displayed.
The right part is the exception stack. You can view the exception information.
In the next article, we will provide more examples and continue to introduce JUnit.