DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Anti-Pattern for Testing RxJava Code

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.

Liviu Tudor user avatar by
Liviu Tudor
·
Jun. 06, 18 · Tutorial
Like (2)
Save
Tweet
Share
8.82K Views

Join the DZone community and get the full member experience.

Join For Free

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.

unit test Anti-pattern

Published at DZone with permission of Liviu Tudor, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Develop a Portrait Retouching Function
  • Public Cloud-to-Cloud Repatriation Trend
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Visual Network Mapping Your K8s Clusters To Assess Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: