About writing mockito junit test case 1 of 2 branches missed junit if condition | Community
Skip to main content
Level 3
April 27, 2023

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

  • April 27, 2023
  • 4 replies
  • 3016 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

milind_bachani
Adobe Employee
Adobe Employee
April 27, 2023

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 3
April 28, 2023

@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

milind_bachani
Adobe Employee
Adobe Employee
April 28, 2023

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

@Mock XSSFilter xss; context.registerService(XSSFilter.class, xss);
milind_bachani
Adobe Employee
Adobe Employee
April 28, 2023

@srinivas_opti Can you post the code that has issues ?