JUnit

JUnit is the most popular open source framework for Java in a test-driven development. JUnit helps in unit testing of the component. It also widely supports tools such as ANT, Maven, and Eclipse IDE. The unit test class is an ordinary class, just like any other class, with a major difference of use of the @Test annotation. The @Test annotation lets the JUnit test runner know that this annotated method needs to be executed to perform testing.

The org.junit.Assert provides class is a series of static assertXXX() methods, which perform testing by comparing the actual output to the assumed output of the method under test. If the comparison of the test returns normally, it indicates the test has passed. However, if the comparison fails, the execution stops, indicating that the test has failed.

The unit test class is normally called, unit test case. The test case can have multiple methods, which will be executed one after another in the order in which they were written. JUnit facilitates setting up the test data for a public unit under testing, and does the testing against it. The initialization of data can be done in the setUp() method, or in the method that is annotated by the @Before annotation. By default, it uses the JUnit runner to run the test case. However, it has suite, parameterised, and categories as a few more built-in runners. Along with these runners, JUnit also supports third-party runners, such as SpringJUnit4ClassRunner, MokitoJUnitRunner, and HierarchicalContextRunner. It also facilitates the use of @RunWithwhich, which facilitates using the custom runners. We will discuss the annotation along with the Spring testing framework shortly.

The following are a few assertion methods that facilitate the testing using comparison:

  • assertEquals: This method tests the equality of two objects with the help of the equals() method
  • assertTrue and assertFalse: This is used to test Boolean values against true or false conditions
  • assertNull and assetNotNull: This method tests the value to be either null or not null
  • assertSame and assertNotSame: This is used to test whether the two references passed as an argument point to the same object or not
  • assertArrayEquals: This is used to test whether the two arrays contain equal elements, and each element from one of the array is equal to the element from the other array with the same index
  • assertThat: This tests whether the object matches to an object of org.harmcrest.Matcher or not
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.84.155