NullpointerException in integration tests after upgrade from Dalston.SR4 to Edgware
After upgrading from Dalston.SR4 to Edgware our integration tests are failing. The log:
Application startup failed
java.lang.NullPointerException: null
at org.springframework.cloud.context.scope.refresh.RefreshScope.start(RefreshScope.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Line 121 of the RefreshScope class states: this.context.getBean(name).getClass();
In our case this.context.getBean(name) returns null because this bean is optional so getClass() results in a NullPointerException.
It seems getClass() is added in this commit: https://github.com/spring-cloud/spring-cloud-commons/commit/c28ca5de91408e6c64a2c9a3e1977b8f104940ac
In our case we have a java config class where we autowire an optional class:
@Configuration
public class MyCustomConfig {
...
@Autowired(required = false)
@Qualifier("myCustomBean")
private MyCustomBean myCustomBean;
}
@sanderino666 is that class @RefreshScope?
Fixed via 464624e3759526bbc227127d621822bc80d5dc6d
Reopening for a test. The fix is easy, coming up with the test is another thing. @sanderino666 can you provide a snippet of how your bean is defined?
@dsyer any ideas?
@spencergibb I believe you fixed it before I could comment on it :-). Indeed, the injected bean has a @RefreshScope annotation.
Ha, by merging to master it closed it again. @sanderino666 could you provide a sample of what that looks like? I'm unable to craft a test that recreates it, so we can prevent a regression in the future.
@spencergibb I created an integration test which proves it all. I hope this helps. If you run the tests it should pass with Dalston.SR4 in the pom.xml and it should fail with Edgware.RELEASE.
Thanks @sanderino666, very helpful, but very odd to create a bean and return null.
@spencergibb I know :). In this case it is a simple example but in our codebase there are some beans that are only created when there is a specific property present in the application.properties for example.
Right, but in that case there wouldn't be a bean definition with a null value
Here's the magic ;-)
@Bean
@RefreshScope
public ILogResource logResource(@Value("${logging.file:}") String logfile) {
if (logfile == null) {
LOG.info("No log resource file configured");
return null;
}
LogFileResource logFileResource = new LogFileResource(logfile);
if (!logFileResource.exists()) {
LOG.warn("Configured log resource file does not exist");
return null;
}
return logFileResource;
}
Yeah, I'd put that in a custom condition and never create the bean
I agree. Just going through our codebase to see if this is the only place where we return a null bean so we can fix it. Thanks for your help!
FYI, Spring Framework returns NullBean instead of null since 5.0.
See https://github.com/spring-projects/spring-framework/commit/b94302b5bde7475b9926dacb9fb69f3c8c894444