繁体   English   中英

从eclipse运行JUnit测试时加载ApplicationContext,但从ant build运行时加载ApplicationContext

[英]ApplicationContext loads when JUnit test run from eclipse, but not from ant build

对我来说似乎很奇怪。 从Eclipse开始,JUnit测试运行良好。 但是,当我从Ant命令行运行时,它显示错误。 看起来它没有加载/应用程序上下文。 这是测试类的基类。

@ContextConfiguration(locations = { "classpath*:applicationContext.xml"})
public abstract class TransactionalTestCase extends StrutsSpringTransactionalTests {

    @Before
    public void onSetUp() throws Exception {
        super.setUp();
    }

    @Override
    protected void setupBeforeInitDispatcher() throws Exception {
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext);
    }

当我尝试使用此方法打印appContext时,它将打印org.springframework.context.support.GenericApplicationContext@9320aa71:启动日期xxxxxxx

但是它从ant build命令行输出null。

我比较了eclipse和ant的类路径。 两者都一样。 包括相同的文件和文件夹集。

当我从ant build运行时, @ContextConfiguration无法生效吗?

还有什么可能是错的?

StrutsSrpingTransactionalTests类

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
    TransactionalTestExecutionListener.class,
    DependencyInjectionTestExecutionListener.class, 
    DirtiesContextTestExecutionListener.class
})
@Transactional
public abstract class StrutsSpringTransactionalTests extends StrutsTestCase implements ApplicationContextAware {

    protected ApplicationContext appContext;

    @Override
    public void setApplicationContext(ApplicationContext appContext) throws BeansException {
        this.appContext = appContext;
    }
}

弄清楚,问题出在JUnit版本上。 Eclipse默认使用Junit4,而ant默认使用Junit3。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM