Home > Back-end >  How to mock factory method in service test with mockito
How to mock factory method in service test with mockito

Time:10-14

Hi I am trying to test service layer. I have already wrote tests for ConverterFactory. I think I need the mock dependency classes which ConverterServiceImpl using but Still I got NullPointerException

This is my service class

@Service
@RequiredArgsConstructor
public class ConverterServiceImpl implements ConverterService {

    ConverterFactory factory = new ConverterFactory();
    private final WebLinkRepository webLinkRepository;
    private final DeepLinkRepository deepLinkRepository;

    @Override
    public DeepLinkResponse toDeepLink(WebLinkRequest webLinkRequest) {

        WebLink webLink;
        String url = webLinkRequest.getUrl();
        Converter converter = factory.getConverter(url);


        webLink = new WebLink();
        webLink.setUrl(url);
        String convertedUrl = converter.toDeepLink(url);
        webLink.setConvertedUrl(convertedUrl);
        webLinkRepository.save(webLink);

        return new DeepLinkResponse(convertedUrl);
    }
}

And this is the test

@RunWith(MockitoJUnitRunner.class)
public class ConverterServiceImplTest {


    @InjectMocks
    ConverterServiceImpl converterService;

    @Mock
    WebLinkRepository webLinkRepository;

    @Mock
    DeepLinkRepository deepLinkRepository;

    @Mock
    ConverterFactory converterFactory;

    @Mock
    ProductConverter productConverter;

    @Mock
    WebLinkRequest webLinkRequest;

    @BeforeAll
    void init(){
        webLinkRequest.setUrl(WEBLINK_ONLY_PRODUCT);
    }

    @Test
    public void toDeepLink_only_content_id() {
        ConverterFactory converterFactory = mock(ConverterFactory.class);
        when(converterFactory.getConverter(any())).thenReturn(productConverter);

        DeepLinkResponse deepLinkResponse = converterService.toDeepLink(webLinkRequest);
        assertEquals(deepLinkResponse.getUrl(),"ty://?Page=Product&ContentId=1925865");

    }
}

This code throws error says. What am i doing wrong here?:

java.lang.NullPointerException
    at com.example.converter.service.factory.ConverterFactory.getConverter(ConverterFactory.java:13)

CodePudding user response:

You don't need to create a ConverterFactory converterFactory = mock(ConverterFactory.class) a second time in your test method, since you have already created such mock as a class field.

Besides, you did not inject the mock created in the test method into the class under test, whereas the mock, created as a field, was injected using @InjectMocks annotation.

So just remove ConverterFactory converterFactory = mock(ConverterFactory.class) from test method:

@RunWith(MockitoJUnitRunner.class)
public class ConverterServiceImplTest {


    @InjectMocks
    ConverterServiceImpl converterService;

    @Mock
    ConverterFactory converterFactory;

    // other stuff


    @Test
    public void toDeepLink_only_content_id() {
        when(converterFactory.getConverter(any())).thenReturn(productConverter);

        // other stuff

        converterService.toDeepLink(webLinkRequest);
    }
}
  • Related