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
  • 相关阅读:
    【前端积累】Awesome初识
    【Python系列】Python3获取控制台输入
    【linux系列】Centos下安装mysql数据库
    Intellij 部署项目java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    【大数据系列】节点的退役和服役[datanode,yarn]
    【大数据系列】使用api修改hadoop的副本数和块大小
    【规范】alibaba编码规范阅读
    【大数据系列】hadoop上传文件报错_COPYING_ could only be replicated to 0 nodes
    【分布式系列之ActiveMq】ActiveMq入门示例
    类的生命周期
  • 原文地址:https://www.cnblogs.com/bbsh/p/11733589.html
Copyright © 2011-2022 走看看