Skip to content

Instantly share code, notes, and snippets.

@dariahervieux
Created January 26, 2024 15:26
Show Gist options
  • Save dariahervieux/6f5399c30e4ab41d3043eaa9cf303afa to your computer and use it in GitHub Desktop.
Save dariahervieux/6f5399c30e4ab41d3043eaa9cf303afa to your computer and use it in GitHub Desktop.
Spring Boot - SpringBootTest configuration - mock a bean before application context is ready
@SpringBootTest
// Import of the inner @TestConfiguration is important here
// see Note in https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing.spring-boot-applications.excluding-configuration
@Import(ExempleITest.MockConfig.class })
@ActiveProfiles("itest")
class ExempleITest {
// ...
@TestConfiguration
public static class MockConfig {
@Bean
@Primary
public TestBean testBeanMock() {
TestBean mock = Mockito.mock(TestBean.class);
Mockito.doReturn("Test 1")
.when(mock)
.getValue("1");
Mockito.doReturn("Test 2")
.when(mock)
.getValue("2");
return mock;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment