Question

I've been struggling for the past days to write some tests to check if my deserialization code for some classes is working.

I have a java package of REST services class that communicates with our API, via Retrofit. My idea was to use Robolectric FakeHttpLayer to supply certain responses and write the set of tests that check that my service classes effectively create the Model objects from JSON responses.

  • It seems that robolectric isn't intercepting the network calls (i.e. I always get live data from our API). I'm not sure why is that.
  • Is there any other approach to testing what I've written so far? Its really important for me to have this layer tested, so the person who's writing the logic layer of our app has something reliable to work with, while I move to work on other parts of the app.

Thanks!

Was it helpful?

Solution

Test these separately!

  1. Serialization should be tested directly with your serialization mechanism of choice.

    For example, if you are using JSON and Gson, have canned JSON strings that you deserialize and verify the result of.

  2. Behavior based on network responses can be tested by using fake implementations of the Retrofit interfaces that you use in the tests rather than calling RestAdapter.create.

    class FakeFoo implements Foo { // Foo is your Retrofit interface
      @Override public FooResponse doSomething(FooRequest request) {
        return new FooResponse("Fake", "Data");
      }
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top