Method call on Mock object calls real method instead of mock

I have my code as below

public process() {
    extract();
    ...
}

private Obj extract() {
    Constructor const = new Constructor();
    Obj object = const.getOBJMethod("12345","c:/file/a.zip",null);
    return object;
}

      

I am testing a method using mockito. and in my test class i wrote the code as

Constructor mocckConst = mock(Constructor.class);
Obj mockObject = mock(Obj.class);
when(mocckConst .getOBJMethod("12345","c:/file/a.zip",null).thenReturn(mockObject);

      

But when the test request is called, when the extract method is called, it goes into the real implementation of getOBJMethod ().

The constructor class has a different inner class. Is this causing problems? Can anyone tell me what is going wrong here and the solution.

I would like to improvise my process method.

public process(String base) {
    if("abc".equals(base)) {
       ---
    }
    else if("def".equals(base) {
    extract();
    ---
    }
}

      

This extract () method is only called when base def. and I don't want to pass the constructor object to the process () method, then are there any solutions?

+3


source to share


2 answers


In the class you want to test, you create a new Constructor object (via Constructor const = new Constructor()

), hence you always use the REAL implementation. You must enter an object Constructor

if you want it to be replaced with a mock object for testing. Injection is also possible through the test constructor.

private final Constructor const; // remove final, if required

public <ConstructorOfYourClassHere>(Constructor const) {
    assert const != null : "const != null"; // use assertions, if you like

    this.const = const;

    // other constructor code...
}

// your other code here...

public process(String base) {
    if("abc".equals(base)) {
        // ---
    }
    else if("def".equals(base) {
        extract();
        // ---
    }
}

private Obj extract() {
    Obj object = const.getOBJMethod("12345","c:/file/a.zip",null);
    return object;
}

      

Then you can inject mock when creating the tested object and call process()

. Then your mock implementation will be used.



BTW

  • If you are the owner of the code, you may need to change the visibility of the method extract

    to protected

    so you can test that method as well. See here .
  • Also, you can read something about dependency injection in general. Testing with DI is often much easier. You don't need to use the DI-framework, just inject your dependencies through method parameters or through the constructor during object creation.
+5


source


You can view the Constructor object if you want to be validated with a mock.



public class Test{

    @spy
    Constructor const;

    public process() {
        extract();
        ...
    }

    private Obj extract() {

        Obj object = const.getOBJMethod("12345","c:/file/a.zip",null);
        return object;
    }
    }

      

+1


source







All Articles