0

I was wondering if I wrote this test in a proper way to mock a real situation. Can you please provide any feedback?

I'm using Mockito in a Spring Boot environment. I'm new to Mockito and other mocking techniques, but I want to know if I'm on the right path or not.

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
class FileServiceImplTest {
    @Mock
    private UserPrincipal userPrincipal;
    @Mock
    private User user;
    @Mock
    private FileService fileService;

    @BeforeEach
    void initialize() {
        user = new User("testUser", "[email protected]", "testPassword");
        user.setId(1L);
        user.setRoles(List.of(Role.User));
        userPrincipal = UserPrincipal.create(user);
    }

    @Test
    void usedMockUpsShouldNotBeNull() {
        assertAll("All mock instaces of classes should not be null",
                () -> assertNotNull(fileService),
                () -> assertNotNull(userPrincipal),
                () -> assertNotNull(user)
        );
    }

    @Test
    void collectionOfFilesShouldChangeAfterNewFileIsAdded_Mockito() throws ExecutionException, InterruptedException {
        Image image = createImageFile();
        List<? super File> files = createFilesCollection();

        doReturn(files).when(fileService).getAll(userPrincipal);
        int initialSize = fileService.getAll(userPrincipal).size();

        fileService.save(image);
        doReturn(files.add(image)).when(fileService).save(image);
        int newSize = fileService.getAll(userPrincipal).size();

        assertNotEquals(newSize, initialSize);
        Mockito.verify(fileService, atLeast(2)).getAll(userPrincipal);
        Mockito.verify(fileService).save(image);
    }
}
2

1 Answer 1

2

I am sorry that you are in the wrong path.

If FileService is just an interface, you do not need to test it.

If FileService is an implementation, you should create an actual instance to test it but not a mock.

The mock is only useful for the direct dependencies of the class that you are testing (i.e FileSerivice) such that you can easily stub the result when calling methods on these dependencies and verify if the class you are testing interacts with them correctly. (i.e. call the correct methods with the correct parameters etc.)

As I cannot see FileSerivice 's source codes , but if UserPrincipal and User are not its dependencies , it does not make sense to mock them.

Also as mentioned by other in the comment, as you are not doing the integration testing with some Spring framework stuff, you should simply rewrite your test as a plain Mockito test which is simpler and run faster :

@ExtendWith(MockitoExtension.class)
public class FileServiceImplTest {


}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi thanks for your reply, I appreciate it. My intention was to test the actual FileServiceImpl(ementation). From what I understand now is that I should create an actual instace of it, got it. So when using the '@ExtendWith(MockitoExtension.class)', can you provide me an example for my situation? Because I'm really new to this world of working with Java annotations, especially in testing stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.