mockito

Mockito mocking restTemplate.postForEntity

你离开我真会死。 提交于 2020-04-16 02:14:26
问题 I am trying to mock restTemplate.postForEntity method, The actual method call is: URI myUri = new URI(myString); HttpEntity<String> myEntity ... String myResponse = restTemplate.postForEntity(myUri, myEntity, String.class); What I have in my test class is: Mockito.when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), eq(String.class))).thenReturn(response); This does not work; I have tried several other permutations with no success either. Any suggestions appreciated, thanks.

JVM基础结构与字节码执行引擎

倖福魔咒の 提交于 2020-04-11 15:38:42
JVM基础结构与字节码执行引擎 JVM基础结构 JVM内部结构如下:栈、堆。 栈 JVM中的栈主要是指线程里面的栈,里面有方法栈、native方法栈、PC寄存器等等;每个方法栈是由栈帧组成的;每个栈帧是由局部变量表、操作数栈等组成。 每个栈帧其实就代表一个方法 堆 java中所有对象都在堆中分配;堆中对象又分为年轻代、老年代等等,不同代的对象使用不同垃圾回收算法。 -XMs:启动虚拟机预留的内存 -Xmx:最大的堆内存 一、堆的分代假设 根据研究表明,堆中对象大部分都是创建后,立马就可以被销毁的。如: 为了优化堆中的内存,将堆中对象分为不同代。在年轻代中,GC发生比较频繁;在老年代中,GC发生比较少。 二、堆的分代 年轻代:Young Generation 老年代:Old Generation/Tenured 永久代:Permanent Generation 永久代在Java虚拟机规范中是没有的,但是Host Spot虚拟机中有。 三、方法区 方法区被所有线程共享;方法区是用来存储编译后的代码,即存储每个类的运行时常量池、字段和方法。 方法区在虚拟机启动时创建;虽然方法区在逻辑上是堆的一部分,但在一些简单的实现中,方法区可以选择不进行垃圾回收和紧凑化。 方法区在java8的变化 java7之前:方法区的实现:永久代,是作为堆的一部分; java8之后:方法区的实现

NoClassDefFoundError or NoSuchMethodError when trying to mock static (incompatible dependencies)

。_饼干妹妹 提交于 2020-04-11 12:41:13
问题 When a class with static method is mocked, an exception been thrown. The version 2.0.0 of PowerMock displays NoClassDefFoundError 1 . The versions 2.0.1 and 2.0.2 displays NoSuchMethodError 2 . In project that I work is used Mockito on version 2.25.0 , but I've tried using new versions of Mockito following the compatible versions table (https://github.com/powermock/powermock/wiki/Mockito#supported-versions). In addition, I tested the downgrade to versions 2.8.9 of Mockito with 1.7.x of

How to mock Application class to unit test ViewModel

↘锁芯ラ 提交于 2020-04-11 05:50:31
问题 I have a View Model that extends AndroidViewModel class MoveViewModel(application: Application): AndroidViewModel(application),CoroutineScope{ .... } And I want to unit test it but I cannot figure out how to Mock the Application class @Test fun testSearchDataValidation() { val application = Mockito.mock(Application::class.java) val viewModel = MoveViewModel(application) ..... } But when I go to run the test I get an error that Mockito cannot mock Application org.mockito.exceptions.base

How to mock Application class to unit test ViewModel

最后都变了- 提交于 2020-04-11 05:50:06
问题 I have a View Model that extends AndroidViewModel class MoveViewModel(application: Application): AndroidViewModel(application),CoroutineScope{ .... } And I want to unit test it but I cannot figure out how to Mock the Application class @Test fun testSearchDataValidation() { val application = Mockito.mock(Application::class.java) val viewModel = MoveViewModel(application) ..... } But when I go to run the test I get an error that Mockito cannot mock Application org.mockito.exceptions.base

Is it in considered a good practice to mock in integration test? [closed]

巧了我就是萌 提交于 2020-04-10 04:06:07
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Somebody told me @Mock is usually employed only for unit tests but I think is useful for substituting the external parts outside the tested class. Is it correct to mock in integration tests? 回答1: In the end, this is all about wording. When you think of "correct" in its very

小公司和大公司对编写unit test的要求有哪些区别?

大城市里の小女人 提交于 2020-04-05 21:52:22
开发系统的时候公司都会要求开发人员编写unit test,但是不同的公司对编写unit test的要求不尽相同。这里记录一下遇到过的编写unit test的一些写法。 1、创建项目,引入unit test相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 项目中会自动生成一个*ApplicationTests.java类 @SpringBootTest(classes = SpringbootUnitTestDemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class SpringbootUnitTestDemoApplicationTests { @Test void

JUnit 5 使用 Mockito 2 进行测试

柔情痞子 提交于 2020-03-26 02:21:25
3 月,跳不动了?>>> JUnit 5 刚出来那时,也就是第一个版本 5.0.0 时,还不能很好的支持 Mockito 的测试,因为 Mockito 没能跟得那么紧密。那时候 JUnit 5 只能试验性的提供了一个极不正式的 com.example.mockito.MockitoExtension , 看那包名就知道不是来真的,所以决定再等。JUnit 5 不再原生支持 JUnit 4 的 Rule,一切都将是 Extension,那也是要求 Mockito 能够与之俱进。现在等来了,JUnit 5 进化到了 5.2.0, Mockito 也早已有了一个单独的模块 mockito-junit-jupiter 来迎接它。 在 Mockito 2.1.0 的 What's new in Mockito 2 中记述了 JUnit 5 为 Mockito 2 开发了一个 MockitoExtension。追溯到 Mockito 2 的 Release Notes, 我们发现 Mockito 2 官方最早引入 MockitoExtension 的版本是 2.16.3(2018-03023)。我对 Mockito 对 JUnit 5 支持的最新更新是从这个 Pull Request MockitoExtension for JUnit5 得知的。 一句话讲就是现在的 Mockito 2

How to fix java.lang.IllegalStateException: Cannot clear JavaAgentClassRegister. Set method has not been called.?

£可爱£侵袭症+ 提交于 2020-03-22 09:12:09
问题 I am using JunitRunner for running unit tests written using PowerMock and Mockito . Spring Boot Version used is <version>2.0.5.RELEASE</version> pom.xml has below dependencies <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.0-beta.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.0-beta.5</version> <scope>test</scope> <dependency>

Matchers.any() for null value in Mockito

自作多情 提交于 2020-03-12 21:13:11
问题 Suppose I am having this object objectDemo which calls to the method objectDemoMethod with 2 parameters String and null. Now I want to verify that this method was called with Mockito: objectDemo.objectDemoMethod("SAMPLE_STRING", null); I have written this: Mockito.verify(objectDemo, Mockito.times(1)).objectDemoMethod(Matchers.any(String.class), null); but it's giving an error: Invalid use of argument matchers for null value. Is there any another way to pass null value? 回答1: The error message