简体   繁体   English

Mockito模拟方法的价值

[英]Mockito mocking the value of a method

Class under test: 被测课程:

class FruitQuality {

  void testQuality() {
    getfruits();
    //some code
  }

  List<Fruits> getfruits() {
    return orange;
  }
}

Below is the test code. 下面是测试代码。 I mocked the class class under test and overridden a return value for the method getfruits. 我嘲笑了受测类,并重写了方法getfruits的返回值。 But when I run the mock, I don't get the expected mocked return value. 但是,当我运行模拟时,我没有得到预期的模拟返回值。 Easymock can substitute return values for methods of class under test, if those are explicitly mocked. 如果显式模拟了Easymock的返回值,则可以用它们代替测试中的类的方法。 How can I get the mocked return value when I mock the real object method. 模拟真实对象方法时,如何获得模拟的返回值。

@Test
public void test() {
  Fruits fruit= new Fruits();
  fruit.setFruit("orange");
  List<Fruits> fruitsList = new ArrayList<Fruits>();
  fruitsList.add(fruit);

  Fruits mock = Mockito.mock(FruitQuality.class)
  classUnderTest = new FruitQuality();
  when(mock.getfruits()).thenReturn(fruitsList);

  result= classUnderTest.getfruits();
  assertEquals(result, fruitsList);
}

Typically, when writing unit tests, you have one class under test containing the implementation code you are testing, while mocking the methode of other classes your class under test is relying on. 通常,在编写单元测试时,您有一个包含要测试的实现代码的被测类,同时模拟了被测类所依赖的其他类的方法。

Refering to your example you would have a class 参考您的示例,您将有一个课程

public class FruitQuality {
    private FruitProvider fruitProvider;

    public List<Fruit> testQuality() {
        List<Fruit> fruits = fruitProvider.getfruits();
        /* Some code doing something with fruits, e.g., filtering. */
        return fruits;
    }
}

And a second class 还有二等

public class FruitProvider {
    public List<Fruits> getfruits() {
        /* Returning some fruits ... */
    }
}

Now, you could mock your Provider to test your class under test: 现在,您可以模拟您的Provider来测试您的被测类:

@RunWith(MockitoJUnitRunner.class)
public class FruitQualityUnitTest {
    @InjectMocks
    private FruitQuality fruitQuality;
    @Mock
    private FruitProvider fruitProvider;

    @Test
    public void testQuality() {
        /* Mocking the provider. */
        Fruits fruit= new Fruits();
        fruit.setFruit("orange");
        List<Fruits> fruitsList = new ArrayList<Fruits>();
        fruitsList.add(fruit);
        when(fruitProvider.getFruits()).thenReturn(fruitsList);

        /* Invoke the method under test. */
        List<Fruits> result = fruitProvider.testQuality();

        /* Assert that some methods where called. */
        verify(fruitProvider, times(1)).getFruits();

        /* If the method would return a value, you can do some assertions based on the mocked method calls. */
        assertEquals(result, fruitsList);
    }
}

If you want to test methods of the same object you are mocking, you can use the @Spy annotation instead of @Mock, assuring that only methods you are explicitely mocking doing other things than the original implementation. 如果要测试要模拟的同一对象的方法,可以使用@Spy注释而不是@Mock,以确保只有您显式地模拟的方法会执行除原始实现之外的其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM