java.lang.IllegalArgumentException

If you like a tip written by other Samebug users, mark is as helpful! Marks help our algorithm provide you better solutions and also help other users.
tip

If you're unit testing, you should instantiate only the service and mock the dependencies. If you're testing integration, you should add all the mocking to the configurations file.

You have a different solution? A short tip here would help you and many other users who saw this issue last week.

  • 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!
    via by Chris Beams,
  • 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!
    via by Chris Beams,
  • Spring Boot test class wont inject beans
    via Stack Overflow by rayman
    ,
  • Spring file upload by ajax
    via Stack Overflow by kris14an
    ,
  • mybatis + spring failed to start
    via by Hain Bach,
    • 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)

    Users with the same issue

    osvzs
    2 times, last one,
    odd
    2 times, last one,
    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    mauritius
    1 times, last one,
    189 more bugmates