gpt4 book ai didi

java - SpringBoot中mocking时出现NullPointerException

转载 作者:行者123 更新时间:2023-11-30 05:52:59 24 4
gpt4 key购买 nike

我正在尝试为其中一个类编写一个 Junit 测试用例。但在尝试执行此操作时出现错误,

测试类如下所示 -

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

实现类如下所示 -

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

当我在 Debug模式下运行它时,validator.validateData(offerList); 行出现错误。

它返回“NullPointerException”。

最佳答案

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

@Mock
Validator validator;

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

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

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

测试现在变成了

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

关于java - SpringBoot中mocking时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53622629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com