spring-cloud-commons icon indicating copy to clipboard operation
spring-cloud-commons copied to clipboard

NullpointerException in integration tests after upgrade from Dalston.SR4 to Edgware

Open sanderino666 opened this issue 8 years ago • 13 comments

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 avatar Dec 20 '17 15:12 sanderino666

@sanderino666 is that class @RefreshScope?

spencergibb avatar Dec 20 '17 16:12 spencergibb

Fixed via 464624e3759526bbc227127d621822bc80d5dc6d

spencergibb avatar Dec 20 '17 16:12 spencergibb

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 avatar Dec 20 '17 16:12 spencergibb

@spencergibb I believe you fixed it before I could comment on it :-). Indeed, the injected bean has a @RefreshScope annotation.

sanderino666 avatar Dec 20 '17 17:12 sanderino666

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 avatar Dec 20 '17 18:12 spencergibb

@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.

spring-cloud-commons-issue-297.zip

sanderino666 avatar Dec 20 '17 21:12 sanderino666

Thanks @sanderino666, very helpful, but very odd to create a bean and return null.

spencergibb avatar Dec 20 '17 22:12 spencergibb

@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.

sanderino666 avatar Dec 21 '17 06:12 sanderino666

Right, but in that case there wouldn't be a bean definition with a null value

spencergibb avatar Dec 21 '17 06:12 spencergibb

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;
}

sanderino666 avatar Dec 21 '17 06:12 sanderino666

Yeah, I'd put that in a custom condition and never create the bean

spencergibb avatar Dec 21 '17 06:12 spencergibb

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!

sanderino666 avatar Dec 21 '17 06:12 sanderino666

FYI, Spring Framework returns NullBean instead of null since 5.0. See https://github.com/spring-projects/spring-framework/commit/b94302b5bde7475b9926dacb9fb69f3c8c894444

quaff avatar Nov 18 '26 02:11 quaff