powermockito

Power Mockito showing error for org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner cannot be resolved

自古美人都是妖i 提交于 2019-12-13 00:52:46
问题 Mock the static method in class. When using PowerMockito in test class its showing error as The type org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner cannot be resolved. It is indirectly referenced from required .class files Added dependencies as below in gradle project. testCompile 'junit:junit:4.12' testCompile 'org.easymock:easymock:3.1' testCompile 'org.powermock:powermock-core:1.6.1' testCompile 'org.powermock:powermock-module-junit4:1.6.1' testCompile 'org

How to mock current date?

落花浮王杯 提交于 2019-12-12 16:48:25
问题 I have a method in my code which verifies that current day is business. it checks database calendar for this. method looks like this: public boolean iBusinessDayToday() { LocalDate today = LocalDate.now(); //logic with today } I need to write unit tests for this. I want that unit test doesn't depends in week day. What do you think need this issue to use powerMockito to mock LocalDate.now() ? 回答1: An alternative would be to pass the clock to the method: public boolean iBusinessDayToday() {

How to unit testing spring boot rest controller and exception handler using power mock

懵懂的女人 提交于 2019-12-12 13:35:28
问题 I am having a simple Spring boot application which contains Employee controller which returns the Employee names if the year passed is greater than 2014 and if the it is not less than 2014 then I am throwing a custom exception and handling it in Exception Handler. I want to unit test the exception flow using powermock but I am not sure how to do it. I have gone through some links but unable to understand. Currently I am getting java.lang.IllegalArgumentException: WebApplicationContext is

Executing Initialization Code only once for multiple test classes

倾然丶 夕夏残阳落幕 提交于 2019-12-12 11:35:50
问题 I am writing unit test cases for my code. I am using PowerMockito with Junit. I have written an initialization code which will take care of all the initialization stuff in my application. Below is the way my code is structured: Class ServiceInitializer{ public static isInitialized = Boolean.FALSE; public static void initialize(){ //Initilization Code Goes Here isInitialized = Boolean.TRUE; } } @RunWith(PowerMockRunner.class) class BaseTest{ @Before public void preTest(){ //some code } static{

How to mock keystore class and assign mock behavior to its methods?

帅比萌擦擦* 提交于 2019-12-12 10:49:39
问题 I have the below method which I need to write unit tests for. But I cannot spy the class KeyStore. It throws the below exception. org.mockito.exceptions.base.MockitoException: Unable to create mock instance of type 'KeyStore' I can mock it though. But when I go to assign the behavior for the mock methods it throws exceptions. Methods I called and the exceptions I got are as follows. when(keyStoreMock.getCertificate(anyString())).thenReturn(certificateMock); java.security.KeyStoreException:

Unable to mock URL class using PowerMockito/Mockito

核能气质少年 提交于 2019-12-12 10:46:30
问题 I am trying to use PowerMockito to mock the creation of the java.net.URL class in my code that I'm testing. Basically, I want to prevent the real HTTP request from occurring and instead 1) check the data when the request is made and 2) supply my own test data back on a mocked response. This is what I'm trying: @RunWith(PowerMockRunner.class) @PrepareForTest({ URL.class, MockedHttpConnection.class }) public class Test { URL mockedURL = PowerMockito.mock(URL.class); MockedHttpConnection

org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name '' with parameter types: [ java.util.LinkedList ]

霸气de小男生 提交于 2019-12-12 10:24:52
问题 I am developing Test classes using PowerMock , as all my Legacy code is written using Private Static methods(). When running the test class, I am getting below error: org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name 'insertPaytmBilling' with parameter types: [ java.util.LinkedList ] in class com.xyz.PaytmBilling. at org.powermock.reflect.internal.WhiteboxImpl.throwExceptionIfMethodWasNotFound(WhiteboxImpl.java:1122) at org.powermock.reflect.internal

Singleton returning new instance when accessed from test method

空扰寡人 提交于 2019-12-12 04:18:55
问题 I am using Junit 4.12 with PowerMock 1.6 with Mockito. I have also used PowerMockRule library as described here. I am trying to execute initialization code for all of my test cases exactly once as described in this SO Thread. Its executing the initialization code exactly one time however, if I do ServiceInitializer.INSTANCE inside test method it returns me new object. I am not able to understand this behavior. Does anyone have any idea why this is happening? If I execute my code without

Mocking final class with parameterized constructor

自古美人都是妖i 提交于 2019-12-12 03:06:49
问题 I have a final class as below public class firstclass{ private String firstmethod(){ return new secondclass("params").somemethod(); } } public final class secondclass{ secondclass(String params){ //some code } public String somemethod(){ // some code return somevariable"; } } I have to here test first class so I have mocked this as below secondclass classMock = PowerMockito.mock(secondclass .class); PowerMockito.whenNew(secondclass .class).withAnyArguments().thenReturn(classMock); Mockito

Mock Protected Parent Method When the Parent is in a Different Package

我怕爱的太早我们不能终老 提交于 2019-12-11 12:32:36
问题 I need to mock a protected method in the parent class of my class under test but the parent class is in a different package so my test class cannot access that method so I cannot mock it. There's gotta be a solution for this issue without refactoring I need to use Powermock and Mockito. Here's the JARs mockito-all 1.10.8 powermock-core 1.6.1 powermock-module-junit4 1.6.1 powermock-api-mockito 1.6.1 junit 4.12 This is legacy code so I cannot refactor, but here's the simplified code. Parent