我在一个新的微服务,这是工作良好,所有的测试用例也工作良好。最近,spring-boot微服务通过spring-security和sso启用。微服务运行良好,但由于安全原因,测试用例失败了。我有一个类需要在spring-boot单元测试的运行时删除。我提供以下代码。
@Configuration
@Slf4j
public class CloudSecurityConfig {
private String host = "some host";
private String port = System.getenv("redis.port");
private String password = System.getenv("redis.password");
private String ssl = System.getenv("redis.ssl");
@Bean
JedisPoolConfig jedisPoolConfig(@Value("${redis.pool.maxactive}") int maxActive,
@Value("${redis.pool.maxidle}") int maxIdle, @Value("${redis.pool.minidle}") int minIdle) {
.... Other code
return jedisPoolConfig;
}
@Bean
JedisConnectionFactory jedisConnectionFactory() throws Exception {
.... Other code
return jedisConFactory;
}
@Bean
@Primary
public RedisTemplate<String, CtmUser> redisTemplate() throws Exception {
.... Other code
return redisTemplate;
}
}
我还尝试了javaspring引导测试:如何从测试上下文中排除java配置类。但它不起作用。我得到以下错误详细信息。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<?, ?>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
... 84 common frames omitted
以上是来自班级 CloudSecurityConfig
. 我想,如果我能够删除这个配置,一切都会正常工作。
上面的错误后面是下面的错误:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customAuthenticationProvider': Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<?, ?>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我试过以下几种方法。
//@EnableMongoRepositories
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration()
@SpringBootTest(classes = { ValidationApplication.class, TestMongoConfiguration.class })
//@ImportAutoConfiguration(classes = {ValidationApplication.class,TestMongoConfiguration.class})
//@EnableAutoConfiguration(exclude={ DataSourceAutoConfiguration.class, RedisAutoConfiguration.class,
// RedisRepositoriesAutoConfiguration.class, MongoAutoConfiguration.class })
//@EnableAutoConfiguration(exclude={ DataSourceAutoConfiguration.class, RedisAutoConfiguration.class,
// RedisRepositoriesAutoConfiguration.class, MongoAutoConfiguration.class, SecurityAutoConfiguration.class })
//@EnableAutoConfiguration(exclude={ CloudSecurityConfig.class })
//@TestPropertySource(inheritProperties = false, properties =
// "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration")
//@TestConfiguration()
//@ConditionalOnProperty(name = "test", havingValue="test")
@ActiveProfiles("test")
@ComponentScan(basePackages = {"com.a.b.c.d"}
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {SecurityCloudConfig.class})})
public class CheckTest {
@Autowired
private MyController controller;
@Test
void testAllBeforeEntry() {
ResponseEntity<?> re = controller.getValdiations();
boolean flag = re.getStatusCode().is2xxSuccessful();
assertEquals(true, flag);
}
}
1条答案
按热度按时间flseospp1#
与其忽略,不如重写configure类。您可以为testenv编写适当的testconfiguration类。此处添加了一个示例:
注意:不要忘记添加
@Primary
在测试豆和spring.main.allow-bean-definition-overriding=true
在test application.properties文件中。并添加@SpringBootTest(classes = TestSecurityConfig.class)
在主单元测试类中重写原始bean。你也可以提供mockbean而不是真的。