tags:

views:

1437

answers:

1

I am getting the error "java.lang.IllegalStateExcepton: Failed to load Application Context" when I am trying to run my unit test from within Eclipse.

The Unit Test itself seems very simple:

package com.mycompany.interactive.cs.isales.ispr.ws.productupgrade;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class ProductUpgradeTest {

    @Test
    public void getProductUpgrade () throws Exception
    {
     //System.out.println(PrintClasspath.getClasspathAsString());
     Assert.assertTrue(true);
    }
}

But no matter what I seem to do with the @ContextConfiguration I still get the same error.

The applicationContext.xml file resides within a folder /etc/ws, but even if I put a copy in the same folder it is still giving me errors.

I am very new to Java, Spring, Eclipse and Ant, and really don't know where this is going wrong.

+5  A: 

The problem is that you use an absolute path in your @ContextConfiguration annotation. You now specify that your applicationContext.xml is located in your root folder of your system (where it probably isn't, since you say it is at /etc/ws). I see two possible solutions:

  1. Use @ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
  2. Move your applicationContext.xml file into your project (for example in your WEB-INF folder) and use @ContextConfiguration(locations={"classpath:applicationContext.xml"})

In case 2, you have to make sure that the directory in which you place your applicationContext.xml is located in your classpath. In Eclipse this can be configured in your project properties.

Martin Sturm