Expand my Community achievements bar.

About writing mockito junit test case 1 of 2 branches missed junit if condition

Avatar

Level 4

Hello,

 

In the report it says 1 of 2 branches missed for the below conditions checks in the code

if (Objects.nonNull(xssFilter))
if (Objects.nonNull(httpResponse))

Please let me know how could make it work 100% coverage for the above conditions

 

Thanks,

Srinivas

4 Replies

Avatar

Employee Advisor

Hi @Srinivas_Opti ,

 

To have the perfect testsuite, you need to write test cases for the negative scenarios as well.

Considering your code for instance:
if (Objects.nonNull(xssFilter))
if (Objects.nonNull(httpResponse))

You might have written test case if this conditions hold true, but you also need to write test cases for false conditions:
Here, if xssFilter is null and/or httpResponse is null what should happen ?

Hope this helps!

Best Regards,

Milind Bachani

Avatar

Level 4

@milind_bachani 

if I set  @Mock
XSSFilter xssFilter;

 

Then Objects.nonNull(xssFilter)  is never null

 

If set xssFilter=null is not getting failing the if check

 

How to resolve the issue

 

Thanks

Avatar

Employee Advisor

@Srinivas_Opti if you are using AemContext(), you should be able to use:

@Mock
XSSFilter xss;

context.registerService(XSSFilter.class, xss);