我的代码可以作为JUnit测试用例正常运行。但是,当我在主类中放入相同的测试代码时,弹簧配置无法正确加载对象。
Spring代码如下所示:
@ContextConfiguration(locations = { "classpath:/fileonly-sens-services.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class AppTest extends ContextBuilder {
@BeforeClass
public static void setup() {
System.setProperty("app-
init.properties","classpath:test.app.properties");
@Test
someTestMethod()
}
我认为这是一个非常简单的问题,但我不能让它在JUnit之外工作!谢谢你的帮助!
答案 0 :(得分:1)
在您的应用程序中,您必须创建一个ApplicationContext。具体方法取决于您正在构建的应用程序类型。
如果要构建命令行应用程序,可以实例化ClassPathXmlApplicationContext并使用它来实例化所需的bean。
如果要构建Web应用程序,可以在应用程序初始化期间使用ContextLoaderListener加载上下文。