gpt4 book ai didi

java - 如何在 Spring 测试中连接依赖关系

转载 作者:行者123 更新时间:2023-12-02 11:31:26 25 4
gpt4 key购买 nike

我很难弄清楚如何在 Spring 中编写单元测试。

这是我正在尝试测试的方法:

@Service
public class ActionRequestHandler {

@Autowired
private Vertx vertx;

@Autowired
private InventoryService inventoryService;

@Autowired
private RequestSerializationWrapper requestWrapper;

@Autowired
private ProducerTemplate producer;

@EventListener
@Transactional
public void registerConsumer(ApplicationReadyEvent event) {
EventBus eb = vertx.eventBus();

eb.consumer("bos.admin.wui.action", (Message<String> msg) -> {
handleIncomingRequest(msg);
});
}
// ...
}

到目前为止,我已经尝试在测试类中创建配置,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class ActionRequestHandlerTest {

@Configuration
static class ContextConfiguration {

@Bean
@Primary
public Vertx vertx() {
return Mockito.mock(Vertx.class);
}

@Bean
@Primary
public InventoryService inventoryService() {
return Mockito.mock(InventoryService.class);
}

@Bean
@Primary
public RequestSerializationWrapper requestWrapper() {
return new RequestSerializationWrapper();
}
}

@Autowired
private Vertx vertx;

@Autowired
private InventoryService inventoryService;

@Autowired
private RequestSerializationWrapper requestWrapper;

@Autowired
private ActionRequestHandler systemUnderTest;

@Test
public void registerConsumer_shouldRegisterVertxEventBusConsumer() {
EventBus eventBusMock = Mockito.mock(EventBus.class);
Mockito.when(vertx.eventBus()).thenReturn(eventBusMock);

systemUnderTest.registerConsumer(null);

Mockito.verify(eventBusMock.consumer(Matchers.anyString()), Mockito.times(1));
}
}

但是,这似乎试图解决 InventoryService 内的每个依赖项,而不是模拟整个类。当我运行时,上面的配置给了我这个错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private admin.messaging.converters.XmlToEntityConverter admin.persistence.service.InventoryService.entityConverter; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [admin.messaging.converters.XmlToEntityConverter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我也尝试过使用建议的配置文件 here 。配置类看起来是一样的:

@Profile("test")
@Configuration
public class ActionRequestHandlerTestConfiguration {

@Bean
@Primary
public Vertx vertx() {
return Mockito.mock(Vertx.class);
}

@Bean
@Primary
public InventoryService inventoryService() {
return Mockito.mock(InventoryService.class);
}

@Bean
@Primary
public RequestSerializationWrapper requestWrapper() {
return new RequestSerializationWrapper();
}
}

测试的设置略有不同,使用以下注释:

@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ActionRequestHandler.class)
public class ActionRequestHandlerTest {
// ...
}

但这反而给了我一个错误,Vertx 无法连接:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private io.vertx.core.Vertx admin.messaging.request.ActionRequestHandler.vertx; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [io.vertx.core.Vertx] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我怎样才能让它发挥作用?我哪里出错了?

最佳答案

您不需要整个 spring 上下文来为 ActionRequestHandler 编写单元测试。您应该使用 MockitoJunitRunner 来代替并对依赖项进行模拟。

@RunWith(MockitoJunitRunner.class)
public class ActionRequestHandlerTest {

@Mock
private Vertx vertx;

@Mock
private InventoryService inventoryService;

@Mock
private RequestSerializationWrapper requestWrapper;

@Mock
private ProducerTemplate producer;

@InjectMocks
private ActionRequestHandler actionRequestHandler;

@Test
public void testRegisterConsumer() {
.... Your code to test ActionRequestHandler#registerConsumer will go here....
}
}

您可以阅读更多相关信息here .

关于java - 如何在 Spring 测试中连接依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49275354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com