首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在SpringBoot中取笑时获取SpringBoot

在SpringBoot中取笑时获取SpringBoot
EN

Stack Overflow用户
提问于 2018-12-04 22:50:21
回答 1查看 819关注 0票数 2

我试图为其中一个类编写一个Junit测试用例。但是在尝试的时候出错了,

测试类如下所示

代码语言:javascript
复制
public class IntegratorClassTest{
    @InjectMocks    
    IntegratorClass integratorClass;

    @Mock
    RequestClass requestClass;

    @Mock
    ContentList contentResponse;

    @Before
    public void setUp() throws Exception {

        MockitoAnnotations.initMocks(this);
    }


    @Test
    public void getCmsOffersTest()throws Exception{
        ContentService contentService = Mockito.mock(ContentService.class);
        RequestClass requestClass = Mockito.mock(RequestClass.class);
        ContentList contentResponse = getContentList();
        when(contentService.getContentCollection()).thenReturn(contentResponse);

        Validator validator = Mockito.mock(Validator.class);
        List<OfferDetails> offerList = new ArrayList<OfferDetails>();
        Mockito.doNothing().when(validator).validateData(offerList);

        List<OfferDetails> offerListResult = integratorClass.getCmsOffers(contentService, requestClass);
        assertTrue(offerListResult.size()>0);
    }
}

实现类如下所示-

代码语言:javascript
复制
public class IntegratorClass {
    private static final Logger LOGGER = LoggerFactory.getLogger(IntegratorClass.class);

    @Autowired
    Validator validator;

    public List<OfferDetails> getCmsOffers(ContentService contentService,RequestClass requestClass)throws Exception{
        LOGGER.info("Entered method getCmsOffers to get the list of offers from CMS");
        List<OfferDetails> offerList = new ArrayList<OfferDetails>();
        ContentList contentResponse = null;
        try 
        {
            contentResponse = contentService.getContentCollection();
            offerList = getOfferListFromCmsResponse(contentResponse, requestClass);

            LOGGER.info("Total number of active offers we got from CMS are -" + offerList.size());
        }catch (Exception e)
        {
            ErrorResponse errorResponse = PromotionalOffersUtilities.createErrorResponse("500", e.getMessage(),"Getting error while fetching content from CMS - getCmsOffers", ErrorResponse.Type.ERROR);
            LOGGER.error("Getting error while fetching content from CMS with Error Message: " + e.getMessage());
            throw new ServiceException(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
        }

        //failing here
        validator.validateData(offerList);
        LOGGER.info("Exiting method getCmsOffers");

        return offerList;
    }
}

当我在调试模式下运行它时,行validator.validateData(offerList);的am错误。

它正在返回"NullPointerException“。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-05 00:10:10

您需要在模拟依赖项时包含Validator,以便将其注入被测试的主题中。

代码语言:javascript
复制
@Mock
Validator validator;

此外,在安排validator的行为时,请为被调用的成员使用参数匹配器,因为当前的设置将不匹配,因为在执行测试时,它们将比较不同的实例。

代码语言:javascript
复制
Mockito.doNothing().when(validator).validateData(any(List<OfferDetails>.class));

您正在手动模拟测试方法中的其他依赖项,因此在测试方法的外部不需要它们。

现在测试变成

代码语言:javascript
复制
public class IntegratorClassTest{
    @InjectMocks
    IntegratorClass integratorClass;

    @Mock
    Validator validator;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getCmsOffersTest()throws Exception{
        //Arrange
        ContentService contentService = Mockito.mock(ContentService.class);
        RequestClass requestClass = Mockito.mock(RequestClass.class);
        ContentList contentResponse = getContentList();
        Mockito.when(contentService.getContentCollection()).thenReturn(contentResponse);

        Mockito.doNothing().when(validator).validateData(any(List<OfferDetails>.class));

        //Act
        List<OfferDetails> offerListResult = integratorClass.getCmsOffers(contentService, requestClass);

        //Assert
        assertTrue(offerListResult.size() > 0);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53622629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档