- 浏览: 7834794 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (2425)
- 软件工程 (75)
- JAVA相关 (662)
- ajax/web相关 (351)
- 数据库相关/oracle (218)
- PHP (147)
- UNIX/LINUX/FREEBSD/solaris (118)
- 音乐探讨 (1)
- 闲话 (11)
- 网络安全等 (21)
- .NET (153)
- ROR和GOG (10)
- [网站分类]4.其他技术区 (181)
- 算法等 (7)
- [随笔分类]SOA (8)
- 收藏区 (71)
- 金融证券 (4)
- [网站分类]5.企业信息化 (3)
- c&c++学习 (1)
- 读书区 (11)
- 其它 (10)
- 收藏夹 (1)
- 设计模式 (1)
- FLEX (14)
- Android (98)
- 软件工程心理学系列 (4)
- HTML5 (6)
- C/C++ (0)
- 数据结构 (0)
- 书评 (3)
- python (17)
- NOSQL (10)
- MYSQL (85)
- java之各类测试 (18)
- nodejs (1)
- JAVA (1)
- neo4j (3)
- VUE (4)
- docker相关 (1)
最新评论
-
xiaobadi:
jacky~~~~~~~~~
推荐两个不错的mybatis GUI生成工具 -
masuweng:
(转)JAVA获得机器码的实现 -
albert0707:
有些扩展名为null
java 7中可以判断文件的contenttype了 -
albert0707:
非常感谢!!!!!!!!!
java 7中可以判断文件的contenttype了 -
zhangle:
https://zhuban.me竹板共享 - 高效便捷的文档 ...
一个不错的网络白板工具
junit中的@classrule,可以在所有类方法开始前进行一些初始化调用,比如创建临时文件,
其中,@ClassRule中指定创建临时文件夹,这是在所有的测试方法前会创建文件夹,并且会在所有测试完成后,递归删除其下的子目录和子文件夹。
@Rule是方法级别的,每个测试方法执行时都会调用被注解的Rule,而@ClassRule是类级别的,在执行一个测试类的时候只会调用一次被注解的Rule
------------------------------------------------------------
再看下其他内置的rule
ExternalResource Rule
ExternalResource 是TemporaryFolder的父类,主要用于在测试之前创建资源,并在测试完成后销毁。
Java代码 收藏代码
File tempFile;
@Rule
public ExternalResource extResource = new ExternalResource() {
//每个测试执行之前都会调用该方法创建一个临时文件
@Override
protected void before() throws Throwable {
tempFile = File.createTempFile("test", ".txt");
}
//每个测试执行之后都会调用该方法删除临时文件
@Override
protected void after() {
tempFile.delete();
}
};
@Test
public void testExtResource() throws IOException {
System.out.println(tempFile.getCanonicalPath());
}
ErrorCollector Rule
ErrorCollector允许我们收集多个错误,并在测试执行完后一次过显示出来
Java代码 收藏代码
@Rule
public ErrorCollector errorCollector = new ErrorCollector();
@Test
public void testErrorCollector() {
errorCollector.addError(new Exception("Test Fail 1"));
errorCollector.addError(new Throwable("fff"));
}
Verifier Rule
Verifier是ErrorCollector的父类,可以在测试执行完成之后做一些校验,以验证测试结果是不是正确
Java代码 收藏代码
String result;
@Rule
public Verifier verifier = new Verifier() {
//当测试执行完之后会调用verify方法验证结果,抛出异常表明测试失败
@Override
protected void verify() throws Throwable {
if (!"Success".equals(result)) {
throw new Exception("Test Fail.");
}
}
};
@Test
public void testVerifier() {
result = "Fail";
}
TestWatcher Rule
TestWatcher 定义了五个触发点,分别是测试成功,测试失败,测试开始,测试完成,测试跳过,能让我们在每个触发点执行自定义的逻辑。
Java代码 收藏代码
@Rule
public TestWatcher testWatcher = new TestWatcher() {
@Override
protected void succeeded(Description description) {
System.out.println(description.getDisplayName() + " Succeed");
}
@Override
protected void failed(Throwable e, Description description) {
System.out.println(description.getDisplayName() + " Fail");
}
@Override
protected void skipped(AssumptionViolatedException e, Description description) {
System.out.println(description.getDisplayName() + " Skipped");
}
@Override
protected void starting(Description description) {
System.out.println(description.getDisplayName() + " Started");
}
@Override
protected void finished(Description description) {
System.out.println(description.getDisplayName() + " finished");
}
};
@Test
public void testTestWatcher() {
/*
测试执行后会有以下输出:
testTestWatcher(org.haibin369.test.RulesTest) Started
Test invoked
testTestWatcher(org.haibin369.test.RulesTest) Succeed
testTestWatcher(org.haibin369.test.RulesTest) finished
*/
System.out.println("Test invoked");
}
TestName Rule
TestName能让我们在测试中获取目前测试方法的名字。
Java代码 收藏代码
@Rule
public TestName testName = new TestName();
@Test
public void testTestName() {
//打印出测试方法的名字testTestName
System.out.println(testName.getMethodName());
package com.jdriven; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; public class JUnitClassRuleTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static File tempFile; @BeforeClass public static void createTempFile() throws IOException { tempFile = temporaryFolder.newFile("tempFile.txt"); } @Test public void testJUnitClassRule_One() { //Your test should go here, which uses tempFile } @Test public void testJUnitClassRule_Two() { //Your test should go here and uses the same tempFile } }
其中,@ClassRule中指定创建临时文件夹,这是在所有的测试方法前会创建文件夹,并且会在所有测试完成后,递归删除其下的子目录和子文件夹。
@Rule是方法级别的,每个测试方法执行时都会调用被注解的Rule,而@ClassRule是类级别的,在执行一个测试类的时候只会调用一次被注解的Rule
------------------------------------------------------------
再看下其他内置的rule
ExternalResource Rule
ExternalResource 是TemporaryFolder的父类,主要用于在测试之前创建资源,并在测试完成后销毁。
Java代码 收藏代码
File tempFile;
@Rule
public ExternalResource extResource = new ExternalResource() {
//每个测试执行之前都会调用该方法创建一个临时文件
@Override
protected void before() throws Throwable {
tempFile = File.createTempFile("test", ".txt");
}
//每个测试执行之后都会调用该方法删除临时文件
@Override
protected void after() {
tempFile.delete();
}
};
@Test
public void testExtResource() throws IOException {
System.out.println(tempFile.getCanonicalPath());
}
ErrorCollector Rule
ErrorCollector允许我们收集多个错误,并在测试执行完后一次过显示出来
Java代码 收藏代码
@Rule
public ErrorCollector errorCollector = new ErrorCollector();
@Test
public void testErrorCollector() {
errorCollector.addError(new Exception("Test Fail 1"));
errorCollector.addError(new Throwable("fff"));
}
Verifier Rule
Verifier是ErrorCollector的父类,可以在测试执行完成之后做一些校验,以验证测试结果是不是正确
Java代码 收藏代码
String result;
@Rule
public Verifier verifier = new Verifier() {
//当测试执行完之后会调用verify方法验证结果,抛出异常表明测试失败
@Override
protected void verify() throws Throwable {
if (!"Success".equals(result)) {
throw new Exception("Test Fail.");
}
}
};
@Test
public void testVerifier() {
result = "Fail";
}
TestWatcher Rule
TestWatcher 定义了五个触发点,分别是测试成功,测试失败,测试开始,测试完成,测试跳过,能让我们在每个触发点执行自定义的逻辑。
Java代码 收藏代码
@Rule
public TestWatcher testWatcher = new TestWatcher() {
@Override
protected void succeeded(Description description) {
System.out.println(description.getDisplayName() + " Succeed");
}
@Override
protected void failed(Throwable e, Description description) {
System.out.println(description.getDisplayName() + " Fail");
}
@Override
protected void skipped(AssumptionViolatedException e, Description description) {
System.out.println(description.getDisplayName() + " Skipped");
}
@Override
protected void starting(Description description) {
System.out.println(description.getDisplayName() + " Started");
}
@Override
protected void finished(Description description) {
System.out.println(description.getDisplayName() + " finished");
}
};
@Test
public void testTestWatcher() {
/*
测试执行后会有以下输出:
testTestWatcher(org.haibin369.test.RulesTest) Started
Test invoked
testTestWatcher(org.haibin369.test.RulesTest) Succeed
testTestWatcher(org.haibin369.test.RulesTest) finished
*/
System.out.println("Test invoked");
}
TestName Rule
TestName能让我们在测试中获取目前测试方法的名字。
Java代码 收藏代码
@Rule
public TestName testName = new TestName();
@Test
public void testTestName() {
//打印出测试方法的名字testTestName
System.out.println(testName.getMethodName());
发表评论
-
复习:强迫线程顺序执行方式
2019-01-03 23:42 1466方法1: 三个线程,t1,t2,t3,如果一定要按顺序执行, ... -
(转)不错的前后端处理异常的方法
2019-01-02 23:16 1957前言 在 Web 开发中, 我们经常会需要处理各种异常, 这是 ... -
info q的极客时间大咖说等资料下载
2018-08-15 08:40 3406info q的极客时间大咖说等资料下载,还有不少思维导图 链 ... -
CXF 客户端超时时间设置(非Spring配置方式)
2018-07-03 22:38 2172import org.apache.cxf.endpoint. ... -
(转)synchronized关键字画像:正确打开方式
2018-06-14 09:25 438https://mp.weixin.qq.com/s/b3Sx ... -
CountDownLatch的例子
2018-06-13 14:10 622public class StatsDemo { ... -
两道面试题,带你解析Java类加载机制
2018-06-12 16:29 543https://mp.weixin.qq.com/s/YTa0 ... -
Spring中获取request的几种方法,及其线程安全性分析
2018-06-11 09:03 613https://mp.weixin.qq.com/s/KeFJ ... -
内部类小结
2018-06-06 10:25 385https://mp.weixin.qq.com/s/hErv ... -
JVM虚拟机小结1
2018-06-04 20:43 4451 jps -l //列出详细的类名和进程ID 2)jps ... -
windows下自带命令行工具查看CPU资源情况等
2018-06-04 12:53 3031微软提供了不少命令行 ... -
(收藏)深入分析Java的序列化与反序列化
2018-05-30 15:21 545https://mp.weixin.qq.com/s/T2Bn ... -
apache common包中的序列化工具
2018-05-30 09:10 1763什么是序列化 我们的 ... -
JAVA8 JVM的变化: 元空间(Metaspace)
2018-05-24 22:30 896本文将会分享至今为至我收集的关于永久代(Permanent G ... -
(转)服务器性能指标(一)——负载(Load)分析及问题排查
2018-05-21 21:03 1252原创: Hollis Hollis 负载 ... -
(转)对象复用
2018-05-20 15:27 799public class Student { priv ... -
mapreduce中入门中要注意的几点
2018-05-06 08:59 607在 mapreduce中,比如有如下的词: I love b ... -
HDFS的基本操作
2018-05-02 21:47 866-mkdir 在HDFS创建目录 ... -
一个不错的开源工具类,专门用来解析日志头部的,好用
2018-05-02 20:00 698一个不错的开源工具类,专门用来解析日志头部的,好用。 http ... -
介绍个不错的RESTFUL MOCK的工具wiremock
2018-04-27 21:02 1842介绍个不错的RESTFUL MOCK的工具wiremock,地 ...
相关推荐
在JUnit中使用@Rule测试文件和目录Java开发Java经验技巧共3页.pdf.zip
Junit是java中测试的必备工具,Junit_test这个程序是更好的实现对Junit的了解。帮助大家学习
JUnit 类别 JUnit 4.9 引入了一个很好的注释来对您的测试进行分类。 而 TestNG 选择为使用字符串 JUnit 类别使用类。 这比字符串更灵活,这在使用多模块 maven 项目时可能会很痛苦(有关更多详细信息,请参阅 JUnit ...
今天小编就为大家分享一篇关于Junit 5中@ParameterizedTest与@EnumSource结合使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
测试规则嵌入式mongodb 测试规则嵌入MongoDB是的一个JUnit @ClassRule包装支撑JUnit和斯波克。 在测试开始时它会触发 mongoDb 服务器,当测试结束时它会停止 mongoDb。二进制文件Mavan 示例: < dependency> ...
JUnit类别 弄清楚如何在多模块Maven项目中使用@Category Maven 万无一失的插件 surefire 插件具有默认文件模式,它寻找作为单元测试运行 我们应该能够通过 JUnit @Category包含组 我们应该能够通过 JUnit @...
Junit 5中文帮助指南,JUnit 5跟以前的JUnit版本不一样,它由几大不同的模块组成,这些模块分别来自三个不同的子项目。 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage JUnit Platform是在JVM上 启动...
使用junit中例子, junit demo, junit 教程
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,包含依赖包:junit-jupiter-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-jupiter-params-5.5.1.jar,junit-platform-launcher-1.5.1.jar,junit-...
NULL 博文链接:https://bijian1013.iteye.com/blog/2119792
org.junit.Rule.class org.junit.Test.class org.junit.experimental.ParallelComputer.class org.junit.experimental.categories.Categories.class org.junit.experimental.categories.Category.class org.junit....
A JUnit rule to easily override Dagger 2 objects More info about testing using Dagger 2 and Mockito are available in this Medium post. Overriding an object managed by Dagger 2 is not easy, you ...
例如,@ @Test必须在“公共非静态void no-arg方法”上进行注释, @ClassRule必须在“公共静态TestRule方法/字段”上进行注释。 尽管JUnit将在运行测试之前对其进行检查,但是如何在编译测试中检查其用法呢? 用法 ...
在测试类的公共字段中声明用@Rule 注释的S2TestRule。 // @RunWith(Seasar2.class) は不要 public class FooTest { // S2TestRuleインスタンスをpublicフィールドにし、@Ruleで注釈する。 @Rule public S2...
junit4.1junit4.1junit4.1junit4.1junit4.1
赠送jar包:junit-4.11.jar; 赠送原API文档:junit-4.11-javadoc.jar; 赠送源代码:junit-4.11-sources.jar; 赠送Maven依赖信息文件:...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
赠送jar包:junit-4.13.2.jar; 赠送原API文档:junit-4.13.2-javadoc.jar; 赠送源代码:junit-4.13.2-sources.jar;...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
这个好用,junit,junit,junit
JUNIT介绍.JUNIT介绍.JUNIT介绍.JUNIT介绍.