基本上就是記錄各類報錯的解決辦法。基本上就是將散落在項目各個模塊中的配置文件複製到測試模塊中。java
目錄結構:spring
——srcapp
——java單元測試
——test測試
——javaui
——DaoTest.javathis
——resourcesspa
——testcode
——applicationContext-dao.xmlserver
——applicationContext-dubbo-client.xml
——applicationContext-dubbo-server.xml
——applicationContext-servcie.xml
——env.properties
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:testing/applicationContext-*.xml"
"classpath:testing/example-config.xml",
"classpath:testing/mq-receive.xml",
"classpath:testing/mq-send.xml"})
}) public class DaoTest { @Test public void test1(){ System.out.println("..."); } }
datasource中加載配置文件:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="testing/env.properties"/>
</bean>
好比錯誤:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXX': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.runtime.jms.send.xxxx com.xxxx.xxxx.service.common.ws.xxxx.xxxx; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xxxx.runtime.jms.send.xxxx] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
這基本就是該類所在的模塊的配置文件沒有引入到Junit中。
最終由於業務太複雜,涉及到不少業務我根本不瞭解,因此我將單元測試放到了dao模塊下,這樣涉及到的無關模塊最少,最後很容易的經過了單元測試