基本上就是记录各种报错的解决办法。基本上就是将散落在项目各个模块中的配置文件复制到测试模块中。
目录结构:
——src
——java
——test
——java
——DaoTest.java
——resources
——test
——applicationContext-dao.xml
——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模块下,这样涉及到的无关模块最少,最后很容易的通过了单元测试