• 4

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Mocking Network response for unit test case

I am trying to create a test case for AlbumsController which fetches data from the network and parses it.

AlbumService: Responsible for network call and getting data. ( Assume it can be anything that performs some async task and gives callback accordingly so as to avoid "Server" specific mocking solutions )

public class AlbumsController {

    public void getAlbums(final ServiceCallback<AlbumsWrapper> albumsServiceCallback) {

        new AlbumsService().fetchAlbums(new ServiceCallback<NetworkResponse>() {
            public void onSuccess(NetworkResponse response) {
                // parse response

            public void onFailure(NetworkResponse error) {
                // do something for Failure

public class AlbumControllerTest {

    private ServiceCallback<NetworkResponse> serviceCallback;

    private ArgumentCaptor<ServiceCallback<AlbumsWrapper>> albumsWrapper;

    private  ArgumentCaptor<ServiceCallback<NetworkResponse>> networkResponseArgumentCaptor;

    public void init() {

    public void parseAlbums_EmptyList_ReturnsTrue() {

// below are different ways I am trying with no success so far.
        AlbumsController albumsController = new AlbumsController();

        NetworkResponse response = new NetworkResponse();


  • To mock AlbumService inside getAlbums method so that instead of getting data from the server I should be able to call onSuccess() with my test data as an argument. And is it possible to get the list after parsing so as that I can assert base on the data in the list?
  • I don't want to move code responsible for parsing to some method and make that public. My intention here is to understand how to handle such a case.
  • Open for refactoring of code if that's what is required to be taken care of in TDD.
  • Full solution or pointers to look for both are appreciated.


  • mockito-core:2.2.9
  • junit:4.12

If small refactoring is the option then:

1) Move the new AlbumsService() to a package level method:

AlbumService createAlbumService(){
    return new AlbumService();


public void getAlbums(final ServiceCallback<AlbumsWrapper> albumsServiceCallback) {

    createAlbumService().fetchAlbums(new ServiceCallback<NetworkResponse>()

package visibility is enough as the test class will be in the same package as the AlbumController.

2) Spy the AlbumController:

private AlbumsController albumsControllerSpy = new AlbumController();

private AlbumService albumServiceMock;

3) Make the createAlbumService() method return your mock:

public void parseAlbums_EmptyList_ReturnsTrue() {

  • 1
Reply Report