【问题标题】:How to mock factory method in service test with mockito如何使用 mockito 在服务测试中模拟工厂方法
【发布时间】:2021-10-13 18:05:47
【问题描述】:

您好,我正在尝试测试服务层。我已经为ConverterFactory 编写了测试。我想我需要ConverterServiceImpl 使用的模拟依赖类,但我仍然得到NullPointerException

这是我的服务类

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

这就是测试

@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");

    }
}

此代码抛出错误说。我在这里做错了什么?:

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

【问题讨论】:

    标签: java spring junit mockito


    【解决方案1】:

    您不需要在测试方法中再次创建ConverterFactory converterFactory = mock(ConverterFactory.class),因为您已经创建了这样的模拟作为类字段。

    此外,您没有将在测试方法中创建的模拟注入到被测类中,而作为字段创建的模拟是使用@InjectMocks 注解注入的。

    所以只需从测试方法中删除ConverterFactory converterFactory = mock(ConverterFactory.class)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多