I have a spring application whith configuration classes where instance the beans.
Aplication class:
@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 class:
@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; } //.... }
And SecurityConfiguration class:
@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(); } }
How I can test them with JUnit? How to test the beans are created in the spring context?
Basically, you: use a specific configuration to @EnableConfigurationProperties and @EnableAutoConfiguration , listing all the @ConfigurationProperties files you want to load. in the test class, you load this configuration file of tests, with an initializer class defined by Spring to load application. yml file.
To test whether Spring MVC controllers are working as expected, use the @WebMvcTest annotation. To test that Spring WebFlux controllers are working as expected, you can use the @WebFluxTest annotation. You can use the @DataJpaTest annotation to test JPA applications.
In a word - "don't", that way lays madness.
What you really want is higher level tests that make use of your Spring configuration but are still focused on behaviour not implementation.
For example, looking at your security configuration - you don't really care that the configure
method is called, or what it does, what you want to test for is:
Using Spring for DI and security is merely how those things are implemented whereas your tests should be focused on the fact those things actually work.
I believe this can only be achieved with an Integration Test.
The purpose of Unit Tests are not to check if the whole Spring Context is being created successfully.
You can test each configuration method with a Unit Test by using mocks, etc to check if they are OK, but the whole Spring Context thing is an Integration test.
I use to do this Configuration Test by doing what Spring Docs calls "Spring Unit Test" (that for me is more like a Integration Test of the Controllers + Views)
The idea is that, if you can get a Spring Context running for a Controller integration Test, then your Configurations are OK.
There is a whole chapter in spring docs on how to do that kind of test. http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With