powermock

PowerMockito: got InvalidUseOfMatchersException when use matchers mocking static method

末鹿安然 提交于 2020-01-11 08:48:05
问题 When I'm testing this static method public class SomeClass { public static long someMethod(Map map, String string, Long l, Log log) { ... } } with import org.apache.commons.logging.Log; @RunWith(PowerMockRunner.class) //@PrepareForTest(SomeClass.class) public class Tests { @Test public void test() { ... PowerMockito.mockStatic(SomeClass.class); Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L); ... } } I got InvalidUseOfMatchersException . My

TestNG + Mockito + PowerMock - verifyStatic() does not work

↘锁芯ラ 提交于 2020-01-09 11:22:29
问题 I am new TestNG and unit-testing in general. I am using TestNG 6.9.6 with Mockito 1.10.19 and PowerMock 1.6.4. I want to verify whether the myMethod() method in MyService class internally calls the static method Util.myStaticMethod with the correct arguments. Since verification of static methods is not natively supported in Mockito, I am using PowerMock along with it. My Test class is shown below: public class MyTest { private MyService myService; @Captor ArgumentCaptor<String> argCaptor;

Spock-Groovy实战

血红的双手。 提交于 2020-01-08 21:16:23
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前言 之前写单测都是用mockito然后基于java来写单测,但是操作下来写单测确实很占用时间,而且有时候为了提高条件覆盖率,需要写大量的代码,那么该咋办呢? 经过小伙伴的推荐,我去试了下Spock + Groovy,果然很好用,所以在这里特地记下来。 操作 那就不多废话了,如果想了解Spock理论知识的可以自行google一波。 安装Groovy环境 官网地址: http://www.groovy-lang.org/download.html 找一个自己喜欢的路径将他下载下来并解压 配置环境变量 vi ~/.bash_profile 加入如下配置 # groovy export GROOVY_PATH=/Users/***/groovy/groovy-2.5.8/bin export PATH=$PATH:$GROOVY_PATH 验证 groovy -v 这样的话Groovy环境就ok啦! 新建groovy目录 接下去就自己新建路径,然后new Groovy Class即可 注意点: 如果想要统计覆盖率的话,记得要在idea中配置插件,如下: 写法 基本的语法大家可以自行查找一波,可以看W3C school。地址: https://www.w3cschool.cn/groovy/

一文全面了解Android单元测试

浪尽此生 提交于 2020-01-08 10:28:04
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前言 成为一名优秀的Android开发,需要一份完备的 知识体系 ,在这里,让我们一起成长为自己所想的那样~。 ==》完整项目单元测试学习案例 众所周知,一个好的项目需要不断地打造,而一些有效的测试则是加速这一过程的利器。本篇博文将带你了解并逐步深入Android单元测试。 什么是单元测试? 单元测试就是针对类中的某一个方法进行验证是否正确的过程,单元就是指 独立的粒子 ,在Android和Java中大都是指方法。 为什么要进行单元测试? 使用单元测试可以提高开发效率,当项目随着迭代越来越大时,每一次编译、运行、打包、调试需要耗费的时间会随之上升,因此,使用单元测试可以不需这一步骤就可以对单个方法进行功能或逻辑测试。 同时,为了能测试每一个细分功能模块,需要将其相关代码抽成相应的方法封装起来,这也在一定程度上改善了代码的设计。因为是单个方法的测试,所以能更快地定位到bug。 单元测试case需要对这段业务逻辑进行验证。在验证的过程中,开发人员可以 深度了解业务流程 ,同时新人来了看一下项目单元测试就知道 哪个逻辑跑了多少函数,需要注意哪些边界 ——是的,单元测试做的好和文档一样 具备业务指导能力。 Android测试的分类 Android测试主要分为三个方面: 单元测试(Junit4、Mockito

error in mocking nested calls

时光怂恿深爱的人放手 提交于 2020-01-05 07:08:43
问题 I have a simple line of Code: DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil .getDraftCampaignDetailsfromCreateDraftRequest(request)); I am trying to mock it like this: ConvertionUtil action1 = PowerMockito.mock(ConvertionUtil.class); when(action1.getDraftCampaignDetailsfromCreateDraftRequest(request)).thenReturn(details); when(draftCampaignI.createDraftCampaign(details)).thenReturn(details); But I am getting this error: when() requires an argument which

JUnit & Powermock: Native Library already loaded in another classloader

不羁岁月 提交于 2020-01-04 05:14:10
问题 I have some test classes that need to verify that GLFW-functions were called. But when I want to execute all tests in IntelliJ then I get the error: Native Library lwjgl.dll already loaded in another classloader I use Powermock to verify that the static methods have been called: @RunWith(PowerMockRunner.class) @PrepareForTest({GLFW.class}) public class GlfwWindowImplTest { // ... @Test public void update_swapsBufferAndPollsEvents() { GlfwWindowImpl target = new GlfwWindowImpl(1L); mockStatic

Gradle JRE vs JDK please add lib/tools.jar from your JDK

久未见 提交于 2020-01-04 04:42:06
问题 I'm running java tests with gradle. here is the exception I have: java.lang.RuntimeException: java.lang.IllegalStateException: Unable to load Java agent; please add lib/tools.jar from your JDK to the classpath at org.powermock.modules.agent.PowerMockClassRedefiner.redefine(PowerMockClassRedefiner.java:59) at org.powermock.modules.agent.support.PowerMockAgentTestInitializer.redefine(PowerMockAgentTestInitializer.java:49) at org.powermock.modules.agent.support.PowerMockAgentTestInitializer

Mock a single static method using PowerMock and TestNG

若如初见. 提交于 2020-01-03 15:34:51
问题 class StaticClass { public static String a(){ return "a"; } public static String ab(){ return a()+"b"; } } I want to mock StaticClass::a so that it returns "x" and the call to StaticClass.ab() results in "xb" ... I find it very hard in PowerMock and TestNG... the exact code I am testing righ now: class StaticClass { public static String A() { System.out.println("Called A"); throw new IllegalStateException("SHOULD BE MOCKED AWAY!"); } public static String B() { System.out.println("Called B");

How to verify if method is called on System under test (not a mock)

不问归期 提交于 2020-01-03 10:02:18
问题 I'm trying to write a unit test that needs to confirm if a method is called or not. I'm using JUnit, Mockito and PowerMock. public class Invoice { protected void createInvoice() { // random stuff here markInvoiceAsBilled("57"); } protected void markInvoiceAsBilled(String code) { // marked as billed } } So, here my system under test is Invoice . I'm running this test: public class InvoiceTest { @Test public void testInvoiceMarkedAsBilled() { Invoice sut = new Invoice(); Invoice sutSpy = spy

How to verify if method is called on System under test (not a mock)

心不动则不痛 提交于 2020-01-03 10:02:09
问题 I'm trying to write a unit test that needs to confirm if a method is called or not. I'm using JUnit, Mockito and PowerMock. public class Invoice { protected void createInvoice() { // random stuff here markInvoiceAsBilled("57"); } protected void markInvoiceAsBilled(String code) { // marked as billed } } So, here my system under test is Invoice . I'm running this test: public class InvoiceTest { @Test public void testInvoiceMarkedAsBilled() { Invoice sut = new Invoice(); Invoice sutSpy = spy