Skip to content

Commit 288df7b

Browse files
pimterryjoel-costigliola
authored andcommitted
Fixes assertj#308 : Add asString() to switch to String assertions
1 parent b7379fc commit 288df7b

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/main/java/org/assertj/core/api/AbstractAssert.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ public S isNotOfAnyClassIn(Class<?>... types) {
361361
return Assertions.assertThat((List<Object>) actual);
362362
}
363363

364+
/** {@inheritDoc} */
365+
@Override
366+
public AbstractCharSequenceAssert<?, String> asString() {
367+
objects.assertIsInstanceOf(info, actual, String.class);
368+
return Assertions.assertThat((String) actual);
369+
}
370+
364371
/**
365372
* The description of this assertion set with {@link #describedAs(String, Object...)} or {@link #describedAs(Description)}.
366373
*

src/main/java/org/assertj/core/api/Assert.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,23 @@ public interface Assert<S extends Assert<S, A>, A> extends Descriptable<S>, Exte
259259
*/
260260
AbstractListAssert asList();
261261

262+
/**
263+
* Verifies that the actual value is an instance of String,
264+
* and returns a String assertion, to allow chaining of String-specific
265+
* assertions from this call.
266+
* <p>
267+
* Example :
268+
*
269+
* <pre><code class='java'>
270+
* Object stringAsObject = "hello world";
271+
*
272+
* assertThat(stringAsObject).asString().contains("hello");
273+
* </code></pre>
274+
*
275+
* @return a string assertion object
276+
*/
277+
AbstractCharSequenceAssert<?, String> asString();
278+
262279
/**
263280
* @deprecated
264281
* Throws <code>{@link UnsupportedOperationException}</code> if called. It is easy to accidentally call
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2014 the original author or authors.
12+
*/
13+
package org.assertj.core.api;
14+
15+
import org.assertj.core.test.ExpectedException;
16+
import org.junit.Rule;
17+
import org.junit.Test;
18+
19+
import java.util.Arrays;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.test.ExpectedException.none;
23+
24+
/**
25+
* Tests for Assert.asString() methods
26+
*/
27+
public class Assertions_assertThat_asString {
28+
29+
@Rule
30+
public ExpectedException thrown = none();
31+
32+
@Test
33+
public void should_pass_string_asserts_on_string_objects_with_asString() {
34+
Object stringAsObject = "hello world";
35+
assertThat(stringAsObject).asString().contains("hello");
36+
}
37+
38+
@Test
39+
public void should_fail_string_asserts_on_non_string_objects_even_with_asString() {
40+
Object nonString = new Object();
41+
42+
thrown.expectAssertionError("an instance of:\n <java.lang.String>\nbut was instance of:\n <java.lang.Object>");
43+
assertThat(nonString).asString().contains("hello");
44+
}
45+
46+
}

0 commit comments

Comments
 (0)