SpringBoot(十二):springboot2.0.2写测试用例

导入maven依赖:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

测试用例:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boco.jobmonitor.App;
import com.boco.jobmonitor.model.SjmcJobitem;
import com.boco.jobmonitor.service.SjmcJobitemService;
import com.boco.jobmonitor.service.impl.SjmcJobitemServiceImpl;
import com.boco.jobmonitor.vo.ClusterAppVo;
import com.boco.jobmonitor.vo.ClusterNodeVo;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { App.class, JobitemServiceImpl.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)//配置启动类
public class AppTest {

    @Autowired
    private JobitemService sJobitemService;

    @Test
    public void testInsertJob() {
        Jobitem jobitem = new Jobitem("application_1548381669007_0055", "/home/dx/tommyduan/submit_x1_x2.sh",
                "RUNING", "", "1", "admin", new Date());
        Integer result = sJobitemService.insert(jobitem);
        System.out.println(result);
    }
}

注意:

在@SpringBootTest(classes = { App.class, JobitemServiceImpl.class }中必须包含JobitemServiceImpl,否则会抛出异常,说一些依赖未能加载。

App.class是springboot的入口类:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

import tk.mybatis.spring.annotation.MapperScan;

/**
 * SpringBoot入口函数<br>
 * 启动脚本:java -jar -Dspring.profiles.active=prod -Dserver.port=8080
 * 。。。-web-1.0.0-SNAPSHOT.jar
 * */
@SpringBootApplication(scanBasePackages = {})
@MapperScan("com.dx.jobmonitor.mapper")
@EnableScheduling
public class App {
    private static final Logger logger = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) {
        logger.info("App start...");
        SpringApplication.run(App.class, args);
    }
}

运行:

运行日志如下:

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

  .   ____          _            __ _ _
 /\\ / ___‘_ __ _ _(_)_ __  __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  ‘  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.2.RELEASE)

2019-02-18 22:38:59.277  INFO 21268 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
Mon Feb 18 22:39:01 CST 2019 WARN: Establishing SSL connection without server‘s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn‘t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false‘. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2019-02-18 22:39:01.921  INFO 21268 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2019-02-18 22:39:01.927 DEBUG 21268 --- [           main] c.b.j.m.PermissionMapper.selectAllPerms  : ==>  Preparing: SELECT id, permission_id, name, description, url, perms, parent_id, type, order_num, icon, status, create_time, update_time FROM permission WHERE status=? ORDER BY order_num
2019-02-18 22:39:02.089 DEBUG 21268 --- [           main] c.b.j.m.PermissionMapper.selectAllPerms  : ==> Parameters: 1(Integer)
2019-02-18 22:39:02.152 DEBUG 21268 --- [           main] c.b.j.m.PermissionMapper.selectAllPerms  : <==      Total: 41
2019-02-18 22:39:02.624  INFO 21268 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-02-18 22:39:02.624  INFO 21268 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2019-02-18 22:39:02.635  INFO 21268 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : Loaded APR based Apache Tomcat Native library [1.2.19] using APR version [1.6.5].
2019-02-18 22:39:02.636  INFO 21268 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2019-02-18 22:39:02.636  INFO 21268 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2019-02-18 22:39:02.639  INFO 21268 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1a  20 Nov 2018]
2019-02-18 22:39:02.814  INFO 21268 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-02-18 22:39:04.705  WARN 21268 --- [           main] org.thymeleaf.templatemode.TemplateMode  : [THYMELEAF][main] Template Mode ‘LEGACYHTML5‘ is deprecated. Using Template Mode ‘HTML‘ instead.
2019-02-18 22:39:06.152  INFO 21268 --- [           main] c.b.jobmonitor.aspect.RedisCacheAspect   : 清空缓存 - scom_dx_jobmonitor_service_impl_JobitemServiceImpl*
2019-02-18 22:39:06.255  INFO 21268 --- [           main] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2019-02-18 22:39:06.256  INFO 21268 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
2019-02-18 22:39:06.459 DEBUG 21268 --- [           main] c.b.j.mapper.JobitemMapper.insert    : ==>  Preparing: INSERT INTO jobitem ( id,appid,submitfilepath,state,monitortype,createuserid,createusername,createtime ) VALUES( ?,?,?,?,?,?,?,? )
2019-02-18 22:39:06.508 DEBUG 21268 --- [           main] c.b.j.mapper.JobitemMapper.insert    : ==> Parameters: null, application_1548381669007_0054(String), /home/dx/tommyduan/submit_x1_x2.sh(String), RUNING(String), (String), 1(String), admin(String), 2019-02-18 22:39:06.137(Timestamp)
2019-02-18 22:39:06.540 DEBUG 21268 --- [           main] c.b.j.mapper.JobitemMapper.insert    : <==    Updates: 1
2019-02-18 22:39:06.543 DEBUG 21268 --- [           main] c.b.j.m.S.insert!selectKey               : ==>  Executing: SELECT LAST_INSERT_ID()
2019-02-18 22:39:06.546 DEBUG 21268 --- [           main] c.b.j.m.S.insert!selectKey               : <==      Total: 1
1
2019-02-18 22:39:07.705  INFO 21268 --- [       Thread-3] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed

