Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE when @SpringBootTest(webEnvironment = RANDOM_PORT) and @WebAppConfiguration are used together #6795

Closed
wilkinsona opened this issue Aug 31, 2016 · 8 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@wilkinsona
Copy link
Member

The context fails to load due to a NullPointerException:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) ~[spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) ~[spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) ~[spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) ~[spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
Caused by: java.lang.NullPointerException: null
    at org.springframework.test.context.web.socket.MockServerContainerContextCustomizer.customizeContext(MockServerContainerContextCustomizer.java:38) ~[spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.boot.test.context.SpringBootContextLoader$ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:270) ~[classes/:na]
    at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:639) ~[classes/:na]
    at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:349) ~[classes/:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) ~[classes/:na]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111) ~[classes/:na]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.3.BUILD-SNAPSHOT.jar:4.3.3.BUILD-SNAPSHOT]
    ... 25 common frames omitted

There's no need to use @WebAppConfiguration when you're using @SpringBootTest and, in this case at least, it's harmful. Perhaps we should fail fast, telling the user to remove @WebAppConfiguration?

@wilkinsona wilkinsona added the type: bug A general bug label Aug 31, 2016
@wilkinsona wilkinsona added this to the 1.4.1 milestone Aug 31, 2016
@wilkinsona
Copy link
Member Author

Another option could be to skip over MockServerContainerContextCustomizer when we're getting the initializers. We could log a warning or error message pointing out the misconfiguration.

@artembilan
Copy link
Member

SO question on the matter: http://stackoverflow.com/questions/39237328/nullpointerexception-when-using-springboottest-annotation-with-webenvironment.
I'm inclined to agree with @sbrannen (and with @wilkinsona as well 😉) that it doesn't not make sense to combine webEnvironment with real Embedded Servlet container and @WebAppConfiguration to mock Servlet context.
👍 to "fail fast" and suggest to remove @WebAppConfiguration.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Aug 31, 2016
@sbrannen
Copy link
Member

Perhaps we should fail fast, telling the user to remove @WebAppConfiguration?

That's the route I'd opt for in order to avoid any further unexpected side effects.

@wilkinsona wilkinsona removed the for: team-attention An issue we'd like other members of the team to review label Aug 31, 2016
@wilkinsona
Copy link
Member Author

We're going to fail fast

@wilkinsona wilkinsona self-assigned this Sep 14, 2016
@wilkinsona
Copy link
Member Author

We overlooked #6371 when we decided to fail fast

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Sep 14, 2016
@wilkinsona wilkinsona reopened this Sep 14, 2016
@sbrannen
Copy link
Member

We overlooked #6371 when we decided to fail fast

So the saga continues....

What is your current plan of action?

@wilkinsona wilkinsona removed the for: team-attention An issue we'd like other members of the team to review label Sep 15, 2016
@wilkinsona
Copy link
Member Author

What is your current plan of action?

We're going to fail fast when the two are used in combination and @SpringBootTest is configured with a non-mock web environment.

@sbrannen
Copy link
Member

Sounds good, @wilkinsona! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants