主要问题:有没有一种方法可以在整个Spring上下文中用模拟对象替换bean,并将确切的bean注入测试中以验证方法调用?
我有一个Spring Boot应用程序,我正在尝试编写一些集成测试,其中我使用MockMvc
调用Rest API。
集成测试针对实际数据库和使用Testcontainer
和Localstack
的AWS资源运行。但是,为了测试与Keycloak
集成作为外部依赖关系的API,我决定模拟KeycloakService
并验证正确的参数是否传递给了该类的适当函数。
所有我的集成测试类都是AbstractSpringIntegrationTest
的子类:
@Transactional
@Testcontainers
@ActiveProfiles("it")
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@ContextConfiguration(initializers = PostgresITConfig.DockerPostgreDataSourceInitializer.class, classes = {AwsITConfig.class})
public class AbstractSpringIntegrationTest {
@Autowired
public MockMvc mockMvc;
@Autowired
public AmazonSQSAsync amazonSQS;
}
假设有以下这个子类:
class UserIntegrationTest extends AbstractSpringIntegrationTest {
private static final String USERS_BASE_URL = "/users";
@Autowired
private UserRepository userRepository;
@MockBean
private KeycloakService keycloakService;
@ParameterizedTest
@ValueSource(booleans = {true, false})
void changeUserStatus_shouldEnableOrDisableTheUser(boolean enabled) throws Exception {
// Some test setup here
ChangeUserStatusRequest request = new ChangeUserStatusRequest()
.setEnabled(enabled);
String responseString = mockMvc.perform(patch(USERS_BASE_URL + "/{id}/status", id)
.contentType(APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
// Some assertions here
Awaitility.await()
.atMost(10, SECONDS)
.untilAsserted(() -> verify(keycloakService, times(1)).changeUserStatus(email, enabled); // Fails to verify method call
}
}
这是调用 KeycloakService
函数的类,基于事件:
@Slf4j
@Component
public class UserEventSQSListener {
private final KeycloakService keycloakService;
public UserEventSQSListener(KeycloakService keycloakService) {
this.keycloakService = keycloakService;
}
@SqsListener(value = "${cloud.aws.sqs.user-status-changed-queue}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void handleUserStatusChangedEvent(UserStatusChangedEvent event) {
keycloakService.changeUserStatus(event.getEmail(), event.isEnabled());
}
}
无论何时运行测试,都会出现以下错误:
Wanted but not invoked:
keycloakService bean.changeUserStatus(
"rodolfo.kautzer@example.com",
true
);
Actually, there were zero interactions with this mock.
在调试代码后,我理解到由于上下文重新加载,在
UserIntegrationTest
中模拟的 bean 与注入到 UserEventSQSListener
类中的 bean 并不相同。因此,我尝试其他解决方案,如使用 Mockito.mock()
创建 mock 对象并将其作为 bean 返回,以及使用 @MockInBean,但它们也无法解决问题。 @TestConfiguration
public static class TestBeanConfig {
@Bean
@Primary
public KeycloakService keycloakService() {
KeycloakService keycloakService = Mockito.mock(KeycloakService.class);
return keycloakService;
}
}
更新1:
根据 @Maziz 的回答,并为了调试目的,我将代码更改如下:
@Component
public class UserEventSQSListener {
private final KeycloakService keycloakService;
public UserEventSQSListener(KeycloakService keycloakService) {
this.keycloakService = keycloakService;
}
public KeycloakService getKeycloakService() {
return keycloakService;
}
...
class UserIT extends AbstractSpringIntegrationTest {
...
@Autowired
private UserEventSQSListener userEventSQSListener;
@Autowired
private Map<String, UserEventSQSListener> beans;
private KeycloakService keycloakService;
@BeforeEach
void setup() {
...
keycloakService = mock(KeycloakService.class);
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void changeUserStatus_shouldEnableOrDisableTheUser(boolean enabled) throws Exception {
// Some test setup here
ChangeUserStatusRequest request = new ChangeUserStatusRequest()
.setEnabled(enabled);
String responseString = mockMvc.perform(patch(USERS_BASE_URL + "/{id}/status", id)
.contentType(APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
// Some assertions here
ReflectionTestUtils.setField(userEventSQSListener, "keycloakService", keycloakService);
assertThat(userEventSQSListener.getKeycloakService()).isEqualTo(keycloakService);
await().atMost(10, SECONDS)
.untilAsserted(() -> verify(keycloakService).changeUserStatus(anyString(), anyBoolean())); // Fails to verify method call
}
正如你所看到的,mock 已经被适当地替换在 UserEventSQSListener
类中:
然而,我遇到了以下错误:
Wanted but not invoked:
keycloakService.changeUserStatus(
<any string>,
<any boolean>
);
Actually, there were zero interactions with this mock.
KeycloakService
bean,则更改将应用于 Keycloak 端。 - Reza Ebrahimpour@TestConfiguration
对您不起作用吗? - EugeneUserEventSQSListener
中,因此更改将应用于Keycloak。在某些情况下,UserEventSQSListener
将再次初始化,并使用构造函数将新的模拟对象注入其中,但是测试类中的引用不会更改。因此,另一个模拟对象将被调用,并且验证将发生在另一个模拟对象上,并且将抛出错误。@ParthManaktala - Reza Ebrahimpour