在使用JUnit进行测试时遇到自定义异常的失败情况。

huangapple go评论72阅读模式
英文:

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&lt;CustomFieldRequest customFieldRequest, CustomField customField&gt; {
    @PutMapping(path = &quot;/{customFieldId}&quot;,
            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(&quot;&quot;)

huangapple
  • 本文由 发表于 2020年8月22日 19:46:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63535818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定