Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Web Service Unit Tests: java.lang.IllegalStateExcepton: Failed to load Application Context

Tags:

java

junit

spring

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.

like image 882
Xetius Avatar asked Jan 29 '26 02:01

Xetius


1 Answers

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.

like image 173
Martin Sturm Avatar answered Jan 31 '26 16:01

Martin Sturm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!