mockito

单元测试利器Mockito框架

你离开我真会死。 提交于 2020-07-27 08:28:07
什么是Mock Mock 的中文译为仿制的,模拟的,虚假的。对于测试框架来说,即构造出一个模拟/虚假的对象,使我们的测试能顺利进行下去。 Mock 测试就是在测试过程中,对于某些 不容易构造(如 HttpServletRequest 必须在 Servlet 容器中才能构造出来)或者不容易获取 比较复杂 的对象(如 JDBC 中的 ResultSet 对象),用一个 虚拟 的对象( Mock 对象)来创建,以便测试方法。 为什么使用Mock测试 单元测试 是为了验证我们的代码运行正确性,我们注重的是代码的流程以及结果的正确与否。 对比真实运行代码,可能其中有一些 外部依赖 的构建步骤相对麻烦,如果我们还是按照真实代码的构建规则构造出外部依赖,会大大增加单元测试的工作,代码也会参杂太多非测试部分的内容,测试用例显得复杂难懂。 采用 Mock 框架,我们可以 虚拟 出一个 外部依赖,只注重代码的 流程与结果,真正地实现测试目的。 Mock测试框架的好处 可以很简单的虚拟出一个复杂对象(比如虚拟出一个接口的实现类); 可以配置 mock 对象的行为; 可以使测试用例只注重测试流程与结果; 减少外部类、系统和依赖给单元测试带来的耦合。 Mockito的流程 如图所示,使用 Mockito 的大致流程如下: 创建 外部依赖 的 Mock 对象, 然后将此 Mock 对象注入到 测试类 中;

单元测试工具-PowerMock

邮差的信 提交于 2020-07-25 23:59:55
单元测试工具-PowerMock Mock:为某个类创建一个虚假的对象,在测试时用这个虚假的对象替换掉真实的对象。 PowerMock实现原理: 1)PowerMock会创建一个org.powermock.core.classloader.MockClassLoader类加载器,然后该类加载器会去加载测试用例使用到的类(系统类除外)。 2)PowerMock会去修改写在注解@PrepareForTest里的class文件(注:当前测试类会自动加入该注解中),以实现mock的功能。 3)如果需要mock系统类的final方法和静态方法,PowerMock不会直接修改系统类的class文件,而是修改调用系统类的class文件。 UT指标: 行覆盖、函数覆盖、分支覆盖 增量行覆盖、增量函数覆盖、增量分支覆盖 注意:AClassTest和AClass的包路径必须一致,否则单测覆盖率统计不到。 实现: 第一步: maven依赖: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId

Mocking ScheduledExecutorService.scheduleWithFixedDelay(…) is returning null

蓝咒 提交于 2020-07-21 04:19:33
问题 In my unit test, I've injected a mocked instance of the ScheduledExecutoryService class into the class that I'm trying to test so that when the scheduleAtFixedRate(...) method is called, it returns a mocked Future . For some reason though, it's always returning null . Any ideas ? Application code: Future<?> t = scheduledExecutorService.scheduleAtFixedRate(this, 10, 10, TimeUnit.SECONDS); Test code: @Mock ScheduledExecutorService scheduledExecutorService; @Mock ScheduledFuture<?> t; Mockito

Mockito's argThat returning null when in Kotlin

ぃ、小莉子 提交于 2020-07-20 08:23:30
问题 Given the following class (written in kotlin): class Target { fun <R> target(filter: String, mapper: (String) -> R): R = mapper(filter) } I'm able to test in java, the test code: @Test public void testInJava() { Target mockTarget = Mockito.mock(Target.class); Mockito.when(mockTarget.target( argThat(it -> true), Mockito.argThat(it -> true) )).thenReturn(100); assert mockTarget.target("Hello World", it -> 1) == 100; } The java test pass as expected, but the same test is written in kotlin: @Test

Mockito - verify a double value

一世执手 提交于 2020-07-20 06:53:11
问题 I have a method called method1 that takes a double which is called on myManager I am passing into this 65.888 * 60. When I try and verify this I get floating point problems. The verification fails. It expects 3953.28 but 3953.280029296875 verify(myManager, times(1)).method1(65.888 * 60d); Is there anyway I can make this verify do a fuzzy check for floating point checking. Much like you do with assertEquals where you input a delta at the end. Thanks 回答1: You could capture the value, e.g. final

Mockito - verify a double value

丶灬走出姿态 提交于 2020-07-20 06:52:24
问题 I have a method called method1 that takes a double which is called on myManager I am passing into this 65.888 * 60. When I try and verify this I get floating point problems. The verification fails. It expects 3953.28 but 3953.280029296875 verify(myManager, times(1)).method1(65.888 * 60d); Is there anyway I can make this verify do a fuzzy check for floating point checking. Much like you do with assertEquals where you input a delta at the end. Thanks 回答1: You could capture the value, e.g. final

Mockito when().thenReturn calls the method unnecessarily

倾然丶 夕夏残阳落幕 提交于 2020-07-18 03:39:08
问题 I'm working a bit on an inherited code. I've written a test that is supposed to catch NullPointerException (for it is trying to call a method from null object) @Test(expected=NullPointerException.class) public void checkXRequirement_NullProduct_AddAction_ShouldThrowNullPointerException() throws CustomException { Site site = mock(Site.class); Product product = null; when(BasketHelper.getAction(request)).thenReturn(0); when(BasketHelper.getActionProduct(site, request)).thenReturn(product);

How to test a method that uses Random(), without arguments and return value, using JUnit or Mockito

大城市里の小女人 提交于 2020-07-09 13:19:06
问题 I'm studying to be a Java developer and right now I'm learning test driven development, which means that im very new to JUnit and Mockito. I've been struggling for a while now and I'm stuck. I have no idea how to test this particular method that has no arguments, no return value and a randomizer. Old logic: public void getPlayerToStart(int randomNr) { if (randomNr == 1) { currentPlayer = p1; opponentPlayer = p2; } else { currentPlayer = p2; opponentPlayer = p1; } } Old test @Test void

Mock a static method with mockito

拥有回忆 提交于 2020-07-09 08:04:47
问题 I want to mock a static method in Mockito. As far as I know this is not possible, how can I get around the problem? powermock is not an option. I want that my authentication variable won't be null. Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); I read an answer here but I don't know how to put this answer to code. Can someone give a solution? 回答1: As you pointed out, it is not possible to mock static methods with Mockito and since you do not wanna use

Mock a static method with mockito

只谈情不闲聊 提交于 2020-07-09 08:04:12
问题 I want to mock a static method in Mockito. As far as I know this is not possible, how can I get around the problem? powermock is not an option. I want that my authentication variable won't be null. Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); I read an answer here but I don't know how to put this answer to code. Can someone give a solution? 回答1: As you pointed out, it is not possible to mock static methods with Mockito and since you do not wanna use