Results 1 to 5 of 5

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

  1. #1

    Default 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 TestContext.java (121) indicating that the applicationcontext is null. However the test java file has the following line:

    Code:
    @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. #2

    Default

    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:

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

  3. #3

    Default

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

    Code:
    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;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/JpaIntegrationTests-context.xml" })
    @TransactionConfiguration( transactionManager = "transactionManager", 
                               defaultRollback    = true )
    @Transactional
    public class JpaIntegrationTests 
     extends     AbstractTransactionalJUnit4SpringContextTests 
    {
      @Autowired
      protected LicenseDao licenseDao;
      @Test
      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 at 11:03 AM.

  4. #4

    Default

    Can you post the complete stack trace?

  5. #5

    Default

    java.lang.NullPointerException
    at org.springframework.test.context.TestContext.retri eveContextLoaderClass(TestContext.java:197)
    at org.springframework.test.context.TestContext.<init >(TestContext.java:121)
    at org.springframework.test.context.TestContextManage r.<init>(TestContextManager.java:117)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.createTestContextManager(SpringJUnit 4ClassRunner.java:120)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.<init>(SpringJUnit4ClassRunner.java: 108)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
    at org.junit.internal.builders.AnnotatedBuilder.build Runner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runne rForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerFo rClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitie sBuilder.runnerForClass(AllDefaultPossibilitiesBui lder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerFo rClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner (ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.<init>(JUnit4TestReference.java:32)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestC lassReference.<init>(JUnit4TestClassReference.java :25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestL oader.createTest(JUnit4TestLoader.java:41)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestL oader.loadTests(JUnit4TestLoader.java:31)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)

    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?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •