├── settings.gradle
├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .travis.yml
├── samples
├── build.gradle
└── src
│ └── test
│ └── java
│ └── BasicSamplesTest.java
├── src
├── main
│ └── java
│ │ └── com
│ │ └── petertackage
│ │ └── assertrx
│ │ ├── Assertions.java
│ │ └── TestSubscriberAssert.java
└── test
│ └── java
│ └── com
│ └── petertackage
│ └── assertrx
│ ├── TestSubscriberAssertDelegatedTest.java
│ └── TestSubscriberAssertTest.java
├── gradlew.bat
├── gradlew
└── README.md
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'assert-rx'
2 | include ':samples'
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 |
3 | *.class
4 | *.iml
5 |
6 | .idea/
7 |
8 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/peter-tackage/assert-rx/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 |
3 | before_cache:
4 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
5 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
6 | cache:
7 | directories:
8 | - $HOME/.gradle/caches/
9 | - $HOME/.gradle/wrapper/
10 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Apr 30 18:37:44 CEST 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/samples/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | compileJava.setSourceCompatibility('1.8')
4 | compileJava.setTargetCompatibility('1.8')
5 |
6 | repositories {
7 | mavenCentral()
8 | maven { url "https://jitpack.io" }
9 | }
10 |
11 | dependencies {
12 | compile project(':')
13 | testCompile 'junit:junit:4.12'
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/petertackage/assertrx/Assertions.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2016 Peter Tackage
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.petertackage.assertrx;
17 |
18 | import rx.observers.TestSubscriber;
19 |
20 | /**
21 | * Entry point for performing {@link TestSubscriber} assertions.
22 | */
23 | public final class Assertions {
24 |
25 | /**
26 | * Creates a new instance of {@link TestSubscriberAssert}.
27 | *
28 | * @param actual the actual value.
29 | * @return the created assertion object.
30 | */
31 | public static TestSubscriberAssert assertThat(final TestSubscriber actual) {
32 | return new TestSubscriberAssert(actual, TestSubscriberAssert.class);
33 | }
34 |
35 | private Assertions() {
36 | throw new AssertionError("No instances.");
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/samples/src/test/java/BasicSamplesTest.java:
--------------------------------------------------------------------------------
1 | import org.junit.Ignore;
2 | import org.junit.Test;
3 | import rx.Observable;
4 | import rx.functions.Func1;
5 | import rx.observers.TestSubscriber;
6 |
7 | import java.util.concurrent.TimeUnit;
8 |
9 | import static com.petertackage.assertrx.Assertions.assertThat;
10 |
11 | /**
12 | * Some more real-world style test samples.
13 | *
14 | * Remove @Ignore annotations to experiment.
15 | */
16 | public class BasicSamplesTest {
17 |
18 | @Test
19 | public void testDelay() {
20 | // Not an ideal way of testing Observables with concurrency because it will still take 2 seconds!
21 | String value = "avalue";
22 | Observable delayObservable = Observable.just(value).delay(2, TimeUnit.SECONDS);
23 |
24 | TestSubscriber ts = TestSubscriber.create();
25 | delayObservable.subscribe(ts);
26 |
27 | assertThat(ts)
28 | .afterTerminalEvent()
29 | .hasReceivedValue(value);
30 | }
31 |
32 | @Test
33 | public void testFiltering() {
34 | String value = "catalog";
35 | String value2 = "caterpillar";
36 | String value3 = "dog";
37 | Observable filteredObservable = Observable.just(value, value2, value3)
38 | .filter(new Func1() {
39 | @Override
40 | public Boolean call(String s) {
41 | return s.startsWith("cat");
42 | }
43 | });
44 |
45 | TestSubscriber ts = TestSubscriber.create();
46 | filteredObservable.subscribe(ts);
47 |
48 | assertThat(ts)
49 | .hasNoErrors()
50 | .hasReceivedValuesWhich()
51 | .containsOnly(value, value2);
52 | }
53 |
54 | @Test
55 | @Ignore("This test is intentionally failing - ignored so that the build passes")
56 | public void testFilteringEmitsUnexpectedValue() {
57 | String value = "catalog";
58 | String value2 = "caterpillar";
59 | String value3 = "dog";
60 | Observable filteredObservable = Observable.just(value, value2, value3)
61 | .filter(new Func1() {
62 | @Override
63 | public Boolean call(String s) {
64 | return s.startsWith("cat");
65 | }
66 | });
67 |
68 | TestSubscriber ts = TestSubscriber.create();
69 | filteredObservable.subscribe(ts);
70 |
71 | assertThat(ts)
72 | .hasNoErrors()
73 | .hasReceivedValuesWhich()
74 | .containsSequence(value, value2, value3);
75 | }
76 |
77 | @Test
78 | @Ignore("This test is intentionally failing - ignored so that the build passes")
79 | public void testFilteringErrors() {
80 | String value = "catalog";
81 | Observable erroringFilteringObservable = Observable.just(value).
82 | concatWith(Observable.error(new NullPointerException()));
83 |
84 | TestSubscriber ts = TestSubscriber.create();
85 | erroringFilteringObservable.subscribe(ts);
86 |
87 | assertThat(ts)
88 | .hasNoErrors()
89 | .hasReceivedValuesWhich()
90 | .containsOnly(value);
91 | }
92 |
93 | @Test
94 | @Ignore("This test is intentionally failing - ignored so that the build passes")
95 | public void testNeverErrors() {
96 | Observable empty = Observable.never();
97 |
98 | TestSubscriber ts = TestSubscriber.create();
99 | empty.subscribe(ts);
100 |
101 | assertThat(ts).hasTerminalEvent();
102 | }
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Assert-Rx
2 | =========
3 |
4 | DEPRECATED!
5 |
6 | As of RxJava 1.3, [fluent assertions will be part of the RxJava 1.x library](https://github.com/ReactiveX/RxJava/pull/4777/). You should use the official equivalent instead. The official assertions are assertion library agnostic and use the same API as RxJava 2.x, which should help you make the transition should you chose to.
7 |
8 | Only use this library if somehow you cannot to update to RxJava 1.3.
9 |
10 | Every library has its lifespan and this one's has ended. Thank you for your interest!
11 |
12 | A fluent assertion wrapper to simplify and improve usability of [RxJava's TestSubscriber](http://reactivex.io/RxJava/javadoc/rx/observers/TestSubscriber.html) assertions, built upon the [AssertJ](https://joel-costigliola.github.io/assertj/) framework.
13 |
14 | [](https://travis-ci.org/peter-tackage/assert-rx)
15 | [](https://jitpack.io/#peter-tackage/assert-rx)
16 | [](http://android-arsenal.com/details/1/4069)
17 | # Purpose
18 |
19 | Although `TestSubscriber` provides some very handy assertions to test your Observables, it can be a bit tedious and verbose to use. For example, to test the last value in a sequence you would typically write:
20 |
21 | ```java
22 | TestSubscriber ts = ...... // subscribe to your Observable with TestSubscriber
23 |
24 | ts.assertNoErrors();
25 | List values = ts.getOnNextEvents();
26 | assertThat(values.get(values.size() - 1)).isEqualTo("expectedValue");
27 | ```
28 |
29 | This library makes this more readable by allowing you to express assertions in a fluent style:
30 |
31 | ```java
32 | TestSubscriber ts = ...... // subscribe to your Observable with TestSubscriber
33 |
34 | assertThat(ts).hasNoErrors()
35 | .hasReceivedLastValue("expectedValue");
36 | ```
37 |
38 | In addition to this wrapping, it provides some higher order assertions to allow for testing of specific conditions.
39 |
40 | ```java
41 | assertThat(ts).hasReceivedFirstValueWhich()
42 | .is(notEmptyOrNull());
43 | ```
44 | Where `notEmptyOrNull` is a reusable, [AssertJ Condition](https://joel-costigliola.github.io/assertj/assertj-core-conditions.html).
45 |
46 | # Usage
47 |
48 | Here are a few examples of the assertions performed using Assert-Rx.
49 |
50 | ## OnNext Assertions
51 |
52 | Assert that the subscriber received any single onNext value:
53 |
54 | ```java
55 | assertThat(ts).hasReceivedAnyValue();
56 | ```
57 |
58 | Assert that the subscriber has received one or more onNext values:
59 |
60 | ```java
61 | assertThat(ts).hasReceivedAnyValues();
62 | ```
63 |
64 | Multiple, in-order values:
65 |
66 | ```java
67 | assertThat(ts).hasReceivedValues("a", "b", "c");
68 | ```
69 |
70 | Assert conditions for single onNext events (currently only as `Object` instances):
71 |
72 | ```java
73 | assertThat(ts).hasReceivedValueWhich()
74 | .is(notEmptyOrNull());
75 | ```
76 |
77 | Assert conditions for the entire onNext value sequence:
78 |
79 | ```java
80 | assertThat(ts).hasReceivedValuesWhich()
81 | .doesNotContain(someObject);
82 | ```
83 |
84 | Assert the first or last received onNext values:
85 |
86 | ```java
87 | assertThat(ts).hasReceivedFirstValue("the first value");
88 | ```
89 |
90 | ```java
91 | assertThat(ts).hasReceivedLastValue("the last value");
92 | ```
93 |
94 | ```java
95 | assertThat(ts).hasReceivedFirstValueWhich()
96 | .is(notEmptyOrNull());
97 | ```
98 |
99 | ```java
100 | assertThat(ts).hasReceivedLastValueWhich()
101 | .is(notEmptyOrNull());
102 | ```
103 |
104 | ## OnError Assertions
105 |
106 | Received an `IOException` instance in `onError`:
107 |
108 | ```java
109 | assertThat(ts).hasError(IOException.class);
110 | ```
111 |
112 |
113 | Assert conditions for onError events (currently only as `Throwable` instances):
114 |
115 | ```java
116 | assertThat(ts).hasErrorWhich()
117 | .hasMessageStartingWith("A terrible error");
118 | ```
119 |
120 | ## Concurrency Handling
121 |
122 | Handle concurrency, by ensuring that the `TestSubscriber` awaits a terminal event before asserting:
123 |
124 | ```java
125 | assertThat(ts).afterTerminalEvent()
126 | .hasNoErrors()
127 | .hasReceivedValue("someValue")
128 | .hasCompleted();
129 | ```
130 |
131 | Download
132 | --------
133 |
134 | Releases are available as dependencies via [Jitpack](https://jitpack.io/#peter-tackage/assert-rx/0.9.8).
135 |
136 | Implementation
137 | --------------
138 |
139 | The library is built as an extension to the [AssertJ](https://joel-costigliola.github.io/assertj/) framework, but to avoid duplication of the assertion logic in
140 | `TestSubscriber`, in most cases it simply calls through to the `TestSubscriber` assertion methods.
141 | The exceptions to this are the higher level assertions, which are provided by returning instances of AssertJ assertions.
142 |
143 | Acknowledgements
144 | ----------------
145 |
146 | Brought to you by the power of the [Chilicorn](http://spiceprogram.org/chilicorn-history/) and the [Futurice Open Source Program](http://spiceprogram.org/).
147 |
148 | 
149 |
150 | License
151 | =======
152 |
153 | Copyright 2016 Peter Tackage
154 |
155 | Licensed under the Apache License, Version 2.0 (the "License");
156 | you may not use this file except in compliance with the License.
157 | You may obtain a copy of the License at
158 |
159 | http://www.apache.org/licenses/LICENSE-2.0
160 |
161 | Unless required by applicable law or agreed to in writing, software
162 | distributed under the License is distributed on an "AS IS" BASIS,
163 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
164 | See the License for the specific language governing permissions and
165 | limitations under the License.
166 |
167 |
--------------------------------------------------------------------------------
/src/test/java/com/petertackage/assertrx/TestSubscriberAssertDelegatedTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2016 Peter Tackage
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.petertackage.assertrx;
17 |
18 | import org.junit.Rule;
19 | import org.junit.Test;
20 | import org.junit.rules.ExpectedException;
21 | import org.mockito.Matchers;
22 | import rx.Observable;
23 | import rx.observers.TestSubscriber;
24 |
25 | import java.util.Arrays;
26 | import java.util.Collections;
27 | import java.util.List;
28 | import java.util.concurrent.TimeUnit;
29 |
30 | import static org.mockito.Matchers.eq;
31 | import static org.mockito.Mockito.*;
32 |
33 | public class TestSubscriberAssertDelegatedTest {
34 |
35 | @Rule
36 | public ExpectedException thrown = ExpectedException.none();
37 |
38 | // Delegated tests - only test that the appropriate TestSubscriber assert method is invoked.
39 |
40 | @Test
41 | public void afterTerminalEvent_invokesTestSubscriberAwaitTerminalEvent() {
42 | //noinspection unchecked
43 | TestSubscriber ts = mock(TestSubscriber.class);
44 |
45 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).afterTerminalEvent();
46 |
47 | verify(ts).awaitTerminalEvent();
48 | }
49 |
50 | @Test
51 | public void afterTerminalEventWithTimeout_invokesTestSubscriberAwaitTerminalEvent() {
52 | //noinspection unchecked
53 | TestSubscriber ts = mock(TestSubscriber.class);
54 | long timeoutValue = 5;
55 | TimeUnit unitValue = TimeUnit.DAYS;
56 |
57 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).afterTerminalEvent(timeoutValue, unitValue);
58 |
59 | verify(ts).awaitTerminalEvent(eq(timeoutValue), eq(unitValue));
60 | }
61 |
62 | @Test
63 | public void afterTerminalEventAndUnsubscribingIfTimeout_invokesTestSubscriberAwaitTerminalEvent() {
64 | //noinspection unchecked
65 | TestSubscriber ts = mock(TestSubscriber.class);
66 | long timeoutValue = 5;
67 | TimeUnit unitValue = TimeUnit.DAYS;
68 |
69 | new TestSubscriberAssert(ts, TestSubscriberAssert.class)
70 | .afterTerminalEventAndUnsubscribingIfTimeout(timeoutValue, unitValue);
71 |
72 | verify(ts).awaitTerminalEventAndUnsubscribeOnTimeout(eq(timeoutValue), eq(unitValue));
73 | }
74 |
75 |
76 | @Test
77 | public void hasNoValues_invokesTestSubscribeAssertNoValues() {
78 | //noinspection unchecked
79 | TestSubscriber ts = mock(TestSubscriber.class);
80 |
81 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).hasNoValues();
82 |
83 | verify(ts).assertNoValues();
84 | }
85 |
86 | @Test
87 | public void hasReceivedValue_invokesTestSubscriberAssertValue() {
88 | Integer value = 1;
89 | Observable oi = Observable.just(value);
90 | //noinspection unchecked
91 | TestSubscriber ts = mock(TestSubscriber.class);
92 | oi.subscribe(ts);
93 |
94 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).hasReceivedValue(value);
95 |
96 | verify(ts).assertValue(eq(value));
97 | }
98 |
99 | @Test
100 | public void hasReceivedValues_invokesTestSubscriberAssertReceivedOnNextValue_whenMultiple() {
101 | List values = Arrays.asList(1, 2, 3);
102 | Observable oi = Observable.from(values);
103 | // Requires spy as decoration of TestSubscriber as Subscriber accesses TestSubscriber instance internals
104 | // which are not initialized when using a mock.
105 | TestSubscriber ts = spy(new TestSubscriber());
106 | oi.subscribe(ts);
107 |
108 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).hasReceivedValues(values);
109 |
110 | verify(ts).assertReceivedOnNext(eq(values));
111 | }
112 |
113 | @Test
114 | public void hasReceivedValuesVarArgs_invokesTestSubscriberAssertValues_whenMultiple() {
115 | Integer[] values = {1, 2, 3};
116 | Observable oi = Observable.from(values);
117 | //noinspection unchecked
118 | TestSubscriber ts = mock(TestSubscriber.class);
119 | oi.subscribe(ts);
120 |
121 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).hasReceivedValues(1, 2, 3);
122 |
123 | verify(ts).assertValues(Matchers.anyVararg());
124 | }
125 |
126 | @Test
127 | public void hasValueCount_invokesTestSubscriberAssertValueCount() {
128 | List values = Arrays.asList(1, 2, 3);
129 | Observable oi = Observable.from(values);
130 | // Requires spy as decoration of TestSubscriber as Subscriber accesses TestSubscriber instance internals
131 | // which are not initialized when using a mock.
132 | TestSubscriber ts = spy(new TestSubscriber());
133 | oi.subscribe(ts);
134 |
135 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).hasValueCount(values.size());
136 |
137 | verify(ts).assertValueCount(eq(values.size()));
138 | }
139 |
140 | @Test
141 | public void hasCompleted_invokesTestSubscriberAssertCompleted() {
142 | //noinspection unchecked
143 | TestSubscriber ts = mock(TestSubscriber.class);
144 |
145 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).hasCompleted();
146 |
147 | verify(ts).assertCompleted();
148 | }
149 |
150 | @Test
151 | public void hasNotCompleted_invokesTestSubscriberAssertNotCompleted() {
152 | //noinspection unchecked
153 | TestSubscriber ts = mock(TestSubscriber.class);
154 |
155 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).hasNotCompleted();
156 |
157 | verify(ts).assertNotCompleted();
158 | }
159 |
160 | @Test
161 | public void hasNoErrors_invokesTestSubscriberAssertNoErrors() {
162 | //noinspection unchecked
163 | TestSubscriber ts = mock(TestSubscriber.class);
164 |
165 | new TestSubscriberAssert(ts, TestSubscriberAssert.class).hasNoErrors();
166 |
167 | verify(ts).assertNoErrors();
168 | }
169 |
170 | @Test
171 | public void hasTerminalEvent_invokesTestSubscriberAssertTerminalEvent() {
172 | //noinspection unchecked
173 | TestSubscriber