E-Book, Englisch, 214 Seiten
Acharya Mockito Essentials
1. Auflage 2025
ISBN: 978-1-78398-361-2
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
A practical guide to get you up and running with unit testing using Mockito
E-Book, Englisch, 214 Seiten
ISBN: 978-1-78398-361-2
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
This book is ideal for developers who have some experience in Java application development as well as some basic knowledge of test doubles and JUnit testing. This book also introduces you to the fundamentals of JUnit testing, test doubles, refactoring legacy code, and writing JUnit tests for GWT and web services.
Autoren/Hrsg.
Weitere Infos & Material
Exploring a test spy
A spy secretly obtains the information of a rival or someone very important. As the name suggests, a spy object spies on a real object. A spy is a variation of a stub, but instead of only setting the expectation, a spy records the method calls made to the collaborator. A spy can act as an indirect output of the unit under test and can also act as an audit log.
We'll create a spy object and examine its behavior; the following are the steps to create a spy object:
- Launch Eclipse, open , and go to the project.
- Create a package and create a class. This class will act as a course register service. The following is the code for the class: public class StudentService { private Map
The class contains a map of the course names and students. The method looks up the map; if no student is enrolled, then it creates a collection of students, adds the student to the collection, and puts the collection back in the map. If a student has previously enrolled for the course, then the map already contains a collection. So, it just adds the new student to the list.
- The method is a method and doesn't return a response. To verify that the method was invoked with a specific set of parameters, we can create a spy object. The service will write to the spy log, and the spy will act as an indirect output for verification. Create a spy object to register method invocations. The following code gives the method invocation details: class MethodInvocation { private List
The class represents a method invocation: the method name, a parameter list, and a return value. Suppose a method is invoked with two numbers and the method returns the sum of two numbers, then the class will contain a method name as , a parameter list that will include the two numbers, and a return value that will contain the sum of the two numbers.
Note
Note that the setter methods return . This coding approach is known as builder pattern. It helps to build an object in multiple steps. Java is an example of such a use:
StringBuilder builder = new StringBuilder(); builder.append("step1").append("step2")…The following is the spy object snippet. It has a method to log a method call instance. It has a map of strings and a method. If a method is invoked 10 times, then the map will contain the method name and a list of 10 objects. The spy object provides an invocation method that accepts a method name and returns the method invocation count from the class:
public class StudentServiceSpy { private MapThe method takes a object and puts it in a map.
- Modify the class to set a spy and log every method invocation to the spy object: private StudentServiceSpy spy; public void setSpy(StudentServiceSpy spy) { this.spy = spy; } public void enrollToCourse(String courseName, Student student) { MethodInvocation invocation = new MethodInvocation(); invocation.addParam(courseName).addParam(student).setMethod("enrollToCourse"); spy.registerCall(invocation); List
- Write a test to examine the method invocation and arguments. The following JUnit test uses the spy object and verifies the method invocation: public class StudentServiceTest { StudentService service = new StudentService(); StudentServiceSpy spy = new StudentServiceSpy(); @Test public void enrolls_students() throws Exception { //create student objects Student bob = new Student("001", "Robert Anthony"); Student roy = new Student("002", "Roy Noon"); //set spy service.setSpy(spy); //enroll Bob and Roy service.enrollToCourse("english", bob); service.enrollToCourse("history", roy); //assert that the method was invoked twice assertEquals(2, spy.invocation("enrollToCourse")); //get the method arguments for the first call List




