sldahlin
sldahlin

Reputation: 685

Spring JUnit test getting NullPointerException with TestContext.retrieveContextLoaderClass(TestContext .java:197)

Using Helios, spring 3.0.5 (TestContext Framework) and JUnit 4.7. I am getting an initialization error indicating that it cannot find the ContextConfiguration. I ran ProcMon in the background and determined it is not apparently looking at all. I have tried the logical locations for the xml file to no avail. I am unclear of what I am doing incorrectly. Here is the 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?

Upvotes: 0

Views: 2285

Answers (2)

Sam Brannen
Sam Brannen

Reputation: 31247

First and foremost, set the log level for org.springframework.test.context to DEBUG. That should tell you everything that the Spring TestContext Framework (TCF) is doing.

Note that with your above configuration, the TCF will attempt to load your application context from classpath:/JpaIntegrationTests-context.xml (i.e., in the root of your classpath). So make sure that the JpaIntegrationTests-context.xml file in fact exists in the root of the classpath (e.g., /src/test/resources/JpaIntegrationTests-context.xml for a Maven project layout). For the Maven project layout, you need to make sure that /src/test/resources is configured as a source folder in your IDE.

If this doesn't help you solve your problem, post the DEBUG output from the log.

Regards,

Sam (author of the Spring TestContext Framework)

Upvotes: 1

lrkwz
lrkwz

Reputation: 6523

Quoting Java Project: Failed to load ApplicationContext

"From the Sping-Documentation: A plain path, for example "context.xml", will be treated as a classpath resource from the same package in which the test class is defined. A path starting with a slash is treated as a fully qualified classpath location, for example "/org/example/config.xml".

Upvotes: 0

Related Questions