我正在尝试为后置控制器编写测试用例。我想在post控制器内调用特定方法时模拟异常。但是,它根本不起作用。该函数被执行并且测试失败。
测试用例:
@Test(expected = ControllerException.class)
public void testUpdateModelsSapFailure() throws Exception {
String request = getUpdateModelsRequest(SKILL_ID, modelDefinition, SKILL_VERSION);
// Make SAP call fail with an exception
when(mockSapModelBuilder.buildDomainDefinition())
.thenThrow(new ControllerException(org.springframework.http.HttpStatus.BAD_REQUEST,""));
try {
postController.updateApplicationModels(request, DEVELOPER);
}
finally {
verifyModelIsNotSaved();
verifyAmsIsNotCalledForUpdateSkill();
verifyAmsIsNotCalledForEnableSkill();
}
}
堆栈跟踪:
Testcase: testUpdateModelsSapFailure(com.dew.controller.ManagementPostControllerTest): Caused an ERROR
[junit] Unexpected exception, expected<com.dew.exception.ControllerException> but was<org.mockito.exceptions.verification.NeverWantedButInvoked>
[junit] java.lang.Exception: Unexpected exception, expected<com.dew.exception.ControllerException> but was<org.mockito.exceptions.verification.NeverWantedButInvoked>
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.handleException(PowerMockJUnit44RunnerDelegateImpl.java:364)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.handleException(PowerMockJUnit47RunnerDelegateImpl.java:122)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.handleInvocationTargetException(PowerMockJUnit44RunnerDelegateImpl.java:337)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:315)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
[junit] at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
[junit] at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
[junit] at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
[junit] Caused by: org.mockito.exceptions.verification.NeverWantedButInvoked:
[junit] aSKModelStorageClient.saveModelDefinition(
[junit] <any>,
[junit] <any>,
[junit] Development,
[junit] DewApplicationModelDefinition(models=domain definition, testCases=test cases, Calendars=[Calendar(name=CalendarName, entities=[CalendarEntity(value=CalendarValue)])]),
[junit] <any>
[junit] );
[junit] Never wanted here:
[junit] -> at com.dew.controller.ManagementPostControllerTest.verifyModelIsNotSaved(ManagementPostControllerTest.java:1829)
[junit] But invoked here:
[junit] -> at com.dew.controller.ManagementPostController.updateApplicationModels(ManagementPostController.java:1250)
[junit]
[junit] at com.dew.controller.ManagementPostControllerTest.verifyModelIsNotSaved(ManagementPostControllerTest.java:1829)
[junit] at com.dew.controller.ManagementPostControllerTest.testUpdateModelsSapFailure(ManagementPostControllerTest.java:863)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
VerifyModelIsNotSaved:
private void verifyModelIsNotSaved() throws ModelException {
verify(mockModelStorageClient, times(0)).saveModelDefinition(anyString(), anyString(),
anyString(), eq(modelDefinition), anyString());
}