mockito

Mock a method that returns a Stream and is called more than one time

眉间皱痕 提交于 2020-08-24 07:29:07
问题 MyStreamClass mock = mock(MyStreamClass.class); when(mock.streamMethod()).thenReturn(Stream.of("A", "B")); System.out.println(""+mock.streamMethod().findFirst()); System.out.println(""+mock.streamMethod().findFirst()); the second call of findFirst will throw java.lang.IllegalStateException: stream has already been operated upon or closed 回答1: Try the thenAnswer instead of thenReturn : Answer<Stream> answer = new Answer<Stream>() { public Stream answer(InvocationOnMock invocation) throws

Mockito's Answer in ScalaTest

本小妞迷上赌 提交于 2020-08-24 06:44:09
问题 Is there some alternative to Mockito's Answer in ScalaTest? I was going through its documentation, but didn't find anything. I would like to, for example, execute some logic on arguments of a stubbed method. In Mockito, I would do something like this: when(mock.create(any(A.class))).thenAnswer(new Answer() { Object answer(InvocationOnMock invocation) { A firstArg = (A) invocation.getArguments()[0]; firstArg.callMethod(); return null; } }); In ScalaTest, I'm fine with using Mockito, as well.

程序员日记2020-08-13【减肥Day11】

若如初见. 提交于 2020-08-15 17:21:12
感谢抬爱,竟然已经有人私信催更了,我的日记难道不是很无聊吗?🐶 终于没迟到,踩点到了公司,但是依然是路边买的早餐。明天一定要早起,今天要早睡。昨天1点多才睡,我该死...... 昨天刚买完十一往返的机票,今天竟然告诉我10月8号要数据迁移,杀了我好了...... 上午写单测,下午写单测,第一次用Mockito,踩了好多坑,我好快乐🙂️ 【有个单测一直报错,终于找到原因了,括号写错地方了,我是傻逼,我是傻逼,被自己气死】 依然是和前端小哥联调的一天,今天不是很坑,他笑起来还是挺好看的,我原谅他了【呵,女人 晚上吃完饭在园区散步,群里发消息说一个小伙伴被120拉走了,一个很温柔的男孩子,祝福他...... 今天看点书吧,前几天都没看QAQ 它凝固了...... 饭饭 早饭 午饭 晚饭 今晚运动一下吧,想学游泳了...... 来源: oschina 链接: https://my.oschina.net/u/4355947/blog/4497021

单元测试 springboot-test

▼魔方 西西 提交于 2020-08-11 16:30:40
今天整理了下,springboot下单元测试基本用法 若使用了 @RunWith(SpringRunner.class)配置,需要用 org.junit.Test运行,juint4包, junit5包org.junit.jupiter.api.Test 不需要RunWith注解. 一 引入依赖 1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.3.0.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent> 7 <groupId>com.example</groupId> 8 <artifactId>mvctest</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <name>mvctest</name> 11 <description>Demo project for Spring Boot</description> 12 13 <properties> 14 <java.version>1.8</java

Mockito: InvalidUseOfMatchersException

回眸只為那壹抹淺笑 提交于 2020-08-10 12:32:31
  异常报错信息: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 4 matchers expected, 3 recorded: -> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java: 136 ) -> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java: 136 ) -> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java: 136 ) This exception may occur if matchers are combined with raw values : // incorrect: someMethod(anyObject(), " raw String

Instantiating a Class with private constructor using Java/Mockito/PowerMockito

大城市里の小女人 提交于 2020-08-07 05:47:38
问题 I am writing a test case using JUnit and the method under test takes a final class with a private constructor as a parameter. Since I cannot instantiate it with the new keyword I tried using Mockito but found out that Mockito doesn't like final class . I went to use PowerMockito which seemed reasonable to me but PowerMockito.mockStatic(Field.class); is a void method and I need a reference of Field so that I can pass it as an argument while invoking the method. I want to catch

Instantiating a Class with private constructor using Java/Mockito/PowerMockito

白昼怎懂夜的黑 提交于 2020-08-07 05:47:34
问题 I am writing a test case using JUnit and the method under test takes a final class with a private constructor as a parameter. Since I cannot instantiate it with the new keyword I tried using Mockito but found out that Mockito doesn't like final class . I went to use PowerMockito which seemed reasonable to me but PowerMockito.mockStatic(Field.class); is a void method and I need a reference of Field so that I can pass it as an argument while invoking the method. I want to catch

Mockito不能mock final类的解决办法

故事扮演 提交于 2020-07-29 06:01:34
Mockito 是很常用的测试工具,使用过程中可能会遇到下面的问题: Mockito cannot mock/spy because : - final class 问题重现: 引入该依赖到项目的maven配置如下: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.3.3</version> </dependency> 测试代码如下: package com.pkslow.basic; import org.junit.Assert; import org.junit.Test; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class MockitoTest { public static final class FinalPumpkin { public String getName() { return "pkslow.com"; } } @Test public void test() { FinalPumpkin pumpkin = mock(FinalPumpkin.class); when(pumpkin

Mockito不能mock final类的解决办法

老子叫甜甜 提交于 2020-07-27 09:08:24
Mockito 是很常用的测试工具,使用过程中可能会遇到下面的问题: Mockito cannot mock/spy because : - final class 问题重现: 引入该依赖到项目的maven配置如下: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.3.3</version> </dependency> 测试代码如下: package com.pkslow.basic; import org.junit.Assert; import org.junit.Test; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class MockitoTest { public static final class FinalPumpkin { public String getName() { return "pkslow.com"; } } @Test public void test() { FinalPumpkin pumpkin = mock(FinalPumpkin.class); when(pumpkin