簡體   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