I'm trying to test a webapp using Junit. The class I want to test CalculMoisUtils load a bean from applicationcontext ( spring) using this code:
Properties prop = new Properties();
InputStream input = null;
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
String file = (String) ctx.getBean("CalendarProp");
input = new FileInputStream(file);
prop.load(input);
Here is the test class :
package test.webapp.utils;
import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.logica.planchaweb.utils.CalculMoisUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/applicationcontext.xml","file:src/applicationcontext-security.xml" })
public class CalculMoisUtilsTest {
ClassPathXmlApplicationContext ctx;
@Before
public void setUp(){
ctx = new ClassPathXmlApplicationContext("file:src/applicationcontext.xml");
}
@Test
public void test() {
Properties prop = new Properties();
InputStream input = null;
try {
String file = (String) ctx.getBean("CalendarProp");
input = new FileInputStream(file);
prop.load(input);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}finally{
try{
input.close();
}catch(Exception e){}
}
CalculMoisUtils calculMoisUtils = new CalculMoisUtils();
}
}
The problem is that the class I want to test doesn't have the applicationcontext even if I load it on the test class, where the same code works.
Thanks for your help
Romain