0

I have a method which does following.

public void callService(SomeObject someObject) {

// call helper class method and create a request XML

// scrub this XML using a local method and persist it in MongoDB

// call a 3rd party service using HTTP POST

// Recieve the response

// Persist the response in MongoDB and set in in somObject 

// return

}

Now as part of development we have to write unit test cases for this method. I am new to Junit testing as well as mock objects. but when I googled and looked at the some other similar questions I understood that testing void method is little bit different than normal methods and I think my above method which special in some more way as I am clueless as to what and how to test for this method.

Can someone please give me pointer or any reference as to how I can unit test this method using Junit.

Shantanoo K
  • 765
  • 5
  • 15
  • 43
  • 2
    The method does too much. You'd mock the MongoDB call for a unit test, though. – Dave Newton Mar 10 '15 at 01:28
  • You probably want to use mocks to stand in for MongoDB and the third party service. (you don't want to actually persist things in a unit test). Mocks are objects with the same API as hard-to-use-in-unit-test things like database connections; you'd set up mocks and make sure that the correct data ended up stored in your fake MongoDB and POSTed to your fake third part service. – fzzfzzfzz Mar 10 '15 at 01:29
  • Can you please give me an example. That will really help me getting started on this. – Shantanoo K Mar 10 '15 at 01:30
  • +1 To Dave's comment. According to the description this method should be broken to 6 methods (at least)... Have you heard about [SRP](http://en.wikipedia.org/wiki/Single_responsibility_principle) ? – Nir Alfasi Mar 10 '15 at 01:33
  • possible duplicate of [How to test void method with Junit testing tools?](http://stackoverflow.com/questions/1244541/how-to-test-void-method-with-junit-testing-tools) – kryger Mar 10 '15 at 12:24

1 Answers1

0

You'd probably want to use mocks to stand in for your Mongo connection and the third party service. It's easiest to use an existing mock framework, but this is the general concept.

Pretend that you post to this third party service by constructing a StuffToPost object and passing it to the post method on your ThirdPartyPoster. Then you can create a mock object as follows:

public class MockThirdPartyPoster implements ThirdPartyPoster {
    private int count = 0;
    private StuffToPost stuffToPost;


    @Override
    public void post(StuffToPost stuffToPost) {
        this.count++;
        this.stuffToPost = stuffToPost;
    }

    public int getCount() {
        return count;
    }

    public StuffToPost getStuffToPost() {
        return stuffToPost;
    }
}

In your test, you'd construct this MockThirdPartyPoster and pass it to thingToTest.setThirdPartyPoster, then call your method. Once the method finishes executing, you can call getCount() on the mock to make sure that you POSTed once and only once, and call getStuffToPost() to examine the StuffToPost object and make sure that it is correct. You'd do something similar for Mongo persistence as well.

That calls for a lot of boilerplate; mock frameworks like Mockito or EasyMock exist to solve that problem.

fzzfzzfzz
  • 1,248
  • 1
  • 12
  • 22