我正在尝试注入一个WebTestClient以便使用Spring Webflux实现一些集成测试。
我必须加载使用此配置运行的redis嵌入式软件:
@Configuration
@Profile("test")
public class EmbeddedRedisConfiguration {
private RedisServer redisServer;
public EmbeddedRedisConfiguration(@Value("${spring.redis.port}") int redisPort) {
this.redisServer = new RedisServer(redisPort);
}
@PostConstruct
public void postConstruct() {
redisServer.start();
}
@PreDestroy
public void preDestroy() {
redisServer.stop();
}
@Bean
@Primary
public ReactiveRedisOperations<String, Foo> redisOperations(@Qualifier("redissonConnectionFactory") ReactiveRedisConnectionFactory factory) {
Jackson2JsonRedisSerializer<Foo> serializer = new Jackson2JsonRedisSerializer<>(Foo.class);
RedisSerializationContext.RedisSerializationContextBuilder<String, Foo> builder = RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
RedisSerializationContext<String, Foo> context = builder.value(serializer).build();
return new ReactiveRedisTemplate<>(factory, context);
}
}
运行此类
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test")
public class FooControllerIT {
@Autowired
WebTestClient webTestClient;
@Test
public void testSomething() {
// do something
}
}
我收到此错误:
No qualifying bean of type 'org.springframework.test.web.reactive.server.WebTestClient' available: expected at least 1 bean which qualifies as autowire candidate
如果我更改此行
@SpringBootTest
此行
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
WebTestClient可用于自动装配,但是应用程序在我的测试配置文件配置之前启动,无法连接到嵌入式Redis:
Factory method 'redisson' threw exception; nested exception is org.redisson.client.RedisConnectionException: Unable to connect to Redis server: localhost/127.0.0.1:6379
有没有一种方法可以注入WebTestClient而不启动web环境,或者有一种方法可以在其他事情之前加载我的嵌入式redis配置?
修改
问题似乎是RedissonAutoConfiguration
已加载