java Hamcrest Matchers

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Hamcrest Matchers相关的知识,希望对你有一定的参考价值。

import java.util.Date;
import java.util.List;

import org.hamcrest.CustomTypeSafeMatcher;
import org.hamcrest.Matcher;

import com.eyalgo.rssreader.model.FeedData;
import com.eyalgo.rssreader.model.FeedItem;

public class Matchers {
    public static Matcher<FeedData> sameFeedData(String expectedFeedUrl, List<FeedItem> expectedItems) {
	return new FeedDataMetcher(expectedFeedUrl, expectedItems);
    }

    public static Matcher<FeedItem> sameFeedItem(String title, String link, Date date) {
	return new FeedItemMetcher(title, link, date);
    }

    private final static class FeedItemMetcher extends CustomTypeSafeMatcher<FeedItem> {

	private final String expectedTitle;
	private final String expectedLink;
	private final Date expectedDate;

	private FeedItemMetcher(String title, String link, Date date) {
	    super("a FeedItem Matcher");
	    this.expectedTitle = title;
	    this.expectedLink = link;
	    this.expectedDate = date;
	}

	@Override
	protected boolean matchesSafely(FeedItem feedItem) {
	    if (!feedItem.getTitle().equals(expectedTitle)) {
		return false;
	    }
	    if (!feedItem.getLink().equals(expectedLink)) {
		return false;
	    }
	    if (!feedItem.getPublishedDate().equals(expectedDate)) {
		return false;
	    }
	    return true;
	}

    }

    private final static class FeedDataMetcher extends CustomTypeSafeMatcher<FeedData> {
	private final String expectedFeedUrl;
	private final List<FeedItem> expectedItems;

	private FeedDataMetcher(String expectedFeedUrl, List<FeedItem> expectedItems) {
	    super("a FeedData Matcher");
	    this.expectedFeedUrl = expectedFeedUrl;
	    this.expectedItems = expectedItems;
	}

	@Override
	protected boolean matchesSafely(FeedData feedData) {
	    if (!feedData.getFeedUrl().equals(expectedFeedUrl)) {
		return false;
	    }
	    if (!feedData.getItems().equals(expectedItems)) {
		return false;
	    }
	    return true;
	}

    }

}

以上是关于java Hamcrest Matchers的主要内容,如果未能解决你的问题,请参考以下文章

测试错误 - NoClassDefFoundError:解析失败:Lorg/hamcrest/Matchers

为什么我们的Hamcrest断言不被认为是规则S2699的有效断言?

Junit4 断言新方法

Junit4 断言新方法

Junit4中的新断言assertThat的是用方法

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribeing