Friday, 4 June 2021

JUnit and Mockito

 



JUnit

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

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"))
            )
        ));
    }

Skill is Vision

Author & Editor

It is possible to fly without motors, but not without knowledge and skill. The future belongs to those who learn more skills and combine them in creative ways.

0 comments:

Post a Comment