Announcement Announcement Module
No announcement yet.
NullPointerException with TestContext.retrieveContextLoaderClass(TestContext .java:197 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException with TestContext.retrieveContextLoaderClass(TestContext .java:197

    I am trying to create some Integration tests and I am encountering an error when I try to run the test. I get an assert failure in the (121) indicating that the applicationcontext is null. However the test java file has the following line:

    @ContextConfiguration(locations = { "classpath:com/agg/dao/JpaIntegrationTests-context.xml" })
    The xml does exist but when I run the test Sysinternals procman never indicates that the xml file is being searched for. I am unclear as to what is really going on with this basic example.

  • #2
    File is being found if not you would have gotten FileNotFoundException.

    Where is the NPE coming from? Is it from a collaborator that was supposed to be injected?

    Are you running Spring's JUnit class runner? Your test should look like this for example:

    @RunWith( SpringJUnit4ClassRunner.class ) 
    @ContextConfiguration(locations = { "classpath:com/agg/dao/JpaIntegrationTests-context.xml" })
    public class TestContext {
        private SomeBeanInYourContext someBeanInYourContext;
        public void testSomething() {
    You might want to add logging so you can see what spring is doing. Just add a simple to your classpath.
    log4j.rootLogger=info, stdout
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
    Last edited by nicolas.loriente; Jun 1st, 2011, 06:11 PM.


    • #3
      Here is the complete test. I am still encountering the initializationError:

      package com.hwcs.veri.agg.dao;
      import static org.junit.Assert.assertEquals;
      import java.util.List;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.test.context.ContextConfiguration;
      import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
      import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
      import org.springframework.test.context.transaction.TransactionConfiguration;
      import org.springframework.transaction.annotation.Transactional;
      import com.hwcs.veri.jpa.License;
      @ContextConfiguration(locations = { "/JpaIntegrationTests-context.xml" })
      @TransactionConfiguration( transactionManager = "transactionManager", 
                                 defaultRollback    = true )
      public class JpaIntegrationTests 
       extends     AbstractTransactionalJUnit4SpringContextTests 
        protected LicenseDao licenseDao;
        public void getLicenses()
          List<License> licenses = this.licenseDao.getLicenses();   
          assertEquals( "Expecting 1 license from the query",
                        super.countRowsInTable( "product_schema.license" ),
                        licenses.size() );                  
      Is there some particular step that needs to be done to run this as a JUnit test inside Eclipse?
      Last edited by sldahlin; Jun 2nd, 2011, 11:03 AM.


      • #4
        Can you post the complete stack trace?


        • #5
          at org.springframework.test.context.TestContext.retri eveContextLoaderClass(
          at org.springframework.test.context.TestContext.<init >(
          at org.springframework.test.context.TestContextManage r.<init>(
          at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.createTestContextManager(SpringJUnit
          at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.<init>( 108)
          at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(
          at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(
          at java.lang.reflect.Constructor.newInstance(Construc
          at Runner(
          at rForClass(
          at org.junit.runners.model.RunnerBuilder.safeRunnerFo rClass(
          at sBuilder.runnerForClass(AllDefaultPossibilitiesBui
          at org.junit.runners.model.RunnerBuilder.safeRunnerFo rClass(
          at org.junit.internal.requests.ClassRequest.getRunner (
          at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.<init>(
          at org.eclipse.jdt.internal.junit4.runner.JUnit4TestC lassReference.<init>( :25)
          at org.eclipse.jdt.internal.junit4.runner.JUnit4TestL oader.createTest(
          at org.eclipse.jdt.internal.junit4.runner.JUnit4TestL oader.loadTests(
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRu
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(

          As I modified my last post to indicate is there some particular step that needs to be done to run this as a JUnit test inside Eclipse?