java.lang.IllegalArgumentException: contextLoader can not be null.

Spring JIRA | Chris Beams | 9 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    Running the following test results in an error message that is not particularly helpful: package com.foo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) public class MyTests { @Test public void foo() { // ... } } The resulting stack trace: java.lang.IllegalArgumentException: contextLoader can not be null. at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:176) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:216) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:58) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:205) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:109) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:156) at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) As it turns out, the test is simply missing a @ContextConfiguration annotation. The exception's message could be improved to suggest this as a possible resolution to the user. Here's a patch that adds this bit of user-friendliness: --- ./spring/tiger/mock/org/springframework/test/context/TestContext.java 2007-09-24 09:57:41.000000000 -0700 +++ ./JdoTxnSimple/src/main/java/org/springframework/test/context/TestContext.java 2007-09-29 16:10:53.000000000 -0700 @@ -177,7 +173,8 @@ * context */ protected ApplicationContext buildApplicationContext() throws Exception { - Assert.notNull(getContextLoader(), "contextLoader can not be null."); + Assert.notNull(getContextLoader(), "contextLoader can not be null. " + + "Did you forget to add a @ContextConfiguration annotation on your test class?"); Assert.notNull(getLocations(), "locations can not be null."); return getContextLoader().loadContext(getLocations()); } Thanks!

    Spring JIRA | 9 years ago | Chris Beams
    java.lang.IllegalArgumentException: contextLoader can not be null.
  2. 0

    Running the following test results in an error message that is not particularly helpful: package com.foo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) public class MyTests { @Test public void foo() { // ... } } The resulting stack trace: java.lang.IllegalArgumentException: contextLoader can not be null. at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:176) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:216) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:58) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:205) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:109) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:156) at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) As it turns out, the test is simply missing a @ContextConfiguration annotation. The exception's message could be improved to suggest this as a possible resolution to the user. Here's a patch that adds this bit of user-friendliness: --- ./spring/tiger/mock/org/springframework/test/context/TestContext.java 2007-09-24 09:57:41.000000000 -0700 +++ ./JdoTxnSimple/src/main/java/org/springframework/test/context/TestContext.java 2007-09-29 16:10:53.000000000 -0700 @@ -177,7 +173,8 @@ * context */ protected ApplicationContext buildApplicationContext() throws Exception { - Assert.notNull(getContextLoader(), "contextLoader can not be null."); + Assert.notNull(getContextLoader(), "contextLoader can not be null. " + + "Did you forget to add a @ContextConfiguration annotation on your test class?"); Assert.notNull(getLocations(), "locations can not be null."); return getContextLoader().loadContext(getLocations()); } Thanks!

    Spring JIRA | 9 years ago | Chris Beams
    java.lang.IllegalArgumentException: contextLoader can not be null.
  3. 0

    Bean object must not be null in Spring MVC

    Stack Overflow | 3 years ago | Sivanagaiah
    java.lang.IllegalArgumentException: Bean object must not be null
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Spring file upload by ajax

    Stack Overflow | 3 years ago | kris14an
    java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
  6. 0

    Spring websocket encounter "Ignoring message, no principal info available" error

    Stack Overflow | 3 years ago | zizibj
    java.lang.IllegalArgumentException: No 'javax.websocket.server.ServerContainer' ServletContext attribute. Are you running in a Servlet container that supports JSR-356?
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.lang.IllegalArgumentException

    contextLoader can not be null.

    at org.springframework.util.Assert.notNull()
  2. Spring Core
    Assert.notNull
    1. org.springframework.util.Assert.notNull(Assert.java:112)
    1 frame
  3. Spring TestContext
    SpringJUnit4ClassRunner.invokeTestMethod
    1. org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:176)
    2. org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:216)
    3. org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:58)
    4. org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:205)
    5. org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:109)
    6. org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:156)
    6 frames
  4. JUnit
    JUnit4ClassRunner.run
    1. org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    2. org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    3. org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    4. org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    5. org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    5 frames
  5. JUnit4 Runner
    JUnit4TestReference.run
    1. org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    1 frame
  6. JUnit3 Runner
    RemoteTestRunner.main
    1. org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    2. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    3. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    4. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    5. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    5 frames