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.properties</value>
            </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的写法。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值