项目作者: devopsix

项目描述 :
Hamcrest Mail
高级语言: Java
项目地址: git://github.com/devopsix/hamcrest-mail.git
创建时间: 2019-12-01T17:49:08Z
项目社区:https://github.com/devopsix/hamcrest-mail

开源协议:MIT License

下载


Build Status
Maven Central
License

Hamcrest Mail

Hamcrest Mail is an extension library for the Java Hamcrest matcher library.
It provides matchers for types from the javax.mail and jakarta.mail packages.

The assertj-mail sister project provides a set of AssertJ assertions with similar features.

Usage

To use Hamcrest Mail in a Maven project add a dependency on org.devopsix:hamcrest-mail (for Java EE 8 / javax.mail) or org.devopsix:hamcrest-mail-jakarta (for Jakarta EE 9+ / jakarta.mail) to the pom.xml file.

  1. <!-- Maven coordinates for Java EE 8 / javax.mail -->
  2. <dependency>
  3. <groupId>org.devopsix</groupId>
  4. <artifactId>hamcrest-mail</artifactId>
  5. <version>2.0.8</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <!-- Maven coordinates for Jakarta EE 9+ / jakarta.mail -->
  9. <dependency>
  10. <groupId>org.devopsix</groupId>
  11. <artifactId>hamcrest-mail-jakarta</artifactId>
  12. <version>2.0.8</version>
  13. <scope>test</scope>
  14. </dependency>

The matchers are available as static methods on the MailMatchers class.

Here are a few examples:

  1. Message message;
  2. Assert.assertThat(message, MailMatchers.hasFrom("anna@example.com"));
  3. Assert.assertThat(message, MailMatchers.hasTo("bob@example.com"));
  4. Assert.assertThat(message, MailMatchers.hasRecipients(Matchers.iterableWithSize(1)));
  5. Assert.assertThat(message, MailMatchers.hasSubject("Message from Anna"));
  6. Assert.assertThat(message, MailMatchers.hasHeader("Return-Path", Matchers.notNullValue()));
  7. Assert.assertThat(message, MailMatchers.hasDateHeader("Resent-Date", Matchers.isA(OffsetDateTime.class)));
  8. // OffsetDateTimeMatchers is from eXparity/hamcrest-date:
  9. // https://github.com/eXparity/hamcrest-date
  10. Assert.assertThat(message, MailMatchers.hasDate(OffsetDateTimeMatchers.within(1, MINUTES, now())));
  11. // Casting to Matcher is required when a matcher's signature is
  12. // Matcher<Iterable<? extends T>> or Matcher<Iterable<? super T>>
  13. Assert.assertThat(message, MailMatchers.hasHeaders("Received", (Matcher)Matchers.hasItems(
  14. Matchers.containsString("host1"), Matchers.containsString("host2"))));

More example can be found in the examples directory (for Java EE 8 / javax.mail) and in the examples-jakarta directory (for Jakarta EE 9+ / jakarta.mail).

Matchers

  • hasDate - Tests the “Date” header against a given date or matcher
  • hasFrom - Tests the “From” header against a given string or matcher
  • hasSender - Tests the “Sender” header against a given string or matcher
  • hasReplyTo - Tests the “Reply-To” header against a given string or matcher
  • hasTo - Tests the “To” header against a given string or matcher
  • hasCc - Tests the “Cc” header against a given string or matcher
  • hasBcc - Tests the “Bcc” header against a given string or matcher
  • hasRecipients - Tests a message’s recipients against a given matcher
  • hasAddress - Tests an InternetAddress’ address against a given matcher
  • hasPersonal - Tests an InternetAddress’ personal name against a given matcher
  • hasSubject - Tests the “Subject” header against a given string or matcher
  • hasHeader - Tests a named header against a given string or matcher
  • hasHeaders - Tests a named header which may occur multiple times against a given matcher
  • hasDateHeader - Tests a named date header against a given date or matcher
  • hasDateHeaders - Tests a named date header which may occur multiple times against a given matcher
  • hasValidDkimSignature - Tests a message for having a valid DKIM signature
  • hasTextContent - Tests a message or a part of a multipart message for having plain text content matching a given matcher
  • hasBinaryContent - Tests a message or a part of a multipart message for having binary content matching a given matcher
  • hasMultipartContent - Tests a message or a part of a multipart message for having multipart content matching a given matcher
  • hasMultipartContentRecursive - Tests a message or a part of a multipart message and recursively all child parts thereof for having at least one part with multipart content matching a given matcher
  • multipartMixed - Tests a multipart for having “multipart/mixed” content type
  • multipartAlternative - Tests a multipart for having “multipart/alternative” content type
  • multipartRelated - Tests a multipart for having “multipart/related” content type
  • multipartContentType - Tests a multipart’s content type against a given matcher
  • hasPart - Tests a multipart for having at least one part matching a given matcher
  • hasParts - Tests a multipart for having parts matching a given matcher