• 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
  • 相关阅读:
    【未完成0.0】Noip2012提高组day2 解题报告
    【数论+技巧】神奇的Noip模拟试题第二试 T1 素数统计
    Noip2014 提高组 T2 联合权值 连通图+技巧
    Noip2014 提高组 day1 T1· 生活大爆炸版石头剪刀布
    神奇的Noip模拟试题 T3 科技节 位运算
    博客小谈
    神奇的Noip模拟试题一试 2 排队
    神奇的Noip模拟试题第一试 合理种植 枚举+技巧
    使用Desktop App Converter打包桌面应用程序
    Rust Linking With C Library Functions
  • 原文地址:https://www.cnblogs.com/bbsh/p/11733589.html
Copyright © 2020-2023  润新知