Why do we need JUnit
In real world projects, there are many classes. Each class is having its methods. At the time of building application, the Developer needs to make sure that application is bug-free by testing. So, How to test it?
Possible Approach, Build complete application and test it. But it is not flexible to test the entire application as if something will be wrong. How can be identified what was wrong. Then how to test?
Test the application by its individual unit. In Java applications, classes and methods are smallest units. Using JUnit classes and methods can be tested and unit testing can be achieved.
Implementation of Test Methods using JUnit
Start the test class with @Test annotator.
Example:
Test method for a
@Test public void newArrayListsHaveNoElementsTest() { assertThat(new ArrayList<Integer>().size(), is(0)); }
@Test public void lookupEmailAddresses() { assertThat(new CartoonCharacterEmailLookupService().getResults("looney"), allOf( not(empty()), containsInAnyOrder( allOf(instanceOf(Map.class), hasEntry("id", "56"), hasEntry("email", "roadrunner@fast.org")), allOf(instanceOf(Map.class), hasEntry("id", "76"), hasEntry("email", "wiley@acme.com")) ) )); }
@Test public void sizeReturnsNumberOfElements() { List<Object> instance = new ArrayList<Object>(); instance.add(new Object()); instance.add(new Object()); assertThat(instance.size(), is(2)); }
0 comments:
Post a Comment