原文地址:https://www.cnblogs.com/yy3b2007com/p/10398363.html

时间: 2024-11-09 00:56:18

SpringBoot(十二):springboot2.0.2写测试用例的相关文章

springboot(十二):springboot如何测试打包部署

有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. 1.在pom包中添加spring-boot-starter-test包引用 <dependency> <groupId>org.springframework.boot</groupI

汇编试验十二 编写0号中断的处理程序

要求:除法溢出的时候,在屏幕中心处显示overflow! 分析: 0号中断,对应的中断入口地址中的程序可以不用修改,放到内存0:空余位置(空余位置一般是0000::0200~0000:02FF)于是,就将显示字符串的程序放到这里: 用一个do0安装程序,将这个中断程序复制到内存0000:0200那里去:格式是:es:di 目的地址,ds:si源程序,cld复制方向,rep movsb 复制 子程序(中断程序)中,显示的字符串,不能放到一个独立的代码段,而是放到一个中断程序本身里面,因为复制程序运

微服务springboot视频最新SpringBoot2.0.3版本技术视频教程【免费学习】

超火爆的springboot微服务技术怎么学,看这里,springboot超详细的教程↓↓↓↓↓↓https://ke.qq.com/course/179440?tuin=9b386640 01.springboot介绍02.微服务介绍03.springboot第一个例子04.Springboot中的常用注解分析05.springboot启动配置分析06.springboot热部署07.springboot的yaml语法08.springboot属性配置文件方式详解[email protecte

零基础快速入门SpringBoot2.0 教程(二)

一.SpringBoot2.x使用Dev-tool热部署简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用 官方地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools 核心依赖包: <dependency> <groupId>org.springframework.boot</gro

第二十二篇:再写Windows驱动,再玩Windbg---NET

2011年到现在,就没再怎么搞过Windows驱动了. 最近, 由于项目需要, 试着改一改一个显卡驱动(KMDOD), 从实践上证明, 我在理论上对一个驱动的架构的正确与否.(USB Display = KMDOD + AVStream). 其中, KMDOD是完成显示的部分功能, 完成其中的VidPN(Video present network), 将驱动中原来的POST物理设备转变为USB物理设备. 而AVStream之所以这样提出, 完成是由于USB Video class的启发, 要不然

java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)

java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessController的checkPerssiom方法,访问控制器AccessController的栈检查机制又遍历整个 PerssiomCollection来判断具体拥有什么权限一旦发现栈中一个权限不允许的时候抛出异常否则简单的返回,这个过程实际上比我的描述要复杂 得多,这里我只是简单的一句带过,因为这

[转]我要学ASP.NET MVC 3.0(十二): MVC 3.0 使用自定义的Html控件

本文转自:http://www.cnblogs.com/lukun/archive/2011/08/05/2128693.html 概述   在ASP.NET MVC框架中已经封装了很多基于Html标准的Html控件,我们可以方便的使用这些控件输出想要的内容,使开发变得快捷. 例如ASP.NET MVC框架包括以下设置标准的HTML控件(部分控件): Html.ActionLink() Html.BeginForm() Html.CheckBox() Html.DropDownList() Ht

SpringBoot2.0源码分析(二):整合ActiveMQ分析

SpringBoot具体整合ActiveMQ可参考:SpringBoot2.0应用(二):SpringBoot2.0整合ActiveMQ ActiveMQ自动注入 当项目中存在javax.jms.Message和org.springframework.jms.core.JmsTemplate着两个类时,SpringBoot将ActiveMQ需要使用到的对象注册为Bean,供项目注入使用.一起看一下JmsAutoConfiguration类. @Configuration @Conditional

Senparc.Weixin.MP SDK 微信公众平台开发教程(十二):OAuth2.0说明

紧接上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(十一):高级接口说明>,这里专讲OAuth2.0. 理解OAuth2.0 首先我们通过一张图片来了解一下OAuth2.0的运作模式: 从上图我们可以看到,整个过程进行了2次"握手",最终利用授权的AccessToken进行一系列的请求,相关的过程说明如下: A:由客户端向服务器发出验证请求,请求中一般会携带这些参数 ID标识,例如appId 验证后跳转到的URL(redirectUrl) 状态参数