zoukankan      html  css  js  c++  java
  • springMVC和springboot项目——单元测试

    一、springboot项目:

      1、选中service层需要测试的类右击go to——Test——create new test——在下方选中要测试的方法,点OK

      2、项目的test补录下创建一个测试父类AbstractTest

     1 package com.vhr.test;
     2 
     3 import org.junit.runner.RunWith;
     4 import org.sang.HrserverApplication;
     5 import org.springframework.boot.test.context.SpringBootTest;
     6 import org.springframework.test.context.ActiveProfiles;
     7 import org.springframework.test.context.TestPropertySource;
     8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     9 import org.springframework.test.context.web.WebAppConfiguration;
    10 
    11 @RunWith(SpringJUnit4ClassRunner.class)
    12 @SpringBootTest(classes = HrserverApplication.class)//该class是SpringBoot项目的Application
    13 @ActiveProfiles("test")
    14 @WebAppConfiguration
    15 @TestPropertySource({"classpath:/application.properties"})//加载spring配置文件
    16 public class AbstractTest {
    17 }
    View Code

      3、SpringBoot项目的启动类上加@EnableAutoConfiguration 注解

      4、所有单元测试类继承刚才创建的AbstractTest类,然后写测试逻辑运行即可

    注:如报错 Error:(15, 8) java: org.sang.config.WebSocketConfig不是抽象的,并且未覆盖org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer中的抽象方法configureMessageConverters(java.util.List<org.springframework.messaging.converter.MessageConverter>)

    原来代码编译正常,在升级新的依赖包后,运行提示上述的错误“Error:(错误: xxx不是抽象的, 并且未覆盖xxx中的抽象方法”)

    经排查,是由于升级后,新依赖的包被继承的类多了一个抽象方法;报错的当前类继承了抽象类后没有重写多的那个抽象方法;

    解决方法:根据错误提示,找到抽象类新增的方法,在报错的类中进行重写,满足语法要求不报错即可。

    二、springMVC项目:

      1、选中service层需要测试的类右击go to——Test——create new test——在下方选中要测试的方法,点OK

      2、在测试类中加上注解:

      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(locations = "classpath:/spring/application*.xml") //配置文件路径
    当然,也可创建测试父类,让所有单元测试类继承它
     1 package com.srdz.service.impl;
     2 
     3 import com.srdz.service.OcrService;
     4 import org.json.JSONObject;
     5 import org.junit.Test;
     6 import org.junit.runner.RunWith;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.test.context.ContextConfiguration;
     9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    10 
    11 import static org.junit.Assert.*;
    12 
    13 @RunWith(SpringJUnit4ClassRunner.class)
    14 @ContextConfiguration(locations = "classpath:/spring/application*.xml") //配置文件路径
    15 
    16 public class OcrServiceImplTest {
    17     @Autowired
    18     private OcrService ocrService;
    19 
    20     @Test
    21     public void ocrVehicle() throws Exception {
    22         String img="D:\lhjk\ceshi3.png";
    23         JSONObject jsonObject = ocrService.ocrVehicle(img);
    24         System.out.println(jsonObject);
    25     }
    26 
    27 }
    View Code
  • 相关阅读:
    发布(Windows)
    Parallel并行编程
    query通用开源框架
    深入了解三种针对文件(JSON、XML与INI)的配置源
    GitLab CI
    雅思创始人Keith Taylor谈英语学习
    查看内存使用情况
    Reverse String
    分布式消息系统jafka快速起步(转)
    深入浅出 消息队列 ActiveMQ(转)
  • 原文地址:https://www.cnblogs.com/bbsh/p/11733589.html
Copyright © 2011-2022 走看看