mockito

Spring batch:Test case for Tasklet - Key is not appearing in actual class when it is invoked from Test class

半腔热情 提交于 2020-05-17 05:59:22
问题 I am trying to learn Batch and Tasklet. I am writing a test case for a Tasklet code in spring batch. I am setting a map in my Test class and debug, the actual class is not having the key which I am passing from my test class. MyEventTasklet.java public class MyEventTasklet implements Tasklet { public RepeatStatus execute (StepContribution contribution, ChunkContext chunkContext){ TreeMap<String, Map<Integer, Set<Student>>> studentMap = chunkContext.getStepContext().getJobExecutionContext()

Mockito, argThat, and hasEntry

萝らか妹 提交于 2020-05-14 19:50:08
问题 tl;dr: These tests don't compile because the type parameters don't match. What changes should I make to make them compile and run correctly? https://github.com/wesleym/matchertest I have some non-test code that calls into a service. It calls the service's activate method with a map parameter. public class Foo { private final Service service; public Foo(Service service) { this.service = service; } public void bar() { Map<String, ?> params = getParams(); service.activate(params); } private Map

Mocked suspend lambda returns null in Mockito

南楼画角 提交于 2020-05-14 04:12:44
问题 Dependencies : testImplementation "androidx.arch.core:core-testing:2.0.0" testImplementation 'org.mockito:mockito-core:3.3.0' testImplementation 'org.mockito:mockito-inline:2.8.47' testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0" ViewModel : fun <T : Any> doSomething( result: Result<T>, callback: Callback<T>? = null ) { when (result.status) { Result.Status.SUCCESS -> { callback?.success(result.data) } Result.Status.ERROR -> { callback?.error(result) } } } suspend fun <T :

Mocked suspend lambda returns null in Mockito

为君一笑 提交于 2020-05-14 04:06:45
问题 Dependencies : testImplementation "androidx.arch.core:core-testing:2.0.0" testImplementation 'org.mockito:mockito-core:3.3.0' testImplementation 'org.mockito:mockito-inline:2.8.47' testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0" ViewModel : fun <T : Any> doSomething( result: Result<T>, callback: Callback<T>? = null ) { when (result.status) { Result.Status.SUCCESS -> { callback?.success(result.data) } Result.Status.ERROR -> { callback?.error(result) } } } suspend fun <T :

Mocked suspend lambda returns null in Mockito

 ̄綄美尐妖づ 提交于 2020-05-14 04:04:22
问题 Dependencies : testImplementation "androidx.arch.core:core-testing:2.0.0" testImplementation 'org.mockito:mockito-core:3.3.0' testImplementation 'org.mockito:mockito-inline:2.8.47' testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0" ViewModel : fun <T : Any> doSomething( result: Result<T>, callback: Callback<T>? = null ) { when (result.status) { Result.Status.SUCCESS -> { callback?.success(result.data) } Result.Status.ERROR -> { callback?.error(result) } } } suspend fun <T :

increasing code coverage for JdbcTemplate mocking

冷暖自知 提交于 2020-05-11 11:01:54
问题 I am mocking JdbcTemplate for unit test cases, as don't want to hit actual database integration. But it is decreasing my code coverage (Red indicates missing coverage). Below is the snippet used. Same case happens by using user defined mappers. final List<String> resultList = new ArrayList<String>(); resultList.add("test1"); resultList.add("test2"); final JdbcTemplate template = Mockito.mock(JdbcTemplate.class); Mockito.when( template.query(Mockito.anyString(), Mockito.any(Object[].class),

increasing code coverage for JdbcTemplate mocking

六眼飞鱼酱① 提交于 2020-05-11 10:59:01
问题 I am mocking JdbcTemplate for unit test cases, as don't want to hit actual database integration. But it is decreasing my code coverage (Red indicates missing coverage). Below is the snippet used. Same case happens by using user defined mappers. final List<String> resultList = new ArrayList<String>(); resultList.add("test1"); resultList.add("test2"); final JdbcTemplate template = Mockito.mock(JdbcTemplate.class); Mockito.when( template.query(Mockito.anyString(), Mockito.any(Object[].class),

Can Mockito capture arguments of a method called multiple times?

大城市里の小女人 提交于 2020-05-09 17:31:07
问题 I have a method that gets called twice, and I want to capture the argument of the second method call. Here's what I've tried: ArgumentCaptor<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo.class); ArgumentCaptor<Foo> secondFooCaptor = ArgumentCaptor.forClass(Foo.class); verify(mockBar).doSomething(firstFooCaptor.capture()); verify(mockBar).doSomething(secondFooCaptor.capture()); // then do some assertions on secondFooCaptor.getValue() But I get a TooManyActualInvocations Exception, as

Springboot单元测试Junit深度实践

寵の児 提交于 2020-05-02 19:18:26
Springboot单元测试Junit深度实践 前言 单元测试的好处估计大家也都知道了,但是大家可以发现在国内IT公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这篇文章就是介绍下单测的方法论和如何在Springboot中解决类之间的依赖来实施junit单元测试。 先来他轮下大家不做单元测试的原因: 产品经理天天催进度,哪有时间写UT。 UT是测试自己的代码,自测?那要QA何用? 自测能测出bug?都是基于自身思维, 就像考试做完第一遍,第二遍检查一样,基本检查不出什么东西 。 UT维护成本太高,投入产出比太低 不会写UT 只有真正尝到UT的好处的甜头才会意识到UT的价值。 其实这篇文章大部分章节是讲述单元测试的难点和解决办法,springboot如何集成其实很简单文章中会讲到。 单元测试的困难 假设我们一个service实现依赖某个RPC Service那我们要测试的这个类的话需要做哪些工作。 第一步:数据准备 跑到别人家的数据库插几条数据?或者跟PRC Service的Owner商量好,搭一个测试环境供我们测试?有些公司还真有专门的自动化测试环境,那么即使有测试环境,那如何实现各种case场景下,第三方Service很配合的返回数据给我们?想想都蛋疼。 第二步:执行方法 假设我们成功的解决了第一步中的问题,皆大欢喜。现在来看第二步

spring /spring boot中mock

限于喜欢 提交于 2020-05-02 19:17:58
1 Mockito简介 1.1 Mockito是什么   Mockito是一个简单的流行的Mock框架。它允许你创建和配置mock对象。使用Mockito可以明显的简化对外部依赖的测试类的开发。一般使用 Mockito 需要执行下面三步:  模拟并替换测试代码中外部依赖;  执行测试代码;  验证测试代码是否被正确的执行。 1.2 Mock是什么   Mock测试就是在测试过程中,对某些不容易构造或者不容易获取的对象,用一个虚拟的Mock对象来创建以便测试的测试方法。   Mock最大的功能是帮你把单元测试的耦合分解开,如果你的代码对另一个类或者接口有依赖,它能够帮你模拟这些依赖,并帮你验证所调用的依赖的行为。 2 Mockito在Spring框架中的使用 2.1 依赖包 <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version> 2.21 . 0 </version> <scope>test</scope> </dependency> Mockito在Spring可以通过配置文件和注解的方式使用 2.2 通过xml配置文件使用Mockito   Mockito创建方式有两种:mock,spy。mock为一个interface提供一个虚拟的实现