英文:
Junit tests for Object Creation with final attributes (AssertJ)
问题
以下是已翻译的部分:
我有一个带有最终属性的类,用于报告错误信息。构造函数调用如片段所示。
@Test
public void test_With_Message() throws ParseException {
final Exception e = new Exception("");
final HttpStatus status = HttpStatus.BAD_GATEWAY;
final String path = "abc/def/v1";
final String message = "Exception";
Info info = new Info(e, status, path, message);
如何使用 AssertJ 单元测试测试对象创建?
我已尝试 assertThat(info).hasNoNullFieldsOrProperties();
assertThat(info).isNotNull();
但 JaCoco 仍然报告构造函数上存在缺失的分支。
英文:
I have a class with final attributes to report on error information. The constructor call is as shown in the snippet.
@Test
public void test_With_Message() throws ParseException {
final Exception e = new Exception("");
final HttpStatus status = HttpStatus.BAD_GATEWAY;
final String path = "abc/def/v1";
final String message = "Exception";
Info info = new Info(e, status, path, message);
How do I test Object creation with AssertJ unit tests?
I have tried assertThat(info).hasNoNullFieldsOrProperties();
assertThat(info).isNotNull();
JaCoco still reports missing branches on the constructor.
答案1
得分: 2
如果在构造函数中缺少分支,那几乎肯定意味着其中有一些逻辑需要多个测试案例。如果没有其他情况,似乎很明显存在一种情况,即存在一个空值或未初始化异常的正常路径,以及另一种情况,其中出现了问题并且正在报告。
仅仅为了涵盖对象构造,您可能需要至少两个测试案例。我可能会写更多以涵盖所有可能的边缘情况和角落情况。
英文:
If you are missing branches in the constructor, that almost certainly means there's some logic in there which will require multiple test cases. If nothing else it seems clear that there is a happy path with a null or uninitialized Exception and another path where a problem has occurred and is being reported.
You probably need at least two test cases to cover object construction alone. I'd likely write a lot more than that to cover all the edge and corner cases I could.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论