Repository: assertj/assertj

Problem statement:
Improve error message for "optional.containsInstanceOf(clazz)"
#### Summary

The error message for OptionalAssert.containsInstanceOf() is less descriptive than the similar message for AbstractAssert.isInstanceOf. Pull request is in the works.

#### Example

Before change:

```java
Expecting Optional to contain a value of type Number.
at ...
```

After change:

```java
Expecting:
 <Optional>
to contain a value that is an instance of:
 <class java.lang.Number>
but did contain instance of:
 <class java.lang.String>
at ...
```

Existing isInstanceOf-message:

```java
Expecting:
 <"StringValue">
to be an instance of:
 <java.lang.Number>
but was instance of:
 <java.lang.String>
```

#### Java 8 specific ?

* YES : create Pull Request from the `master` branch
* NO  : create Pull Request from the `2.x` branch





Hints:

Patch:
diff --git a/src/main/java/org/assertj/core/error/OptionalShouldContainInstanceOf.java b/src/main/java/org/assertj/core/error/OptionalShouldContainInstanceOf.java
--- a/src/main/java/org/assertj/core/error/OptionalShouldContainInstanceOf.java
+++ b/src/main/java/org/assertj/core/error/OptionalShouldContainInstanceOf.java
@@ -12,6 +12,8 @@
  */
 package org.assertj.core.error;
 
+import java.util.Optional;
+
 import static java.lang.String.format;
 
 /**
@@ -21,18 +23,25 @@
  */
 public class OptionalShouldContainInstanceOf extends BasicErrorMessageFactory {
 
-  private OptionalShouldContainInstanceOf(Object optional, Class<?> clazz) {
-    super(format("%nExpecting %s to contain a value of type %s.", optional.getClass().getSimpleName(), clazz.getName()));
+  private OptionalShouldContainInstanceOf(String message) {
+    super(message);
   }
 
   /**
    * Indicates that a value should be present in an empty {@link java.util.Optional}.
    *
-   * @param optional Optional to be checked.
+   * @param value Optional to be checked.
    * @return an error message factory.
    * @throws java.lang.NullPointerException if optional is null.
    */
-  public static OptionalShouldContainInstanceOf shouldContainInstanceOf(Object optional, Class<?> clazz) {
-    return new OptionalShouldContainInstanceOf(optional, clazz);
+  public static OptionalShouldContainInstanceOf shouldContainInstanceOf(Object value, Class<?> clazz) {
+    Optional optional = (Optional) value;
+    if (optional.isPresent()) {
+      return new OptionalShouldContainInstanceOf(format("%nExpecting:%n <%s>%nto contain a value that is an instance of:%n <%s>%nbut did contain an instance of:%n <%s>",
+        optional.getClass().getSimpleName(), clazz.getName(), optional.get().getClass().getName()));
+    } else {
+      return new OptionalShouldContainInstanceOf(format("%nExpecting:%n <%s>%nto contain a value that is an instance of:%n <%s>%nbut was empty",
+        optional.getClass().getSimpleName(), clazz.getName()));
+    }
   }
 }


Test patch:
diff --git a/src/test/java/org/assertj/core/error/OptionalShouldContainInstanceOf_create_Test.java b/src/test/java/org/assertj/core/error/OptionalShouldContainInstanceOf_create_Test.java
--- a/src/test/java/org/assertj/core/error/OptionalShouldContainInstanceOf_create_Test.java
+++ b/src/test/java/org/assertj/core/error/OptionalShouldContainInstanceOf_create_Test.java
@@ -23,8 +23,18 @@
 public class OptionalShouldContainInstanceOf_create_Test {
 
   @Test
-  public void should_create_error_message_with_expected_type() {
+  public void should_create_error_message_with_empty() {
     String errorMessage = shouldContainInstanceOf(Optional.empty(), Object.class).create();
-    assertThat(errorMessage).isEqualTo(format("%nExpecting Optional to contain a value of type java.lang.Object."));
+    assertThat(errorMessage).isEqualTo(format("%nExpecting:%n <Optional>%n" +
+      "to contain a value that is an instance of:%n <java.lang.Object>%n" +
+      "but was empty"));
+  }
+
+  @Test
+  public void should_create_error_message_with_expected_type() {
+    String errorMessage = shouldContainInstanceOf(Optional.of(Integer.MIN_VALUE), String.class).create();
+    assertThat(errorMessage).isEqualTo(format("%nExpecting:%n <Optional>%n" +
+      "to contain a value that is an instance of:%n <java.lang.String>%n" +
+      "but did contain an instance of:%n <java.lang.Integer>"));
   }
-}
\ No newline at end of file
+}