SSM框架添加junit单元测试

川长思鸟来 2021-12-10 07:49 436阅读 0赞

1.添加pom依赖

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <scope>test</scope>
  5. </dependency>
  6. <!--加入spring-test-->
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-test</artifactId>
  10. <version>5.1.1.RELEASE</version>
  11. </dependency>

2.添加test类

20190713114523730.png

3.配置test类

20190713114600766.png

4.AccountItemServiceTest类

  1. public class AccountItemServiceTest extends CommonService {
  2. @Test
  3. public void queryAccountingSubject(){
  4. List<AccountItemModel> accountItemModel = accountItemService.queryAccountingSubject("会计分录");
  5. }
  6. }

5.CommonService类

  1. public class CommonService {
  2. public BeanFactory beanFactory;
  3. public AccountItemService accountItemService;
  4. @Before
  5. public void setUp(){
  6. beanFactory = new ClassPathXmlApplicationContext("spring/spring.xml");
  7. accountItemService = (AccountItemService) beanFactory.getBean("accountItemService");
  8. }
  9. }

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

启动测试类成功。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L25pbmdtZW5nYmFieQ_size_16_color_FFFFFF_t_70

为什么要用单元测试?

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

遇到问题

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

添加如下的依赖就好了

  1. <dependency>
  2. <groupId>org.apache.curator</groupId>
  3. <artifactId>curator-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.curator</groupId>
  7. <artifactId>curator-framework</artifactId>
  8. </dependency>

发表评论

表情:
评论列表 (有 0 条评论,436人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java web——单元测试框架Junit

    一、软件测试种类: 单元测试:主要是用于测试程序模块,确保代码运行正确。单元测试是由开发者编写并进行运行测试。一般使用的测试框架是Junit或者 testNG。测试用例

    相关 junit单元测试框架

    junit单元测试框架 一,什么是junit单元测试框架 junit单元测试框架是一个纯java开发的测试功能框架,基本所有的java框架都对junit单元测试框架有