我有一个spring应用程序配置类,其中实例bean.
应用类:
@Configuration
@EnableAspectJAutoProxy
@EnableSpringDataWebSupport
@EnableTransactionManagement
@ComponentScan(basePackageClasses = Application.class)
@PropertySource(value = {"classpath:foo.properties"})
@EnableJpaRepositories(basePackageClasses = Application.class)
@EnableJpaAuditing
public class Application {
@Inject
private Environment env;
@Bean
JndiTemplate jndiTemplate() {
return new JndiTemplate();
}
@Bean
public DataSource dataSource() {
DataSource dataSource = getDataSource();
if (dataSource == null) {
dataSource = new BasicDataSource();
((BasicDataSource) dataSource).setUsername(env.getProperty("jdbc.user"));
((BasicDataSource) dataSource).setPassword(env.getProperty("jdbc.password""));
((BasicDataSource) dataSource).setDriverClassName(env.getProperty("jdbc.driverClassName"));
((BasicDataSource) dataSource).setUrl(env.getProperty("jdbc.url"));
}
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
EntityManagerFactory factory = entityManagerFactory().getObject();
return new JpaTransactionManager(factory);
}
//....
}
MvcConfiguration类:
@Configuration
@ComponentScan(basePackageClasses = Application.class,includeFilters = @Filter({Controller.class,Component.class}),useDefaultFilters = true)
class MvcConfiguration extends WebMvcConfigurationSupport {
private static final String MESSAGES = "classpath:/i18n";
private static final String VIEW_PREFIX = "/WEB-INF/views/";
@Inject
private Environment env;
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping();
requestMappingHandlerMapping.setUseSuffixPatternMatch(false);
requestMappingHandlerMapping.setUseTrailingSlashMatch(true);
return requestMappingHandlerMapping;
}
@Bean(name = "messageSource")
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename(MESSAGES);
messageSource.setCacheSeconds(5);
return messageSource;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/").addResourceLocations("/static/**");
}
@Bean
public MultipartResolver filterMultipartResolver(){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize(Long.parseLong(env.getProperty("multipart.max.size")));
return resolver;
}
//....
}
和SecurityConfiguration类:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
//....
@Override
protected void configure(HttpSecurity http) throws Exception {
//logout por POST con el valor de token csrf
http.authorizeRequests()
.antMatchers("/static/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.failureUrl("/login?error=1")
.loginProcessingUrl("/authenticate")
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/signin")
.permitAll();
}
}
我如何用JUnit测试它们?如何测试bean是在spring上下文中创建的?
最佳答案
我相信这只能通过集成测试来实现.
单元测试的目的不是检查整个Spring Context是否成功创建.
您可以使用模拟等测试单元测试的每个配置方法,以检查它们是否正常,但整个Spring Context事物是一个集成测试.
我通过执行Spring Docs所谓的“Spring Unit Test”(对我而言更像是控制器视图的集成测试)来进行此配置测试
我们的想法是,如果您可以为Controller集成测试运行Spring Context,那么您的配置就可以了.
关于如何进行这种测试的春季文档有一整章.
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html