junit

JUnit theory for hashCode/equals contract

女生的网名这么多〃 提交于 2020-01-09 08:41:37
问题 The following class serve as generic tester for equals/hashCode contract. It is a part of a home grown testing framework. What do you think about? How can I (strong) test this class? It is a good use of Junit theories? The class: @Ignore @RunWith(Theories.class) public abstract class ObjectTest { // For any non-null reference value x, x.equals(x) should return true @Theory public void equalsIsReflexive(Object x) { assumeThat(x, is(not(equalTo(null)))); assertThat(x.equals(x), is(true)); } //

How do you reset Spring JUnit application context after a test class dirties it?

大憨熊 提交于 2020-01-09 06:34:07
问题 I'm using Spring 3.1.1.RELEASE, JUnit 4.8.1 and the HSQL 2.7.7 in-memory database. I have one test class annotated as @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:test-trainingSessionServiceContext.xml" }) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class TrainingSessionServiceTest { The problem is, when I run "mvn clean test", it seems that all test classes run after the above class fail because the in-memory database is destroyed and

Data-driven tests with jUnit

℡╲_俬逩灬. 提交于 2020-01-09 06:21:15
问题 What do you use for writing data-driven tests in jUnit? (My definition of) a data-driven test is a test that reads data from some external source (file, database, ...), executes one test per line/file/whatever, and displays the results in a test runner as if you had separate tests - the result of each run is displayed separately, not in one huge aggregate. 回答1: In JUnit4 you can use the Parameterized testrunner to do data driven tests. It's not terribly well documented, but the basic idea is

Data-driven tests with jUnit

蹲街弑〆低调 提交于 2020-01-09 06:20:05
问题 What do you use for writing data-driven tests in jUnit? (My definition of) a data-driven test is a test that reads data from some external source (file, database, ...), executes one test per line/file/whatever, and displays the results in a test runner as if you had separate tests - the result of each run is displayed separately, not in one huge aggregate. 回答1: In JUnit4 you can use the Parameterized testrunner to do data driven tests. It's not terribly well documented, but the basic idea is

How do I unit test jdbc code in java?

谁都会走 提交于 2020-01-09 06:04:07
问题 I'd like to write some unit tests for some code that connects to a database, runs one or more queries, and then processes the results. (Without actually using a database) Another developer here wrote our own DataSource, Connection, Statement, PreparedStatement, and ResultSet implementation that will return the corresponding objects based on an xml configuration file. (we could use the bogus datasource and just run tests against the result sets it returns). Are we reinventing the wheel here?

Class Not Found: Empty Test Suite in IntelliJ

橙三吉。 提交于 2020-01-09 02:53:07
问题 I'm just starting the computer science program at my college, and I'm having some issues with IntelliJ. When I try to run unit tests, I get the message Process finished with exit code 1 Class not found: "edu.macalester.comp124.hw0.AreaTest"Empty test suite. I also see a message entitled "No tests were found" on the left side of my screen. My test code is here: package edu.macalester.comp124.hw0; import org.junit.Test; import static org.junit.Assert.*; public class AreaTest { @Test public void

扔掉JUnit,迎接Spock

假如想象 提交于 2020-01-08 21:20:45
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 请不要误解,我丝毫没有诋毁JUnit的任何意思,我的意思只是……咳……换个更好用的……而且即便如此,你也不能真正的丢掉它,因为Spock自己也需要JUnit。 简单地讲,Spock是一个Groovy的 BDD 测试框架,如果你是第一次这个词,那不妨先看个 Spock的例子 ,然后再去访问刚才给出的BDD链接: def "subscribers receive published events at least once"() { when: publisher.send(event) then: (1.._) * subscriber.receive(event) where: event << ["started", "paused", "stopped"] } 上面的例子已经非常明白的展示了Spock中的测试例子,但Spock的优点远不仅此而已。 记得 Groovy 1.7 的新特性之一:Power Assert吗?它最早就来源于Spock。对于没有下载使用Groovy 1.7的读者,这里 简单说明一下 。假设代码中有一条Assert语句“assert 91 == a * b”,在a=10和b=9的情况下,语句肯定会失败。那么在console中会显示: Exception thrown Assertion

一文全面了解Android单元测试

浪尽此生 提交于 2020-01-08 10:28:04
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前言 成为一名优秀的Android开发,需要一份完备的 知识体系 ,在这里,让我们一起成长为自己所想的那样~。 ==》完整项目单元测试学习案例 众所周知,一个好的项目需要不断地打造,而一些有效的测试则是加速这一过程的利器。本篇博文将带你了解并逐步深入Android单元测试。 什么是单元测试? 单元测试就是针对类中的某一个方法进行验证是否正确的过程,单元就是指 独立的粒子 ,在Android和Java中大都是指方法。 为什么要进行单元测试? 使用单元测试可以提高开发效率,当项目随着迭代越来越大时,每一次编译、运行、打包、调试需要耗费的时间会随之上升,因此,使用单元测试可以不需这一步骤就可以对单个方法进行功能或逻辑测试。 同时,为了能测试每一个细分功能模块,需要将其相关代码抽成相应的方法封装起来,这也在一定程度上改善了代码的设计。因为是单个方法的测试,所以能更快地定位到bug。 单元测试case需要对这段业务逻辑进行验证。在验证的过程中,开发人员可以 深度了解业务流程 ,同时新人来了看一下项目单元测试就知道 哪个逻辑跑了多少函数,需要注意哪些边界 ——是的,单元测试做的好和文档一样 具备业务指导能力。 Android测试的分类 Android测试主要分为三个方面: 单元测试(Junit4、Mockito

Mybatis-PageHelper的简单使用

孤人 提交于 2020-01-07 14:17:18
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Mybatis-PageHelper一个简洁易用的mybatis分页插件。 文档地址: https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md 加入依赖 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> <scope>runtime</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId

聊聊jdk httpclient的connect timeout异常

只谈情不闲聊 提交于 2020-01-07 12:38:06
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 序 本文主要研究一下httpclient的connect timeout异常 实例代码 @Test public void testConnectTimeout() throws IOException, InterruptedException { HttpClient client = HttpClient.newBuilder() .build(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://twitter.com")) .build(); long start = System.currentTimeMillis(); try{ HttpResponse<String> result = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(result.body()); }finally { long cost = System.currentTimeMillis() - start; System.out.println("cost:"+cost); } } 异常日志如下: cost:75814