Junit 使用最终属性测试对象创建 (AssertJ)

Junit tests for Object Creation with final attributes (AssertJ)

我有一个class final 属性来报告错误信息。构造函数调用如代码段所示。

@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 仍然报告构造函数中缺少分支。

如果您在构造函数中缺少分支,那几乎肯定意味着其中有一些逻辑需要多个测试用例。如果不出意外,很明显存在一条带有 null 或未初始化异常的快乐路径,以及另一条发生问题并被报告的路径。

您可能至少需要两个测试用例才能仅涵盖对象构造。我可能会写更多的东西来涵盖所有可能的边缘和角落案例。