SSM框架添加junit单元测试

1.添加pom依赖 

   <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!--加入spring-test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.1.RELEASE</version>
        </dependency>

2.添加test类

3.配置test类

4.AccountItemServiceTest类

public class AccountItemServiceTest extends CommonService {

    @Test
    public void queryAccountingSubject(){
        List<AccountItemModel> accountItemModel = accountItemService.queryAccountingSubject("会计分录");
    }
}

 5.CommonService类

public class CommonService {
    public BeanFactory beanFactory;
    public AccountItemService accountItemService;

    @Before
    public void setUp(){
        beanFactory = new ClassPathXmlApplicationContext("spring/spring.xml");
        accountItemService = (AccountItemService) beanFactory.getBean("accountItemService");
    }
}

new ClassPathXmlApplicationContext 说明用的sping容器时appliationContext。

启动测试类成功。

为什么要用单元测试?

直接从测试类开始,不用重新启动项目,高效。我这里配置的是调试service,可以根据项目需求配置不同的common...。

遇到问题

项目中用到了dubbo,但是没有配置好 ,报启动报java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy问题

添加如下的依赖就好了

<dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值