Mocking methods of local scope objects with Mockito

后端 未结 5 1186
野性不改
野性不改 2020-11-27 02:44

I need some help with this:

Example:

void method1{
    MyObject obj1=new MyObject();
    obj1.method1();
}

I want to mock obj

相关标签:
5条回答
  • 2020-11-27 03:21

    You could avoid changing the code (although I recommend Boris' answer) and mock the constructor, like in this example for mocking the creation of a File object inside a method. Don't forget to put the class that will create the file in the @PrepareForTest.

    package hello.easymock.constructor;
    
    import java.io.File;
    
    import org.easymock.EasyMock;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.easymock.PowerMock;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
        
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({File.class})
    public class ConstructorExampleTest {
            
        @Test
        public void testMockFile() throws Exception {
    
            // first, create a mock for File
            final File fileMock = EasyMock.createMock(File.class);
            EasyMock.expect(fileMock.getAbsolutePath()).andReturn("/my/fake/file/path");
            EasyMock.replay(fileMock);
    
            // then return the mocked object if the constructor is invoked
            Class<?>[] parameterTypes = new Class[] { String.class };
            PowerMock.expectNew(File.class, parameterTypes , EasyMock.isA(String.class)).andReturn(fileMock);
            PowerMock.replay(File.class); 
        
            // try constructing a real File and check if the mock kicked in
            final String mockedFilePath = new File("/real/path/for/file").getAbsolutePath();
            Assert.assertEquals("/my/fake/file/path", mockedFilePath);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 03:34

    The answer from @edutesoy points to the documentation of PowerMockito and mentions constructor mocking as a hint but doesn't mention how to apply that to the current problem in the question.

    Here is a solution based on that. Taking the code from the question:

    public class MyClass {
        void method1 {
            MyObject obj1 = new MyObject();
            obj1.method1();
        }
    }
    

    The following test will create a mock of the MyObject instance class via preparing the class that instantiates it (in this example I am calling it MyClass) with PowerMock and letting PowerMockito to stub the constructor of MyObject class, then letting you stub the MyObject instance method1() call:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(MyClass.class)
    public class MyClassTest {
        @Test
        public void testMethod1() {      
            MyObject myObjectMock = mock(MyObject.class);
            when(myObjectMock.method1()).thenReturn(<whatever you want to return>);   
            PowerMockito.whenNew(MyObject.class).withNoArguments().thenReturn(myObjectMock);
            
            MyClass objectTested = new MyClass();
            objectTested.method1();
            
            ... // your assertions or verification here 
        }
    }
    

    With that your internal method1() call will return what you want.

    If you like the one-liners you can make the code shorter by creating the mock and the stub inline:

    MyObject myObjectMock = when(mock(MyObject.class).method1()).thenReturn(<whatever you want>).getMock();   
    
    0 讨论(0)
  • 2020-11-27 03:39

    No way. You'll need some dependency injection, i.e. instead of having the obj1 instantiated it should be provided by some factory.

    MyObjectFactory factory;
    
    public void setMyObjectFactory(MyObjectFactory factory)
    {
      this.factory = factory;
    }
    
    void method1()
    {
      MyObject obj1 = factory.get();
      obj1.method();
    }
    

    Then your test would look like:

    @Test
    public void testMethod1() throws Exception
    {
      MyObjectFactory factory = Mockito.mock(MyObjectFactory.class);
      MyObject obj1 = Mockito.mock(MyObject.class);
      Mockito.when(factory.get()).thenReturn(obj1);
      
      // mock the method()
      Mockito.when(obj1.method()).thenReturn(Boolean.FALSE);
    
      SomeObject someObject = new SomeObject();
      someObject.setMyObjectFactory(factory);
      someObject.method1();
    
      // do some assertions
    }
    
    0 讨论(0)
  • 2020-11-27 03:40

    If you really want to avoid touching this code, you can use Powermockito (PowerMock for Mockito).

    With this, amongst many other things, you can mock the construction of new objects in a very easy way.

    0 讨论(0)
  • 2020-11-27 03:40

    You can do this by creating a factory method in MyObject:

    class MyObject {
        public static MyObject create() {
          return new MyObject();
        }
    }
    

    then mock that with PowerMock.

    However, by mocking the methods of a local scope object, you are depending on that part of the implementation of the method staying the same. So you lose the ability to refactor that part of the method without breaking the test. In addition, if you are stubbing return values in the mock, then your unit test may pass, but the method may behave unexpectedly when using the real object.

    In sum, you should probably not try to do this. Rather, letting the test drive your code (aka TDD), you would arrive at a solution like:

    void method1(MyObject obj1) {
       obj1.method1();
    }
    

    passing in the dependency, which you can easily mock for the unit test.

    0 讨论(0)
提交回复
热议问题