user16906859
user16906859

Reputation: 21

Java writing Spring Boot Unit Tests

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

Upvotes: 0

Views: 784

Answers (1)

Ken Chan
Ken Chan

Reputation: 90527

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 {


}

Upvotes: 2

Related Questions