Please note, this is a STATIC archive of website www.javatpoint.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Javatpoint Logo
Javatpoint Logo

Examples of Mockito and JUnit in Eclipse IDE

Here, we are going to use the Mockito framework along with the JUnit framework. We can also use Spring Boot instead of using the JUnit framework.

JUnit is one of the testing frameworks used by the Java programmers for creating test cases. Like Mockito, JUnit is also an open-source testing framework. To deeply understand the concepts of the JUnit framework, click on the given link, https://www.javatpoint.com/junit-tutorial.

1. Stubbing example

In this example, we are going to test a simple module using stubs. Following are the steps to create an example of testing:

Directory Structure

Examples of Mockito and JUnit in Eclipse IDE

Step 1: Add Maven dependencies required by the application. The dependencies are always added in pom.xml file of the application. In this example you need to add the two dependencies:

  1. JUnit dependency
  2. Mockito dependency

Step 2: Create an interface named ToDoService.java that contains an unimplemented method, as shown below.

ToDoService.java

Step 3: Create a Java class named ToDoBusiness.java that contains a parameterized constructor and a method, as shown below.

ToDoBusiness.java

Step 4: Create a stub class named ToDoServiceStub.java in the source folder of the test (src/test/java), for testing purposes.

ToDoServiceStub.java

Step 5: Create another stub named ToDoBusinessStub.java, as shown below.

ToDoBusinessStub.java

Output

The following output shows that the stub is working correctly.

Examples of Mockito and JUnit in Eclipse IDE
Examples of Mockito and JUnit in Eclipse IDE

Disadvantages of Stubbing

In the above example, the ToDoBusinessStub depends on the ToDoService interface, so we have created a stub named ToDoServiceStub that returns dummy values. The ToDoServiceStub is nothing, but a real Java class. There are a lot of maintenance problems using stubs. For example, if we want to add a new method or delete an existing method to/from the ToDoService interface, we need to keep track on the stub related to this interface. Another problem with stubs is the dynamic conditions that makes the code more complicated.

Stubs are useful in simple projects and scenarios, but in complex scenarios, we need something more dynamic than stubs. To overcome the drawbacks, mocks came into play instead of stubs.

In the above example, we didn't make use of the Mockito framework yet, as we have created a simple module for testing. Now, we are going to create an example of mocking. Before creating a mocking example, we should understand some important concepts and methods that are going to be used in the example.

2. Mocking Example

Here, we are going to create an example of mock objects. The following steps are used to create an example of testing using Mockito with JUnit.

Step 1: Create an interface named ToDoService that contains an unimplemented method.

ToDoService.java

Step 2: Now, create an implementation class named ToDoBusiness for ToDoService interface.

ToDoBusiness.java

Step 3: Create a JUnit test case named ToDoBusinessMock for unit testing.

ToDoBusinessMock.java

Output

The following output shows that the test is successfully running. It is also showing that object is available in the list, i.e., "Use Hibernate."

Examples of Mockito and JUnit in Eclipse IDE

3. Example of mocking a List class

Here, we are going to create an example of mocking a List class (java.util.List).

Step 1: Create a mock test class named TestList for testing the List class.

TestList.java

Output

The following output shows that the test is running successfully with one item available on the list. It also shows the values of the object.

Examples of Mockito and JUnit in Eclipse IDE

4. Example of multiple return values of a List

Here, we are going to create an example of mocking a List class (java.util.List) with multiple return values. In the previous example, the list returns only one object (as it contains only one), whereas, in the following example, it returns multiple values (as it contains three items).

Step 1: Create a JUnit test case named TestList for testing the List class.

TestList.java

Output

The following output shows that the test is successfully running with multiple return values of a List.

Examples of Mockito and JUnit in Eclipse IDE

5. Example of mocking List.get() method

In this example, we are going to mock a List.get() method. To use the get() method, we need to pass a value (number) in it, as shown in the example.

Step 1: Create a test class named TestList for mocking the List.get() method.

TestList.java

Output

The following output shows that the test is successfully running when the index value of the list is 0, and it returns the given String, i.e., Mockito.

Examples of Mockito and JUnit in Eclipse IDE

But if we want to check assertEquals("Mockito", mocklist.get(1)), the test would fail and throws an error, as shown below.

Examples of Mockito and JUnit in Eclipse IDE

It returns Mockito always, when the mocklist.get() method is called. To avoid this the argument matchers comes into existence. With the help of argument matchers, we can get the value of any integer, whether it is stubbed or not.

6. Example of verify() method

Here, we are going to create an example of testing using the Mockito's verify() method.

Step 1: Create an interface named ToDoService that contains two unimplemented methods.

ToDoService.java

Step 2: Create an implementation class named ToDoBusiness, and delete todos (object of List) that are not related to "Hibernate."

ToDoBusiness.java

Step 3: Create a mock class named ToDoBusinessMock and verify the specified method (deleteTodosNotRelatedToHibernate()) is invoked or not.

ToDoBusinessMock.java

Output

The following output shows that the test is successfully running. Hence, it shows that all the methods are verified well.

Mockito verify() method

If you want an item that should never be called or deleted, a method never() is used to do so, For example:

By using the method times(), you can specify how many times a specific method is called. For example:

Similarly, you can use other methods of Mockito such as atLeastOnce(), atLeast(), and atMost() with verify().

7. Example of Mockito spy()

Let's create a test class named SpyTest for testing purposes.

SpyTest.java

In the above code, we have created a spy of the ArrayList class and checked its size. The size of the list is zero, it means the list is empty. After checking the list size, we have added an item ("Mockito") in the List. Now, the size has increased by one as we have discussed that the spy creates a partial mock of the real object.

Output

Mockito spy method

The spy() method let the real action continue to happen except the stuff where they want to change the behavior. It means spy() method allows us to keep track of what is happening with the real object as well as allow us to overrides a specific behavior.


Next TopicArgument Matchers





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA