I am using spring boot 2.3.1.RELEASE
along with java version 14
.
When I run test cases I face below exception and if I change java version to 8
everything works fine.
java.lang.IllegalStateException: Failed to load ApplicationContext
.
.
.
.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationManagerBuilder' defined in class path resource [org/springframework/security/config/annotation/authentication/configuration/AuthenticationConfiguration.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/init/DatabasePopulator
This is my security configuration
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.csrf().disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and().httpBasic();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Application properties
spring:
security:
user:
name: admin
password: $2a$10$04i1Es7w1ZfQUhnX5Q0S7eTcw/SMsRdi9fU8Df06tS.azJ8ceOwoi #123456
My test class
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(EmployeeController.class)
@Import({SecurityConfig.class})
public class EmployeeIT {
@Autowired
protected MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private EmployeeDAO employeeDao;
@Test
@WithAnonymousUser
public void getEmployees_ValidRequest_ShouldReturnReportFileWith200OkResponse() throws Exception {
mockMvc.perform(get("/employees/").contentType("application/json"))
.andExpect(status().isOk());
}
@TestConfiguration
public static class ReportControllerTestConfiguration{
@Bean
public EmployeeDAO employeeDao(){
return new EmployeeDAO();
}
}
}
Comment From: snicoll
Thanks for the report. I can't see why changing the Java version would lead to NoClassDefFoundError
. Rather than code in text, can you please share a small sample that we can run? A zip attached to this issue or a link to a GitHub repository.
Comment From: hossein-baghshahi
Thanks for the report. I can't see why changing the Java version would lead to
NoClassDefFoundError
. Rather than code in text, can you please share a small sample that we can run? A zip attached to this issue or a link to a GitHub repository.
Yes here is the project: springboot-basicauth.zip
Comment From: snicoll
@hossein-baghshahi thanks but that project does not reproduce the issue you've described. It misses a dependency on the HTTP client so the setup in your test fails:
java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
at com.howtodoinjava.rest.SpringBootDemoApplicationTests.getClientHttpRequestFactory(SpringBootDemoApplicationTests.java:39)
at com.howtodoinjava.rest.SpringBootDemoApplicationTests.setUp(SpringBootDemoApplicationTests.java:34)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
When I add that, the test fails with 401
. Also, using JUnit 4 and removing the vintage engine does not run your test on the command line. Can you please update your project so that it generates the exception you've shared in your original description?
Comment From: hossein-baghshahi
sing JUnit 4 and removing the vint
I update project here
There is just one test case EmployeeIT
class that produces the exception that I mentioned before.
Comment From: snicoll
@hossein-baghshahi thanks for the update. The test still doesn't run on the command line because you've added an IT
suffix that's not picked up by Surefire by default. I've renamed the test to EmployeeTest
and this project works for me with both Java 1.8
(with a change to java.version
) and 14
(untouched).
I am going to close this issue now as me failing to reproduce is enough evidence that there is a setup problem on your end. I would have advised you to clean your Maven cache as an artifact could have been corrupted while being downloaded but that doesn't add up with the fact it works with one Java version and not another.
If you need more support, please ask on StackOverflow or come chat with the community on Gitter.