1

次の行は、2 つのリストを比較しようとすると失敗します。

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

assertThat(
    actualDealSummary.getDeals(), 
    hasItems(expectedDealSummary.getDeals().toArray(new Deal[expectedDealSummary.getDeals().size()])));

JUnit 4.11 と hamcrest-all (1.3) を使用:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
</dependency>

実際のリストに期待されるリストの値があることを確認したいだけですが、次のエラーが発生します。

java.lang.AssertionError:
予想: (<SIXRxrImUE> を含むコレクション、<RQjVbVRlyG> を含むコレクション、および <avnKxogdyN> を含むコレクション)
     しかし: <SIXRxrImUE> を含むコレクションは <SIXRxrImUE>、<RQjVbVRlyG>、<avnKxogdyN> でした
    org.hamcrest.MatcherAssert.assertThat (MatcherAssert.java:20) で
    org.hamcrest.MatcherAssert.assertThat (MatcherAssert.java:8) で
    で


hamcrest のビルトイン Matchers を使用して 2 つの pojos のリストを比較するにはどうすればよいですか?

4

1 に答える 1

0

これを機能Dealさせるには、クラスを実装する必要がありequalsます。等しいかどうかを確認する方が簡単な場合があります。

assertEquals(expectedDealSummary.getDeals(), actualDealSummary.getDeals());

hasItemsこれとは少し異なるチェックを実行し、実際のリストが予想されるリストのスーパーセットであることを確認していることに注意してください。これにより、予期しないアイテムが存在する場合でも、パスが発生します。

于 2014-02-22T15:54:33.097 に答える