Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

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


Level 4



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




4 Replies


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


Level 4


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




Employee Advisor

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

XSSFilter xss;

context.registerService(XSSFilter.class, xss);