In my case, nothing in my PostConstruct crashes my other tests, only my Mockito.verify, so I decided to keep the injected mock class that the PostConstruct uses and then in my test, re-mock it and re-inject it using Mockito and ReflectionTestUtils. This avoided issues with the bean creation and allowed me to verify only the freshly mocked class:
Class Under Test:
@Component
public class ClassUnderTest
{
@Autowired
private MockedClass nameOfActualBeanInClassUnderTest;
@PostConstruct
private void postConstructMethod()
{
Object param1 = new Object();
Object param2 = new Object();
this.nameOfActualBeanInClassUnderTest.mockedClassFunctionBeingHit(param1, param2);
}
}
Tests Class:
import static org.mockito.Mockito.*;
import org.springframework.test.util.ReflectionTestUtils;
public class Tests
{
// Class Under Test
@Autowired
private ClassUnderTest classUnderTest;
// Mocked Class
@MockBean
private MockedClass mockedClass;
@Test
public void actualTestThatAvoidsPostConstruct()
{
// ============== Ignore PostConstruct Errors ==============
// Note: You will probably want to capture the current mocked class
// to put it back in the class under test so that other tests won't fail
MockedClass savedMockedClass =
(MockedClass)ReflectionTestUtils.getField(this.classUnderTest,
"nameOfActualBeanInClassUnderTest");
this.mockedClass = mock(MockedClass.class);
ReflectionTestUtils.setField(this.classUnderTest,
"nameOfActualBeanInClassUnderTest",
this.mockedClass);
// ============== Setup Test ==============
Object response = new Object();
// Set return value when mockedClass' function is hit
// Note: Only need to pass params if your function actually has them
when(this.mockedClass.mockedClassFunctionBeingHit(param1, param2))
.thenReturn(response);
// ============== Test ==============
this.classUnderTest.testFunction();
// ============== Verify ==============
// Note: Only need to pass params if your function actually has them
verify(this.mockedClass).mockedClassFunctionBeingHit(param1, param2);
// ============== Reset Mocked Class ==============
ReflectionTestUtils.setField(this.classUnderTest,
"nameOfActualBeanInClassUnderTest",
savedMockedClass);
}
}
@scheduled
code shouldn't be running on startup based on the code here. Is this method being called somewhere else? – InsertPostConstruct
that is triggering the method. Realistically it doesn't seem that it is appropriate for a post construct call. Maybe some sort of event listener to trigger on start up that could be put in a profile? – Puton