-1
RestClient.getInstance().getAllLeads(6, new RestCallback<HashMap<String, Object>>(LeadsHttpAdapter.class) {
        @Override
        public void failure(RestError restError) {

        }

        @Override
        public void restSuccess(Object o) {
            ArrayList<Lead> LeadCardsList = (ArrayList) o;
            LeadsListAdapter leadsListAdapter = new LeadsListAdapter(getActivity(), LeadCardsList);
            LeadsListView.setAdapter(leadsListAdapter);
            LeadsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), LeadOfferDetails.class);
                    startActivity(intent);
                }
            });
        }
    });

This is written in my activity class function , I pass the RestClient as a parameter to that function so while unit testing the function i pass the mocked client . How do i test the callback , since by the time callback gets called my test function comes out of scope.

Samyak
  • 137
  • 2
  • 9
  • I would use `mockito` or `easymock`. You can mock the `restclient` and the `restcallback`. You can pass the `restcallback` to the `restclient` and `verify` that the callback was called by initiating your `mocked` `failure` or `restSuccess`. – Jared Burrows May 15 '15 at 17:00
  • @JaredBurrows I am not using mockito or easymock. Created my own custiom class to mock restclient. You can checkthat out here http://www.codeshare.io/KMRuF – Samyak May 15 '15 at 17:14
  • I know you are not using those testing suites, that is why I said "I would use", so you can mock return values and trigger interactions and then verify them. – Jared Burrows May 15 '15 at 17:19
  • @JaredBurrows Sorry to be so dumb but how would i do that. how would i trigger interactions and then verify? i am new to unit testing ? any examples – Samyak May 15 '15 at 17:34
  • Do not call yourself dumb! I am suggesting a different way of testing. I am not sure what `RestClient.getInstance().getAllLeads(` is. Something like this: http://stackoverflow.com/questions/10775254/how-to-use-mockito-for-testing-a-rest-service. – Jared Burrows May 15 '15 at 17:57

2 Answers2

0

I don't think you need a new test framework. Refactor the code inside restSuccess(Object o) to its own method. Place the new method in the outer class. This new method would be simple to test in jUnit. The rule of thumb is in any callback code to keep it very simple with only to one or two lines. You don't need to test if restSuccess(Object o)is called. You can assume it is called, test your code only, not the framework.

For extra credit refactor onItemClick() to its own class and write jUnit tests for it.

fishjd
  • 1,617
  • 1
  • 18
  • 31
0

I would use Mockito's Mockito.verify() and ArgumentCaptor.capture() to trigger callback.

If you are not familiar with Mockito and prefer to stub the rest client yourself, then assume that you can pass in a TestRestClient as an argument to the method you are trying to test:

class TestRestClient extends RestClient {
    public Object response;
    public RestError error;
    public boolean isSuccess;

    @Override
    public getAllLeads(int number, RestCallback<HashMap<String, Object>> callback) {
        if (isSuccess) {
            callback.restSuccess(response);
        } else {
            callback.failure(error);
        }
    }
}

TestRestClient testClient = new TestRestClient();
testClient.isSuccess = true;
testClient.response = <your-response>;
// test your method by passing testClient here

Needless to say TestRestClient should stay in your 'test' source directory, not in your production code. But like I said using Mockito or equivalent mock framework would be more preferable.

hidro
  • 12,333
  • 6
  • 53
  • 53