Assignment #3: Mocking
Part A
Get Eclipse and create a new maven project. Then add dependencies in the pom.xml file. For some reason I get two errors, first line of the pom.xml saying something cannot find the external dependency. However, it works when I check if the JUnit is there and working…
Part B
ListTestTest.java:
For each of the four unit tests, you can see the line
To make letsMockListSizeWithMultipleReturnValues() pass, add this line after the commented line:
Now run the tests again and it should pass
By default, for all methods that return a value, a mock will return either null, a primitive/primitive wrapper value, or an empty collection, as appropriate. For example 0 for an in/Integer and false for a boolean/Boolean.
Once stubbed, the method will always return a stubbed value, regardless of how many times it is called, unless overridden by specifying another value through another stub.
The last stubbing is more important - when you stubbed the same method with the same arguments many times. Other words: the order of the stubbing matters but it is only meaningful rarely, e.g. when stubbing exactly the same method calls or sometimes when argument matchers are used, etc.
Part C - Mocking a class implementation
Now that we have mocked the List class from Java, we will see how mocking a dependency works. In real life, rather than mocking a List object, we would have instances of an actual one. You can find the following Java files in Assignmemt3.zip.
TodoService.java interface
TodoBusinessImplMockitoTest.java test case
TodoBusinessImpl.java class
After you finish all the experiment parts (part A to part C), you need to answer the corresponding questions in the following report and provide appropriate screenshots and / or source files to support your answers.
Please submit it as a PDF file on moodle with the file name of ‘Assignment3_yourname_yourstudentID.pdf’.
Questions
Question 3 2%: Copy the three files above into the same test directory as Part C
Observe the three files and run the unit tests in TodoBusinessImplMockitoTest.java. Describe the interactions.
Observing the provided files and running the unit tests in TodoBusinessImplMockitoTest.java
gives us insights into the interactions between the classes and the mocked dependencies.
- TodoService.java (interface):
- This interface defines a contract for retrieving and deleting todos. It includes methods
retrieveTodos(String user)
anddeleteTodo(String todo)
.
- This interface defines a contract for retrieving and deleting todos. It includes methods
- TodoBusinessImpl.java (class):
- This class implements business logic related to todos. It has a dependency on
TodoService
interface. - The constructor injects a
TodoService
implementation into theTodoBusinessImpl
instance. - It contains methods:
retrieveTodosRelatedToSpring(String user)
: Retrieves todos related to “Spring” for a given user.deleteTodosNotRelatedToSpring(String user)
: Deletes todos not related to “Spring” for a given user.
- This class implements business logic related to todos. It has a dependency on
- TodoBusinessImplMockitoTest.java (test case):
- This test case class verifies the behaviour of
TodoBusinessImpl
using Mockito for mockingTodoService
. - It mocks the
TodoService
interface using Mockito’smock()
method. - It stubs the behaviour of the
retrieveTodos(String user)
method of the mockedTodoService
to return a predefined list of todos when invoked with a specific user. - It then tests the
retrieveTodosRelatedToSpring(String user)
method ofTodoBusinessImpl
to ensure it filters todos related to “Spring” correctly. - The test verifies that the
retrieveTodosRelatedToSpring(String user)
method returns the expected number of todos related to “Spring” for a given user.
- This test case class verifies the behaviour of
Interactions:
- The
TodoBusinessImpl
class interacts with theTodoService
interface to retrieve and filter todos based on certain criteria (in this case, whether the todo contains “Spring” in its description). - In the test case, Mockito is used to mock the behaviour of the
TodoService
interface, specifically theretrieveTodos(String user)
method, to return predefined todos when called with a specific user. - The
TodoBusinessImplMockitoTest
class then verifies that theretrieveTodosRelatedToSpring(String user)
method ofTodoBusinessImpl
correctly filters todos related to “Spring” based on the mocked behaviour ofTodoService
. - No actual deletion of todos is performed as the
TodoService
is mocked, and only retrieval behaviour is stubbed for testing purposes.