0

I am trying to mock a function that is called by other function that I am trying to unit test. I am currently using the following code, following the suggestions given here:

@QuarkusTest
public class SampleServiceTest {
    @Inject
    SampleService sampleService;

    @Test
    public void testFindById() {
        // Given
        final Sample sample = SampleDataProvider.createSample();
        final SampleResponse expectedResponse = SampleDataProvider.createSampleResponse();
        MockedStatic<SampleResponseAssembler> mockedStatic = mockStatic(SampleResponseAssembler.class);

        // When
        doReturn(sample).when(sampleService).findSampleById(SampleDataProvider.ID);
        mockedStatic.when(() -> SampleResponseAssembler.fromSample(sample)).thenReturn(expectedResponse);
        final SampleResponse sampleResponse = sampleService.findById(SampleDataProvider.ID);

        // Then
        verify(sampleService, times(1)).findSampleById(SampleDataProvider.ID);
        mockedStatic.verify(() -> SampleResponseAssembler.fromSample(sample), times(1));
        assertEquals(expectedResponse, sampleResponse);
    }
}

The functions being tested:

    public SampleResponse findById(final UUID id) {
        LOGGER.debug("findById. id={}", id);
        
        return SampleResponseAssembler.fromSample(findSampleById(id));
    }

    public Sample findSampleById(final UUID id) {
        LOGGER.debug("findSampleById. id={}", id);

        final Optional<Sample> optionalSample = sampleRepository.findById(id);
        return optionalSample
                .orElseThrow(() -> new NotFoundException(NotFoundException.NotFoundErrorMessage.SAMPLE_ID,
                        id.toString()));
    }

I basically want to be able to mock the findSampleById function. I already made this change and it worked properly but "initMocks" is deprecated so I need another solution:

    @Spy
    @InjectMocks
    SampleService sampleService;

    @Mock
    SampleRepository sampleRepositoryMock;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.initMocks(this);
    }
Hugo Vinhal
  • 305
  • 1
  • 15

1 Answers1

1

Use @ExtendWith(MockitoExtension.class) instead of @QuarkusTest.

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

@ExtendWith(MockitoExtension.class)
class SampleServiceTest {

    @Spy
    @InjectMocks
    SampleService sampleService;

    @Mock
    SampleRepository sampleRepositoryMock;

    @BeforeEach
    void setUp() {
        // Not needed
    }

    @Test
    void testFindById() {
        // ...
    }
}

Also, IMO, if we have to mock findSampleById to test findById, you should split them into two classes and not use @Spy. When the code is hard to test it may be the case it needs to be better designed.

Edgar Domingues
  • 930
  • 1
  • 8
  • 17
  • I agree with what you are saying but where would I put the logic of findSampleById? I also dont think it is bad design to call methods from the same class, i guess it depends of what is being done in this functions and, in this case, I dont see it being apart of another class :S Anyway, I didnt quite understand the answer. Should I use the code of the last snippet besides the @BeforeEach? And how do I use @RunWith? Is is a differente dependency? – Hugo Vinhal Dec 27 '21 at 21:49
  • Added a code example. @RunWith is from junit – Edgar Domingues Dec 28 '21 at 09:09
  • Just asked about it because I wasnt able to import RunWith. I will give it a go next week and then give you the heads up! Thank you! :D – Hugo Vinhal Dec 29 '21 at 21:00
  • I did it! The code you provided wasnt working 100% tho. I had to change the ```@RunWith(MockitoJUnitRunner.class)``` to ```@ExtendWith(MockitoExtension.class)```. This is where I found the solution: https://stackoverflow.com/questions/40961057/how-to-use-mockito-with-junit5?fbclid=IwAR2cLds5O4vso1kbc-DVRRO7E-hzb_iS8tRsLYeKi97ciW0n_-zrmGPounw. I would say that you could edit you answer with this correction and then I can approve it :D – Hugo Vinhal Jan 03 '22 at 14:32
  • So looks like it's not possible mock static in `@QuarkusTest`. – Panu Haaramo Jan 31 '22 at 09:03