Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Anti-Pattern for Testing RxJava Code

DZone's Guide to

Anti-Pattern for Testing RxJava Code

Let's check out a set of tests that actually leads to more problems than it solves. Here's an anti-pattern for RxJava testing and how to fix it.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

This is an obvious one, but I find myself so often using it — and every single time, it means I have to spend extra time debugging my test/code until I realize that I’ve fallen for the same mistake again. I’m using RxJava here and using Groovy and Spock for testing — and you could argue that this might be the crux of the issue, but I could bet good money that something similar can happen when writing pure Java/JUnit unit tests for an RxJava component, too.

Let’s start with a simple piece of code:

<pre lang=”java” escaped=”true”>
public class Process {
    private StringProcessor stringProcessor;

    public Process(StringProcessor stringProcessor) {
        this.stringProcessor = stringProcessor;
    }

    public Observable<String> process(int x) {
        return Observable.just(x)
            .map(String::valueOf)
            .map(stringProcessor::transform)
            .doOnNext(System.out::println);
    }
}

public interface StringProcessor {
    int transform(String s);
}


Nothing wrong with this code, right? It takes an integer and converts it to Stringand then uses the StringProcessor class to perform some string processing on this, then prints it out on the console.

Ok, now let’s write a Spock unit test for this. Part of this I don’t want to invoke the whole StringProcessor.transform() method — maybe because it’s too heavy for a test or because it might be hard to predict the output (for instance in cases where the implementation relies on time/date etc):

def "ensure zeros get replaced by 9"() {
    def i = 1020
    def proc = Mock(StringProcessor)
    def x = new Process(proc)

when:
    def r = x.process(i).toBlocking().single()

then:
    1 * proc.transform("$i")
}


Looking at the above, you can see that my unit test is interested just in the fact that the tested code converts the integer to string then passes the control to the StringProcessor. And to do so, it seems to do the right thing: It creates a mock for the StringProcessor and ensures that it is called exactly once with my integer converted to a string. Yet there’s something wrong with this code — and if you run it, you will get a NullPointerException!

The reason for it is simple — this line:

1 * proc.transform("$i")


It doesn’t return anything. Which is equivalent to returning (Observable<>)null — and then in my Process class, this null is trying to get applied a .doOnNext, which is where it all goes wrong.

The fix is actually trivial:

1 * proc.transform("$i") >> "abcdef" //<-- return here any string you need to return


Then, the .doOnNext goes through and your test succeeds. A very small change is needed yet, as I said, it created many long debugging sessions. If only writing this blog post would help with that.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ,rxjava ,anti-pattern ,software testing ,unit testing ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}