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(一定不能忘记注册@Service到该类,否则会抛出异常,说一些依赖未能加载)。
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
SpringBoot(十二):springboot2.0.2写测试用例的更多相关文章
- springboot学习入门简易版二---springboot2.0项目创建
2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...
- SpringBoot(十一):springboot2.0.2下配置mybatis generator环境,并自定义字段/getter/settetr注释
Mybatis Generator是供开发者在mybatis开发时,快速构建mapper xml,mapper类,model类的一个插件工具.它相对来说对开发者是有很大的帮助的,但是它也有不足之处,比 ...
- springboot(十二):springboot如何测试打包部署
有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...
- SpringBoot(十三):springboot2.0.2定时任务
使用定义任务: 第一步:启用定时任务第二步:配置定时器资源等第三步:定义定时任务并指定触发规则 1)启动类启用定时任务 在springboot入口类上添加注解@EnableScheduling即可. ...
- springboot(十二):springboot单元测试、打包部署
单元测试 1.在pom包中添加spring-boot-starter-test包引用 <dependency> <groupId>org.springframework.boo ...
- springboot(十二)-分布式锁(redis)
什么是分布式锁? 要介绍分布式锁,首先要提到与分布式锁相对应的是线程锁.进程锁. 线程锁:主要用来给方法.代码块加锁.当某个方法或代码使用锁,在同一时刻仅有一个线程执行该方法或该代码段.线程锁只在同一 ...
- springboot(十二) SpringBoot 性能优化
代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo springboot优化主要有三类优化:1.包扫描优化 2. ...
- Activiti7整合SpringBoot(十二)
1 SpringBoot 整合 Activiti7 的配置 为了能够实现 SpringBoot 与 Activiti7 整合开发,首先我们要引入相关的依赖支持.所以,我们在工程的 pom.xml 文件 ...
- SpringBoot(十二)_springboot整合PageHelper
我之所以会发现这个PageHelper这个东东 是因为公司在使用 ,刚开始我也没太注意这个插件,感觉不就是个分页插件吗?也就那样,直到一天,我在网上找了个代码生成器,用来构建代码,因为它是针对mysq ...
随机推荐
- liunx命令简介
图形界面和命令行要达到的目的是一样的,都是让用户控制计算机.然而,真正能够控制计算机硬件(CPU.内存.显示器等)的只有操作系统内核(Kernel),图形界面和命令行只是架设在用户和内核之间的一座桥梁 ...
- MySql与python交互
No1: 引入MySql:python2是Mysqldb,python3是pymysql No2: Connection对象 用于建立与数据库的连接 创建对象:调用connect()方法 conn=c ...
- P2158 [SDOI2008]仪仗队
P2158 [SDOI2008]仪仗队图是关于y=x对称的,横纵坐标一定是互质的否则在之前就被扫过了,所以就可以用欧拉函数再*2就完了. #include<iostream> #inclu ...
- c#一步一步实现ORM
本篇适合新手了解学习orm.欢迎指正,交流学习. 现有的优秀的orm有很多. EF:特点是高度自动化,缺点是有点重. Nhibnate:缺点是要写很多的配置. drapper:最快的orm.但是自动化 ...
- unity3d俄罗斯方块源码教程+源码和程序下载
小时候,大家都应玩过或听说过<俄罗斯方块>,它是红白机,掌机等一些电子设备中最常见的一款游戏.而随着时代的发展,信息的进步,游戏画面从简单的黑白方块到彩色方块,游戏的玩法机制从最简单的消方 ...
- linux学习笔记 其他常用命令
cd + 回车 = cd ~ 进入当前用户主目录 查看指定进程信息 *ps -ef |grep 进程名 *ps -查看属于自己的进程 *ps -aux 查看所有的用户的执行进程 换成 ps - ...
- 在windows上安装redis并设置密码
在windows上安装redis Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redi ...
- 拓扑排序 --- AtCode - 3596
题目链接: https://cn.vjudge.net/problem/1137733/origin 拓扑排序的基本思想: https://blog.csdn.net/qq_41713256/arti ...
- 2017-9-10-Vim使用说明
首先,很多linux发行版直接进入的是vi,不是vim,刚开始使用vi会有点蒙,需要改一下配置文件到vim:"打开vi编辑器,输入i,左下角没有出现-INSERT-字样,且编辑模式跟vim不 ...
- (Android数据传递)Intent消息传递机制 “Intent”“数据传递”
Intent类的继承关系: 需要注意的是,该类实现了Parcelable(用于数据传递)和Cloneable接口. Intent是一种(系统级别的)消息传递机制,可以在应用程序内使用,也可以在应用 ...