Tag: mockito

用mockito嘲笑一个单身人士

我需要测试一些遗留代码,它在方法调用中使用单例。 测试的目的是确保clas sunder测试调用单例方法。 我在SO上看到过类似的问题,但所有的答案都需要其他依赖项(不同的测试框架) – 我不幸的是仅限于使用Mockito和JUnit,但这对于这样的流行框架来说应该是完全可能的。 单身人士: public class FormatterService { private static FormatterService INSTANCE; private FormatterService() { } public static FormatterService getInstance() { if (INSTANCE == null) { INSTANCE = new FormatterService(); } return INSTANCE; } public String formatTachoIcon() { return “URL”; } } 被测试的课程: public class DriverSnapshotHandler { public String getImageURL() { return […]

PowerMock:模拟静态方法(+在某些特定方法中返回原始值)

我使用的是PowerMock 1.4.7和JUnit 4.8.2 我只需要模拟一些静态方法,我希望其他人(来自同一个类)只返回原始值。 当我使用mockStatic进行模拟并且不调用when().doReturn()所有静态方法都返回其默认值 – 返回Object时为null,返回boolean时返回false等等。 所以我尝试在每个静态方法上显式使用thenCallRealMethod来返回默认实现(意味着没有模拟/没有假货)但我不知道如何在每个可能的参数变量上调用它(=我想要每个可能的输入调用原始方法) 。 我只知道如何模拟具体的论证变异。

Mockito isA(Class clazz)如何解决类型安全问题?

在我的测试中我有以下几行: when(client.runTask(anyString(), anyString(), isA(Iterable.class)).thenReturn(…) isA(Iterable.class)产生警告,它需要未经检查的转换以符合Iterable 。 这是什么语法? isA(Iterable.class) isA((Iterable)Iterable.class 不工作。 有什么建议么?

mockito如何创建模拟对象的实例

当我创建一个类Employee的模拟对象。 它不会调用Employee对象的构造函数。 我知道内部Mockito使用CGLIb和reflection,创建一个代理类,将类扩展为mock。 如果它没有调用employee的构造函数,那么employee类的mock实例是如何创建的?

Mockito UnfinishedStubbingException

我是Mockito的新手,我试过调查这个例外但我还没有找到具体的答案。 当我一起使用两个模拟时,它发生在我的代码中,这意味着我通过模拟的构造函数,另一个模拟。 像这样: … OperationNode child = getNode(Operation.ADD); child.insertNode(getConstantNode(getIntegerValue(2)); … private ConstantNode getConstantNode(NumericalValue value){ ConstantNode node = Mockito.mock(ConstantNode.class); Mockito.when(node.evaluate()).thenReturn(value); Mockito.when(node.toString()).thenReturn(value.toString()); return node; } private IntegerValue getIntegerValue(int number) { IntegerValue integerValue = Mockito.mock(IntegerValue.class); Mockito.when(integerValue.getValue()).thenReturn(number); Mockito.when(integerValue.toString()).thenReturn(Integer.toString(number)); return integerValue; } 在其中一个论坛中,我读到没有通过另一个模拟的构造函数发送模拟,因为Mockito可能会对模拟调用感到困惑,所以我尝试了以下内容: NumericalValue value = getIntegerValue(2); child.insertNode(getConstantNode(value)); 但无济于事。 我确保只调用toString()和getValue()方法,因为这些是类具有的唯一方法。 我不明白发生了什么。 我也试过单独使用模拟,看看我做错了什么: child.insertNode(new ConstantNode(getIntegerValue(2))); 这非常有效。 child.insertNode(getConstantNode(new IntegerValue(2))); 这也很好。

使用PowerMockito 1.6validation静态方法调用

我正在为类似于下面给出的示例的方法编写JUnit测试用例: Class SampleA{ public static void methodA(){ boolean isSuccessful = methodB(); if(isSuccessful){ SampleB.methodC(); } } public static boolean methodB(){ //some logic return true; } } Class SampleB{ public static void methodC(){ return; } } 我在我的测试类中编写了以下测试用例: @Test public void testMethodA_1(){ PowerMockito.mockStatic(SampleA.class,SampleB.class); PowerMockito.when(SampleA.methodB()).thenReturn(true); PowerMockito.doNothing().when(SampleB.class,”methodC”); PowerMockito.doCallRealMethod().when(SampleA.class,”methodA”); SampleA.methodA(); } 现在我想validation是否调用类Sample B的静态methodC()。 如何使用PowerMockito 1.6实现? 我尝试了很多东西,但似乎并没有为我做好准备。 任何帮助表示赞赏。

Mockito Matchers之间有什么区别是A,any,eq和同样的?

我很困惑他们之间有什么区别,在哪种情况下选择哪一个。 有些差异可能很明显,比如any和eq ,但我把它们都包括在内只是为了确定。 我想知道他们之间的差异,因为我遇到了这个问题:我在Controller类中有这个POST方法 public Response doSomething(@ResponseBody Request request) { return someService.doSomething(request); } 并希望在该控制器上执行unit testing。 我有两个版本。 第一个是简单的,就像这样 @Test public void testDoSomething() { //initialize ObjectMapper mapper //initialize Request req and Response res when(someServiceMock.doSomething(req)).thenReturn(res); Response actualRes = someController.doSomething(req); assertThat(actualRes, is(res)); } 但是我想使用像这样的MockMvc方法 @Test public void testDoSomething() { //initialize ObjectMapper mapper //initialize Request req and Response res when(someServiceMock.doSomething(any(Request.class))).thenReturn(res); […]

Mockitovalidation不再与任何模拟交互

在Mockito,有没有办法validation我创建的任何模拟都没有更多的交互? 例如: public void test() { … TestObject obj = mock(TestObject); myClass.test(); verifyNoMoreInteractionsWithMocks(); <——- } 有这样的方法吗?

使用PowerMock模拟私有方法,但仍会调用底层方法

我试图嘲笑模拟一个正在进行JNDI调用的私有方法。 当从unit testing调用该方法时,它会抛出exception^。 我想嘲笑这种方法用于测试目的。 我使用了来自另一个问题答案的示例代码 ,并且在测试通过时,似乎仍然调用了基础方法。 我在doTheGamble()方法中插入了一个System.err.println() ,然后将其打印到我的控制台。 有趣的是,如果我注释掉第一个assertThat ,那么测试就会通过。 ?:( 那么,我如何模拟私有方法,以便它不被调用? import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.support.membermodification.MemberMatcher.method; import java.util.Random; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(CodeWithPrivateMethod.class) public class PowerMock_Test { static boolean gambleCalled = false; @Test(expected = […]

在mockito中使用doThrow()doAnswer()doNothing()和doReturn()的用法

我正在学习mockito,我从链接中了解了上述function的基本用法。 但我想知道它是否可以用于任何其他情况?