英文:
Error with JUnit testing failure condition with Custom Exception
问题
以下是您提供的内容的翻译:
我正在为 Rest Controller 编写一个错误情况的测试用例,当我将预期的异常设置为 Exception.class 时,测试运行没有错误。但是,当我将预期的异常更改为 CustomException.class 时,测试失败并显示断言错误。
这是我的 Controller 类:
@RestController
public class CustomController<CustomFieldRequest customFieldRequest, CustomField customField> {
@PutMapping(path = "/{customFieldId}",
consumes = MediaType.APPLICATION_JSON_VALUE, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE})
public CustomField updateCustomField(@PathVariable String customFieldId, @RequestBody CustomFieldRequest customFieldRequest) throws CustomException {
customFieldRequest.setAuthorId(getUser());
customFieldRequest.setId(customFieldId);
return CustomService.updateCustomField(customFieldRequest);
}
}
这是我的 service 类:
@Service
public class CustomServiceImpl implements CustomService {
@Override
public CustomField updateCustomeField(CustomFieldRequest customFieldRequest) throws CustomException {
}
}
这是我的自定义异常类:
public class CustomException extends Exception {
public CustomException(final String message) {
super(message);
}
public CustomException(final String message, final Throwable cause) {
super(message, cause);
}
}
这是我的测试类:
public class CustomControllerTest {
private CustomService customService = Mockito.mock(CustomService.class);
@Test(expected=Exception.class)
public void updateCustomFieldThrowsException() throws CustomException {
//exception.expect(Exception.class);
CustomFieldRequest request = getRequest();
when(customService.updateCustomeField(request)).thenThrow(Exception.class);
//customController.updateCustomField(request.getId(), request);
}
}
如上所述,如果我在测试中将预期的异常更改为 CustomException.class,如下所示:
@Test(expected=CustomException.class)
public void updateCustomFieldThrowsException() throws CustomException {
//exception.expect(Exception.class);
CustomFieldRequest request = getRequest();
when(customService.updateCustomeField(request)).thenThrow(CustomException.class);
//customController.updateCustomField(request.getId(), request);
}
我会看到测试失败并显示 java.lang.AssertionError
。我尝试了上面注释掉的 @Rule 注解。
英文:
I am writing a test case for the Rest Controller for an error condition and when I set expected exception to Exception.class the test runs with no errors. When I change the expected exception to CustomException.class, the test fails with assertion error
Here is my Controller class:
@RestController
public class CustomController<CustomFieldRequest customFieldRequest, CustomField customField> {
@PutMapping(path = "/{customFieldId}",
consumes = MediaType.APPLICATION_JSON_VALUE, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE})
public CustomField updateCustomField(@PathVariable String customFieldId, @RequestBody CustomFieldRequest customFieldRequest) throws CustomException {
customFieldRequest.setAuthorId(getUser());
customFieldRequest.setId(customFieldId);
return CustomService.updateCustomeField(customFieldRequest);
}
}
Here is my service class:
@Service
public class CustomServiceImpl implements CustomService {
@Override
public CustomField updateCustomeField(CustomFieldRequest customFieldRequest) throws CustomException {
}
}
Here is my custom exception class:
public class CustomException extends Exception {
public CustomException(final String message) {
super(message);
}
public CustomException(final String message, final Throwable cause) {
super(message, cause);
}
}
Here is my test class:
public class CustomControllerTest {
private CustomService customService = Mockito.mock(CustomService.class);
@Test(expected=Exception.class)
public void updateCustomFieldThrowsException() throws CustomException {
//exception.expect(Exception.class);
CustomFieldRequest request = getRequest();
when(customService.updateCustomeField(request)).thenThrow(Exception.class);
//customController.updateCustomField(request.getId(), request);
}
}
As mentioned above if I change my expected exception to CustomException.class in the test like below:
@Test(expected=CustomException.class)
public void updateCustomFieldThrowsException() throws CustomException {
//exception.expect(Exception.class);
CustomFieldRequest request = getRequest();
when(customService.updateCustomeField(request)).thenThrow(CustomException.class);
//customController.updateCustomField(request.getId(), request);
}
I see the test failing with java.lang.AssertionError
. I tried the @Rule annotation(shown commented above).
答案1
得分: 0
你需要为 CustomException
创建一个默认构造函数,或者在测试中像这样创建异常:new CustomException("")
。
英文:
You need a default constructor for CustomException
or create the exeception in your test like this: new CustomException("")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论