Created
January 26, 2024 15:26
-
-
Save dariahervieux/6f5399c30e4ab41d3043eaa9cf303afa to your computer and use it in GitHub Desktop.
Spring Boot - SpringBootTest configuration - mock a bean before application context is ready
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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