Junit测试用于具有final属性的对象创建(AssertJ)

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

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.

huangapple
  • 本文由 发表于 2020年7月28日 20:45:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63134441.html
匿名

发表评论

匿名网友

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

确定