faster_fene
faster_fene

Reputation: 43

Mockito - mock library call

I have the following structure

public class classAImpl implements classA {

   public ClassC getTarget(Classc cObj) { 
   // library call
    RegistryLib.init();
    // some more code to construct cObj with more info
    return cObj
  }
}

// Registry Library Class
Class RegistryLibClass{
   public void init() {
   ClassD.builder.build();   
}
}

My test class is trying to test a method which calls getTarget() method written above. I want to completely avoid executing getTarget() method which I am not been able even after mocking it. So far I have tried the following:

Class Testclass {
   @Before
   public void setUp() {
      Testclass mock = PowerMockito.mock(Testclass.class);
      PowerMockito.when(mock.getTarget(cObj)).thenReturn(cObj);    
  }

 private ClassC getTarget(cObj) {
        return cObj;
    }
}

Any help is appreciated!

Upvotes: 1

Views: 6333

Answers (3)

Prasann
Prasann

Reputation: 1291

Your example is very confusing as you are trying to mock your test class itself instead of the main class.

Also you have mentioned that you are trying to test a method which calls getTarget() method, but I don't see any method in your class that calls getTarget() method.

I have put down a simple example below for your understanding. Let me know if this helps.

ClassA

public class ClassA {
    public String method1() {
        return "ClassA -> method1";
    }

    public static String method2() {
        return "ClassA -> method2";
    }
}

ClassB calls Class A methods

public class ClassB {
    public void method1() {
        System.out.println("ClassB -> method1");
        new ClassA().method1();
        ClassA.method2();
    }
}

ClassB Test

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassA.class)
public class ClassBTest {
    @Mock
    ClassA mock;

    @Before
    public void setUp() throws Exception {
        // Initialize mocks
        MockitoAnnotations.initMocks(this);

        // This is for mocking new objects
        PowerMockito.whenNew(ClassA.class).withNoArguments().thenReturn(mock);
        PowerMockito.when(mock.method1()).thenReturn("Mocked Method 1");

        // This is for mocking static methods
        PowerMockito.mockStatic(ClassA.class);
        PowerMockito.when(ClassA.method2()).thenReturn("Mocked Method 2");
    }

    @Test
    public void testMeth() {
        System.out.println(new ClassA().method1()); // Prints - Mocked Method 1

        System.out.println(ClassA.method2()); // Prints - Mocked Method 2
    }
}

Upvotes: 0

nilesh
nilesh

Reputation: 14287

Assuming you want to test a method in Class B that calls getTarget from Class A, you would do this,

B b = new B();
A a = Mockito.mock(A.class);
C c = new C();
Mockito.when(a.getTarget(Mockito.any(C.class)).thenReturn(c);
boolean isPresent = b.someMethodToTest();
assertEquals("someMethodToTest was supposed to return true", true/*expected*/, isPresent);

Edit#1 You need to use Powermockito to mock the static method to return nothing as mentioned here

@PrepareForTest(RegistryLibClass.class) //at the top of the class

//inside your test
PowerMockito.mockStatic(RegistryLibClass.class); 
Mockito.when(RegistryLibClass.init()).doNothing();

Upvotes: 1

Worthless
Worthless

Reputation: 521

Disclaimer - I'm not that familiar with mockito, but wouldn't you normally mock the class you want to avoid using? Like this:

class Testclass {
   @Before
   public void setUp() {
      //create some mock of expected cObj here to be returned.
      classAImpl mock = PowerMockito.mock(classAImpl.class);
      PowerMockito.when(
         mock.getTarget(cObj.class /*Shouldn't this be class call?*/))
         .thenReturn(cObj);    
  }
}

And then you would inject mock as dependency into the object that uses it and that you want to test.

If I'm wrong then feel free to ignore me, but thats how other mocking libraries I used worked. I'd advise you to go read some tutorials about mocking for tests regardless.

As side note, use of Class and class in names made this example extremely difficult to understand.

Upvotes: 1

Related Questions