Skip to content

Commit

Permalink
(#1039) Replaced @rule with Throws matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
atapin committed Apr 15, 2019
1 parent a760506 commit 38e0c43
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 187 deletions.
36 changes: 20 additions & 16 deletions src/test/java/org/cactoos/ScalarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
package org.cactoos;

import org.cactoos.scalar.NoNulls;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link NoNulls}.
Expand All @@ -35,24 +35,28 @@
*/
public final class ScalarTest {

/**
* A rule for handling an exception.
*/
@Rule
public final ExpectedException cause = ExpectedException.none();

@Test
public void failForNullArgument() throws Exception {
this.cause.expect(IllegalArgumentException.class);
this.cause.expectMessage("NULL instead of a valid scalar");
new NoNulls<>(null).value();
public void failForNullArgument() {
new Assertion<>(
"Must fail for null argument",
() -> new NoNulls<>(null).value(),
new Throws<>(
"NULL instead of a valid scalar",
IllegalArgumentException.class
)
).affirm();
}

@Test
public void failForNullResult() throws Exception {
this.cause.expect(IllegalStateException.class);
this.cause.expectMessage("NULL instead of a valid value");
new NoNulls<>(() -> null).value();
public void failForNullResult() {
new Assertion<>(
"Must fail for null result",
() -> new NoNulls<>(() -> null).value(),
new Throws<>(
"NULL instead of a valid value",
IllegalStateException.class
)
).affirm();
}

@Test
Expand Down
41 changes: 20 additions & 21 deletions src/test/java/org/cactoos/TextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,40 @@
import org.cactoos.text.NoNulls;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.TextHasString;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link Text}.
* @since 0.11
* @todo #1023:30min Replace all occurrences of @Rule ExpectedException
* tests that use it should be refactored to use Throws class
* introduced in cactoos-matchers 0.13.
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class TextTest {

/**
* A rule for handling an exception.
*/
@Rule
public final ExpectedException cause = ExpectedException.none();

@Test
public void failForNullArgument() throws Exception {
this.cause.expect(IllegalArgumentException.class);
this.cause.expectMessage("NULL instead of a valid text");
new NoNulls(null).asString();
public void failForNullArgument() {
new Assertion<>(
"Must fail for null argument",
() -> new NoNulls(null).asString(),
new Throws<>(
"NULL instead of a valid text",
IllegalArgumentException.class
)
).affirm();
}

@Test
public void failForNullResult() throws Exception {
this.cause.expect(IllegalStateException.class);
this.cause.expectMessage("NULL instead of a valid result string");
new NoNulls(
() -> null
).asString();
public void failForNullResult() {
new Assertion<>(
"Must fail for null result",
() -> new NoNulls(() -> null).asString(),
new Throws<>(
"NULL instead of a valid result string",
IllegalStateException.class
)
).affirm();
}

@Test
Expand Down
61 changes: 32 additions & 29 deletions src/test/java/org/cactoos/collection/NoNullsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
package org.cactoos.collection;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test cases for {@link NoNulls}.
Expand All @@ -38,42 +38,45 @@
*/
public final class NoNullsTest {

/**
* A rule for handling an exception.
*/
@Rule
public final ExpectedException exception = ExpectedException.none();

@Test
public void throwsErrorIfNullInToArray() {
this.exception.expect(IllegalStateException.class);
this.exception.expectMessage(
"Item #1 of #toArray() is NULL"
);
new NoNulls<>(
new CollectionOf<>(1, null, 3)
).toArray();
new Assertion<>(
"Must throw exception",
() -> new NoNulls<>(
new CollectionOf<>(1, null, 3)
).toArray(),
new Throws<>(
"Item #1 of #toArray() is NULL",
IllegalStateException.class
)
).affirm();
}

@Test
public void throwsErrorIfNullInToArrayWithArg() {
this.exception.expect(IllegalStateException.class);
this.exception.expectMessage(
"Item #1 of #toArray(array) is NULL"
);
new NoNulls<>(
new CollectionOf<>(1, null, 3)
).toArray(new Object[3]);
new Assertion<>(
"Must throw exception for the item#1",
() -> new NoNulls<>(
new CollectionOf<>(1, null, 3)
).toArray(new Object[3]),
new Throws<>(
"Item #1 of #toArray(array) is NULL",
IllegalStateException.class
)
).affirm();
}

@Test
public void throwsErrorIfNullInContainsArg() {
this.exception.expect(IllegalArgumentException.class);
this.exception.expectMessage(
"Argument of #contains(T) is NULL"
);
new NoNulls<>(
new CollectionOf<>(1, 2, 3)
).contains(null);
new Assertion<>(
"Must throw exception for #contains(null)",
() -> new NoNulls<>(
new CollectionOf<>(1, 2, 3)
).contains(null),
new Throws<>(
"Argument of #contains(T) is NULL",
IllegalArgumentException.class
)
).affirm();
}
}
60 changes: 31 additions & 29 deletions src/test/java/org/cactoos/iterator/NoNullsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
package org.cactoos.iterator;

import java.util.Iterator;
import org.cactoos.iterable.IterableOf;
import org.hamcrest.Matcher;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.StringEndsWith;
import org.hamcrest.core.StringStartsWith;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test cases for {@link NoNulls}.
Expand All @@ -51,34 +48,39 @@ public final class NoNullsTest {

@Test
public void nextThrowsErrorIfNull() {
this.exception.expect(IllegalStateException.class);
this.exception.expectMessage(
new AllOf<>(
new IterableOf<Matcher<? super String>>(
new StringStartsWith(
"Item #0 of org.cactoos.iterator"
),
new StringEndsWith(
"is NULL"
)
)
)
);
new NoNulls<>(
new Iterator<Integer>() {
@Override
public boolean hasNext() {
return true;
}
new Assertion<>(
"Must throw exception",
() -> new NoNulls<>(
new Iterator<Integer>() {
@Override
public boolean hasNext() {
return true;
}

@Override
public Integer next() {
return null;
}

@Override
public Integer next() {
return null;
@Override
public String toString() {
return "Iterator@NoNullsTest";
}
}
}
).next();
).next(),
new Throws<>(
"Item #0 of Iterator@NoNullsTest is NULL",
IllegalStateException.class
)
).affirm();
}

/*
* @todo #1039:15min Currently it's impossible to match error messages
* by a pattern or partially. Replace {@link Rule} with {@link Throws} after
* <a href="https://github.com/llorllale/cactoos-matchers/issues/108">llorllale/cactoos-matchers#108</a>
* is fixed
*/
@Test
public void nthThrowsErrorIfNull() {
this.exception.expect(IllegalStateException.class);
Expand Down
Loading

0 comments on commit 38e0c43

Please sign in to comment.