我尝试使用Junit测试网络应用程序。 我想测试CalculMoisUtils的类使用以下代码从applicationcontext(spring)加载bean:
Properties prop = new Properties();
InputStream input = null;
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
String file = (String) ctx.getBean("CalendarProp");
input = new FileInputStream(file);
prop.load(input);
这是测试类:
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();
}
}
问题是我要测试的类没有applicationcontext,即使我将它加载到相同代码的测试类上也是如此。
感谢您的帮助
罗曼