I have a class:
classA
{
private Boolean isEnable;
private Config config;
@Autowired
private classC objC;
public classA(Config config)
{
this.config = config;
isEnable = config.getEnablingStatus();
}
public classB fun()
{
// Do something!
// Return an object of classB!
}
}
I want to create an object of this class in a test class, is this the right way to do it?
TestClassForClassA
{
@Mock
private classC objC;
@Mock
private Config config;
@InjectMocks
classA objA;
@Before
public void init()
{
initMocks(this);
}
@Test
public void testFunWhenIsEnableIsTrue()
{
doReturn(true).when(config).getEnablingStatus();
objA = new classA(config);
// The reason for above 2 lines is, I want the isEnable to be set to true in objA.
// Use the objA here for the futher logic!
}
@Test
public void testFunWhenIsEnableIsFalse()
{
doReturn(false).when(config).getEnablingStatus();
objA = new classA(config);
// The reason for above 2 lines is, I want the isEnable to be set to false in objA.
// Use the objA here for the futher logic!
}
}
My concern is, will instantiating objects of classA in the above shown way - i.e. inside different test methods, inject the objects declared as mocks (using @Mock), into it?
If not what is the right way to do it?