標籤:
課程設計要求進行junit測試,我是在已經做好的ssh項目上做的測試,測試類別代碼如下
package com.zhang.web.services;import static org.junit.Assert.*;import org.junit.After;import org.junit.Before;import org.junit.Test;import javax.annotation.Resource; import org.junit.runner.RunWith; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring-beans.xml") public class UserDAOTest extends AbstractJUnit4SpringContextTests{@Resource(name="userDAO") private UserDAO userDAO;public void setUserDAO(UserDAO userDAO) {this.userDAO = userDAO;}@Beforepublic void setUp() throws Exception {}@Afterpublic void tearDown() throws Exception {}@Testpublic void testFindUser() {assertEquals(userDAO.findUser(0),null);}}
運行測試時,出現如下錯誤
SEVERE: Caught exception while allowing TestExecutionListener [org.springframewor[email protected]a15856] to prepare test instance [[email protected]]java.lang.IllegalStateException: Failed to load ApplicationContextat org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)at org.junit.runners.ParentRunner.run(ParentRunner.java:300)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-beans.xml] cannot be opened because it does not exist
解決辦法是,把spring-beans.xml設定檔複製到src目錄下。
問題原因:本人一直認為eclipse的classpath包括web-inf目錄,其實不然。我們可以開啟web項目下的.classpath檔案,其內容如下
<?xml version="1.0" encoding="UTF-8"?><classpath><classpathentry kind="src" path="src"/><classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7"><attributes><attribute name="owner.project.facets" value="java"/></attributes></classpathentry><classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0"><attributes><attribute name="owner.project.facets" value="jst.web"/></attributes></classpathentry><classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/><classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/spring3"/><classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/struts"/><classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/hibernate"/><classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/><classpathentry kind="output" path="build/classes"/></classpath>
在這,我們並沒有看到類路徑包含web-inf目錄。假如我們往裡邊添加<classpathentry kind="src" path="WebContent/WEB-INF"/>再重啟eclipse,你會發現以上出現的問題不存在了,test類成功運行。
junit測試時,出現java.lang.IllegalStateException: Failed to load ApplicationContext