Skip to content

Commit

Permalink
Enabled fluent String assertions for non-String objects with asString…
Browse files Browse the repository at this point in the history
…() (#1307).

This is a breaking change for those that depended on asString() to
assert that actual is an instance of String.  It now only asserts that
actual is not null.  Those needing that functionality can use
`assertThat(actual).isInstanceOf(String.class).asString()`.
  • Loading branch information
ianbrandt authored and joel-costigliola committed Sep 9, 2018
1 parent db15704 commit f246373
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/assertj/core/api/AbstractAssert.java
Expand Up @@ -456,8 +456,8 @@ public AbstractListAssert<?, List<? extends Object>, Object, ObjectAssert<Object
@Override
@CheckReturnValue
public AbstractStringAssert<?> asString() {
objects.assertIsInstanceOf(info, actual, String.class);
return Assertions.assertThat((String) actual);
objects.assertNotNull(info, actual);
return Assertions.assertThat(actual.toString());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/assertj/core/api/Assert.java
Expand Up @@ -559,9 +559,8 @@ public interface Assert<SELF extends Assert<SELF, ACTUAL>, ACTUAL> extends Descr
AbstractListAssert<?, List<? extends Object>, Object, ObjectAssert<Object>> asList();

/**
* Verifies that the actual value is an instance of String,
* and returns a String assertion, to allow chaining of String-specific
* assertions from this call.
* Returns a String assertion for the <code>toString()</code> of the actual
* value, to allow chaining of String-specific assertions from this call.
* <p>
* Example :
* <pre><code class='java'> Object stringAsObject = "hello world";
Expand Down
Expand Up @@ -12,7 +12,6 @@
*/
package org.assertj.core.api;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

Expand All @@ -25,16 +24,28 @@ public class Assertions_assertThat_asString_Test {

@Test
public void should_pass_string_asserts_on_string_objects_with_asString() {
Object stringAsObject = "hello world";
assertThat(stringAsObject).asString().contains("hello");
Object stringAsObject = "hello world";
assertThat(stringAsObject).asString().contains("hello");
}

@Test
public void should_fail_string_asserts_on_non_string_objects_even_with_asString() {
public void should_pass_string_asserts_on_non_string_objects_with_asString() {
Object nonString = new Object();
assertThat(nonString).asString().isEqualTo(nonString.toString());
}

@Test
public void should_fail_string_asserts_on_non_string_objects_with_asString() {
Object nonString = new Object();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(nonString).asString()
.contains("probably not this"));
}

assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(nonString).asString().contains("hello"))
.withMessageContaining(format("an instance of:%n <java.lang.String>%nbut was instance of:%n <java.lang.Object>"));
@Test
public void should_fail_if_actual_is_null_with_asString() {
Object nullObject = null;
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(nullObject).asString()
.isEqualTo("never gonna happen"));
}

}

0 comments on commit f246373

Please sign in to comment.