Search code examples
javaunit-testingjunitjacocoassertj

Junit tests for Object Creation with final attributes (AssertJ)


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.


Solution

  • 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.