hamcrest

How to use (primitive) autoboxing/widening with Hamcrest?

限于喜欢 提交于 2019-12-01 16:59:43
I came across https://code.google.com/p/hamcrest/issues/detail?id=130 to add some sugar syntax for Hamcrest matchers. But the idea was rejected by the Hamcrest developers. Any other smart ideas to make tests better readable by avoiding having to type L behind longs? @Test public void test1() { int actual = 1; assertThat(actual, is(1)); } @Test public void test2() { long actual = 1L; assertThat(actual, is(1)); // fails as expected is <1> but result was <1L> // assertThat(actual, is(1L)); off course works.. } @Test public void test3() { Long actual = new Long(1); assertThat(actual, is(1)); //

Spring Boot 的单元测试和集成测试

做~自己de王妃 提交于 2019-11-30 14:22:07
学习如何使用本教程中提供的工具,并在 Spring Boot 环境中编写单元测试和集成测试。 1. 概览 本文中,我们将了解如何编写单元测试并将其集成在 Spring Boot 环境中。你可在网上找到大量关于这个主题的教程,但很难在一个页面中找到你需要的所有信息。我经常注意到初级开发人员混淆了单元测试和集成测试的概念,特别是在谈到 Spring 生态系统时。我将尝试讲清楚不同注解在不同上下文中的用法。 2. 单元测试 vs. 集成测试 维基百科是这么说 单元测试 的: > 在计算机编程中,单元测试是一种软件测试方法,用以测试源代码的单个单元、一个或多个计算机程序模块的集合以及相关的控制数据、使用过程和操作过程,以确定它们是否适合使用。 集成测试 : > “集成测试(有时也称集成和测试,缩写为 I&T)是软件测试的一个阶段,在这个阶段中,各个软件模块被组合在一起来进行测试。” 简而言之,当我们在做单元测试时,只是测试了一个代码单元,每次只测试一个方法,不包括与正测试组件相交互的其他所有组件。 另一方面,在集成测试中,我们测试各组件之间的集成。**由于单元测试,我们可知这些组件行为与所需一致,但不清楚它们是如何在一起工作的。**这就是集成测试的职责。 3. Java 单元测试 所有 Java 开发者都知道 JUnit 是执行单元测试的主要框架。它提供了许多注解来对期望进行断言。

NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11

ぃ、小莉子 提交于 2019-11-30 13:41:33
问题 Another instance of the NoSuchMethodError for the JUnit & Hamcrest combination. Offending code: assertThat(dirReader.document(0).getFields(), hasItem( new FeatureMatcher<IndexableField, String>(equalTo("Patisnummer"), "Field key", "Field key") { @Override protected String featureValueOf(IndexableField actual) { return actual.name(); } } )); Commented lines 152–157 in IndexerTest.java (commit ac72ce) Causes a NoSuchMethodError (see http://db.tt/qkkkTE78 for complete output): java.lang

Composer hanging while updating dependencies

巧了我就是萌 提交于 2019-11-30 11:13:31
I tried updating a Laravel project I'm working on today using composer update But it hung on Updating dependencies (including require-dev) So I tried things like updating composer, dump-autoload, but nothing seemed to work. Then I ran it in verbose mode: composer update -vvv And I noticed it hung while reading this json: Reading path/to/Composer/repo/https---packagist.org/provider-cordoval$hamcrest-php.json from cache I tried searching for cordoval/hamcrest-php on packagist.org and couldn't find it. This isn't listed as a dependency in my composer.json Searching through my vendor folder, I

Ant + JUnit: NoClassDefFoundError

断了今生、忘了曾经 提交于 2019-11-30 10:53:34
Ok, I'm frustrated! I've hunted around for a good number of hours and am still stumped. Environment: WinXP, Eclipse Galileo 3.5 (straight install - no extra plugins). So, I have a simple JUnit test. It runs fine from it's internal Eclipse JUnit run configuration. This class has no dependencies on anything. To narrow this problem down as much as possible it simply contains: @Test public void testX() { assertEquals("1", new Integer(1).toString()); } No sweat so far. Now I want to take the super advanced step of running this test case from within Ant (the final goal is to integrate with Hudson).

Is there a Hamcrest “for each” Matcher that asserts all elements of a Collection or Iterable match a single specific Matcher?

随声附和 提交于 2019-11-30 10:41:36
Given a Collection or Iterable of items, is there any Matcher (or combination of matchers) that will assert every item matches a single Matcher ? For example, given this item type: public interface Person { public String getGender(); } I'd like to write an assertion that all items in a collection of Person s have a specific gender value. I'm thinking something like this: Iterable<Person> people = ...; assertThat(people, each(hasProperty("gender", "Male"))); Is there any way to do this without writing the each matcher myself? Use the Every matcher. import org.hamcrest.beans.HasPropertyWithValue

NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11

限于喜欢 提交于 2019-11-30 07:49:56
Another instance of the NoSuchMethodError for the JUnit & Hamcrest combination. Offending code: assertThat(dirReader.document(0).getFields(), hasItem( new FeatureMatcher<IndexableField, String>(equalTo("Patisnummer"), "Field key", "Field key") { @Override protected String featureValueOf(IndexableField actual) { return actual.name(); } } )); Commented lines 152–157 in IndexerTest.java (commit ac72ce ) Causes a NoSuchMethodError (see http://db.tt/qkkkTE78 for complete output): java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V at org

Is org.junit.Assert.assertThat better than org.hamcrest.MatcherAssert.assertThat?

非 Y 不嫁゛ 提交于 2019-11-30 06:45:15
I'm new to JUnit and Hamcrest and would like best-practice advice so I can decided which documentation to study first. For starters, which of these assertThat methods is better? org.junit.Assert.assertThat (from junit-4.11.jar) org.hamcrest.MatcherAssert.assertThat (from hamcrest-core-1.3.jar) According to one person last year, "JUnit has the assertThat method, but hamcrest has its own assertThat method that does the same thing." . According to someone earlier this year, Hamcrest "could potentially give better error messages because the matcher is called to describe the mismatch" . It's hard

How to check if collection contains items in given order using Hamcrest

自古美人都是妖i 提交于 2019-11-30 05:36:27
How to check using Hamcrest if given collection is containing given items in given order? I tried hasItems but it simply ignores the order. List<String> list = Arrays.asList("foo", "bar", "boo"); assertThat(list, hasItems("foo", "boo")); //I want this to fail, because the order is different than in "list" assertThat(list, hasItems("boo", "foo")); You can use contains matcher instead, but you probably need to use latest version of Hamcrest. That method checks the order. assertThat(list, contains("foo", "boo")); You can also try using containsInAnyOrder if order does not matter to you. That's

Mockito 2 参数匹配器

跟風遠走 提交于 2019-11-30 05:29:00
Mockito 通过使用 equals() 这种自然的 Java 样式来校验参数值。有时候,当需要有其他一些灵活性的时候,你可能会要求使用参数匹配(argument matchers)。 请参考下面的代码: //stubbing using built-in anyInt() argument matcher when(mockedList.get(anyInt())).thenReturn( "element" ); //stubbing using custom matcher (let's say isValid() returns your own matcher implementation): when(mockedList.contains(argThat(isValid()))).thenReturn( "element" ); //following prints "element" System.out.println(mockedList.get( 999 )); //you can also verify using an argument matcher verify(mockedList).get(anyInt()); //argument matchers can also be written as Java 8 Lambdas verify