spring dubbo junit4 测试类配置

注解方式spring dubbo junit4 测试类的写法。

先列举下spring 和 bubbo的配置  再说 测试类怎么写。


applicationContext.xml

<beans>

    <context:annotation-config/>
    <context:component-scan base-package=" org.springframework.web"/>
    <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
    <import resource="spring/applicationContext_dubbo_provider.xml"/>
    <import resource="spring/applicationContext-db.xml"/>
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:system.propertiesvalue>
            list>
        property>
    bean>
beans>

applicationContext_dubbo_provider.xml
<beans>
    <dubbo:application name=""/>
    <context:property-placeholder location="classpath:zookeeper.properties" ignore-unresolvable="true"/>
    <dubbo:registry protocol="zookeeper" address="${dubbo.registry}"/>

    <dubbo:protocol name="dubbo" host="${dubbo.host}" port="${dubbo.port}"/>
    <dubbo:service interface="interf.DemoInterf"
                   ref="DemoImplService"/>
beans>
 
  
 
  
 
  
@Service("DemoImplService")
public class DemoImpl implements DemoInterf {
    @Resource
    private DemoBiz demoBiz;

    @Override
    public String add(DataVo dataVo) throws ProfreesionOrganizationException {
            String s = demoBiz.add(dataVo);
            return s;
    }
}
 
  
配置没有贴全,主要记录下写测试类遇到的问题和解决方法。
 
  
问题1
java.lang.IllegalStateException: Failed to load ApplicationContext
 
  
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
 
  解决:在测试类上加注解 
  
@WebAppConfiguration
测试环境使用,用来表示测试环境使用的ApplicationContext将是WebApplicationContext类型的

问题2
@Transactional  不写这个DB 不回滚
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)

测试类代码:
@WebAppConfiguration
 @Transactional
 @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
 @ContextConfiguration(locations={"classpath*:/applicationContext.xml"})
 @RunWith(SpringJUnit4ClassRunner.class)
 public class DemoImplTest {

    @Autowired
    private DemoInterf demoImplService;

    @Test
    public void testAdd() throws Exception {
        DataVo data = new DataVo(111,"test");
        demoImplService.add(data);
    }
}

稍后补上测试类里操作DB的写法。

你可能感兴趣的:(spring dubbo junit4 测试类配置)