replace injected mock, with UTest local var mock

Viewed 15

My Unit-test class file has many tests that uses mock that injected using:

   @InjectMocks
    private Class1 c1 = new Class1();
    @Mock
    private Class2 c2;

(c2 is an attribute of c1)
I would like to test the case when c2 is null.

1 Answers

if c2 is attribute of c1 then you have below options

First use constructor based injection to inject c2 thats how u can customize your class object initialisation and pass c2 as null to test the behaviour.

Second you can use ReflectionTestUtils class method setField to set c2 as null for your specific test.

In your test just initialise new object of c1 and use that for your behaviour test.

Related