powermock

Mockito调用静态方法和void方法

匿名 (未验证) 提交于 2019-12-03 00:36:02
1 mock 静态方法 mockito库并不能mock静态方法,需要依赖powermock 第一步:给类添加注解 // 静态类优先加载,所以需要提前告诉powermock哪些静态类需要mock public class SupplierServiceImplTest extends PowerMockTestCase {} 第二步:mock使用 @Test (expectedExceptions = BusinessException.class) public void testAddSupplierAccount_genIdentityNoError () { // 告诉powermock,需要mock该类的所有静态方法 PowerMockito.mockStatic(PasswordGenerator.class); final SupplierAccountDto supplierAccountDto = new SupplierAccountDto(); supplierAccountDto.setName( "小明" ); final String randomPWd = "666" ; PowerMockito.when(supplierDao.selectByEmail(anyString())) .thenReturn( new ArrayList

Mockito调用静态方法和void方法

匿名 (未验证) 提交于 2019-12-03 00:36:02
1 mock 静态方法 mockito库并不能mock静态方法,需要依赖powermock 第一步:给类添加注解 // 静态类优先加载,所以需要提前告诉powermock哪些静态类需要mock public class SupplierServiceImplTest extends PowerMockTestCase {} 第二步:mock使用 @Test ( expectedExceptions = BusinessException . class ) public void testAddSupplierAccount_genIdentityNoError () { // 告诉powermock,需要mock该类的所有静态方法 PowerMockito . mockStatic ( PasswordGenerator . class ); final SupplierAccountDto supplierAccountDto = new SupplierAccountDto (); supplierAccountDto . setName ( "小明" ); final String randomPWd = "666" ; PowerMockito . when ( supplierDao . selectByEmail ( anyString ())) .

How can I create dynamic proxy for final Class?

纵饮孤独 提交于 2019-12-03 00:03:05
In short: 1. I have some final class that I want to create dynamic proxy for it. How can I do it? 2. Can I convert MethodHandle to Method? Details First of all, does exists any API to convert MethodHandle to Method? Something like in java.lang.invoke.MethodHandles public MethodHandle unreflect(Method m) throws IllegalAccessException; but the opposite way arrond? Let say I want to create dynamic java.lang.reflect.Method. It is defiend as public final class Method extends AccessibleObject implements GenericDeclaration, Member ; So, if I want to use JDK Dynamic proxy I must use some interface

PowerMock throws NoSuchMethodError (setMockName)

北城余情 提交于 2019-12-02 22:55:29
I'm trying to mock a constructor using PowerMockito but every time I run the test I get the following error: java.lang.NoSuchMethodError: org.mockito.internal.creation.MockSettingsImpl.setMockName(Lorg/mockito/mock/MockName;)Lorg/mockito/internal/creation/settings/CreationSettings; at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:107) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60) at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubstituteMock

PowerMock + Mockito VS Mockito alone

馋奶兔 提交于 2019-12-02 14:17:04
Can anyone please summarize, what exactly features gives you adding PowerMock on top of the Mockito? So far I've found these: mock static, final and private methods remove static initializers allow mocking without dependency injection - this one isn't clear to me. Can you elaborate? Does it add anything else? Can you please sum up in several lines? And do I need to sacrifice something when using PowerMock? I don't know of other benefits offhand, but I want to address 2 of your sub-questions (and this is way too long for a comment): allow mocking without dependency injection - this one isn't

Mock、Powermock学习记录

南笙酒味 提交于 2019-12-02 11:49:12
背景 工作中经常用到单测,某对单测掌握的不好,所以趁此学习、总结一下。 主要参考:https://www.jianshu.com/p/0c2480b1709e、https://www.cnblogs.com/ljw-bim/p/9391770.html 一、不依赖外部方法的单测 1、待测试类 package com.shuimutong.demo.junit; /** * 无依赖方法 * @ClassName: NotStaticMethod * @Description:(这里用一句话描述这个类的作用) * @author: 水木桶 * @date: 2019年10月26日 上午10:37:09 * @Copyright: 2019 [水木桶] All rights reserved. */ public class NoRelayMethod { public static int ADD_NUM = 2; public static int staticAddTwo(int num) { return num + ADD_NUM; } /** * 非静态方法 * @param num * @return */ public int notStaticAddTwo(int num) { return num + ADD_NUM; } /** * 私有非静态方法 * @param

testing powermock simulate http server time out for client call

对着背影说爱祢 提交于 2019-12-02 10:05:10
i need to write testcase for connectTimeout and SocketTimeout exception. Am using powerMock to create mock objects. Below is my code. But am getting null pointer exception for my mock objects. Any help appreciated package com.util; import java.net.ConnectException; import java.net.SocketTimeoutException; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import org.json.JSONObject; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock

Powermock and Mockito. Avoid static initialization for a class while mocking and stubing the same class

a 夏天 提交于 2019-12-02 09:14:30
问题 Suppose I have a class named Util with static fields: public class Util { public static field = Param.getValue("param1"); } and the class Param look like this: public class Param { public static field = SomeClass.getValue("someValue"); } I want to mock and stubb Param.getValue("param1") inside Util, but at the same time I want suppress static initialization for Param class. How can I achieve this? This is my first attempt but it's not working @RunWith(PowerMockRunner.class) @PrepareForTest(

Mockito - how to mock/verify a method call which accepts a new object?

与世无争的帅哥 提交于 2019-12-02 09:03:29
I have a method (method1) that I'd like to test, which based on parameters provided creates an object and calls another method (method2). So I'm mocking method2, which accepts an object (sampleObj). public void method1(booleanParam) { if(booleanParam){ List<SampleObj> fooList = new ArrayList<SampleObj>; fooList.add(new SampleObj("another param")); anotherService.method2(fooList); } //some other smart logic here } And here's my test with same obfuscated names (sorry if I missed any typo): public void testMethod1() { AnotherService mockedAnotherService = PowerMockito.mock(AnotherService.class);

Powermock mocking void method throws error

佐手、 提交于 2019-12-02 08:03:08
I am using Powermockito, mockito with TestNG. My test class extends PowerMockTestCase. I want to mock a void method. For that I used following sample syntax, @PrepareForTest(TestClass.class) class Sample extends PowerMockTestCase{ @BeforeClass public void beforeClass(){ TestClass obj = PowerMockito.spy(TestClass.getInstance()); PowerMockito.doNothing().when(obj).voidMethodName(Matchers.any(Type1.class), Matchers.anyString(), Matchers.any(Type2.class)); } When I give this, I get : FAILED CONFIGURATION: @BeforeClass beforeClass org.mockito.exceptions.misusing.UnfinishedStubbingException: