Junit provides a set of methods to assert tests.
Here are some examples on how to use the class org.junit.Assert:
// Check with equals() that two objects are identical Assert.assertEquals(expectedResult, actualResult); // Check with equals() that two objects are different Assert.assertNotEquals(expectedResult, actualResult); // Check that the condition is true Assert.assertTrue(result); // Check that the condition is false Assert.assertFalse(result); // Check that the result is null Assert.assertNull(obj); // Check that the result is not null Assert.assertNotNull(obj); // Check with == that two objects are the same Assert.assertSame(expectedResult, actualResult); // Check with == that two objects are not the same Assert.assertNotSame(expectedResult, actualResult); // Check that two arrays are identical Assert.assertArrayEquals(expectedArr, actualArr); // Check any condition using matchers Assert.assertThat(actualResult, Matchers.equalTo(expectedResult));
assertThat() offers some advantages as readability and flexibility. However JUnit has a limited set of matchers and you need a library called Hamcrest, if you want more.
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.