powermock

Error in junit while mocking

时间秒杀一切 提交于 2019-12-09 01:23:25
问题 I am new to Junit,Below is the junit code which i am running. package com.de.base.util.general; import static org.junit.Assert.*; import static org.mockito.Mockito.when; import java.util.HashMap; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.powermock.api.mockito.PowerMockito; @RunWith(MockitoJUnitRunner.class) public class JReportUtilTest {

Cannot mock/spy final class using PowerMockito.spy()

亡梦爱人 提交于 2019-12-08 15:23:28
I'm trying to use PowerMockito to create a spy of a final class but I keep getting the following error, even though I am using PowerMockito's spy() method in place of Mockito's: java.lang.IllegalArgumentException: Cannot subclass final class class com.whoever.WidgetUploadClient My test case looks something like this: ... import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.spy; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(RobolectricTestRunner.class) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})

Mocking HttpClient.execute issues: Mockito

我与影子孤独终老i 提交于 2019-12-08 14:00:17
问题 I am trying to test this method. @Override public JSON connectResource() throws IOException { //get the location and credentials for the certificates System.setProperty("javax.net.ssl.trustStore", "C:/Program Files/Java/jdk1.7.0_40/jre/lib/security/cacerts"); System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); HttpRequest httpRequest = new HttpGet(url); System.out.println("hello"); httpRequest.addHeader("Accept", "application/json"); HttpResponse response = httpClient.execute(

Unit testing static method which uses a resource bundle

只愿长相守 提交于 2019-12-08 08:04:20
问题 Ive read so many articles on using Powermock and Mockito and tried so many different ways, but I still cant figure out the way to unit test the below static method. public static Map<String, String> getEntries() { Map<String, String> myEntriesMap = new TreeMap<String, String>(); ResourceBundle myEntries = ResourceBundle.getBundle(ENTRIES_BUNDLE); Enumeration<String> enumList = myEntries.getKeys(); String key = null; String value = null; while (enumList.hasMoreElements()) { key = enumList

SuppressStaticInitializationFor(Powermock)

僤鯓⒐⒋嵵緔 提交于 2019-12-08 07:22:48
问题 I have a public class that has static variables, static blocks and static functions. I am testing one of the static function(say x) from my tester class , I have suppressed static block by using @SuppressStaticInitializationFor at the class level(Powermock) in the tester class . Now when I am running Jnunit test from my tester class I am getting null pointer exception when control reaches the above function that is using the static variables of the class.So my question is that does

PowerMock expectNew how to specify the type of the parameters

穿精又带淫゛_ 提交于 2019-12-08 07:20:38
问题 When I use PowerMock to mock the constructor,I want to specify the type of the paremeters. I use the method PowerMock.expectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) If I want to specify the String type, what shoud I fill in the parameterTypes ? 回答1: Given these example classes: public class MyClass { public MyClass(String theParam) { //Some interesting code... } } public class MyFactory { public MyClass createMyClass() { return new MyClass("foo"); } } Then you would

Cannot mock/spy final class using PowerMockito.spy()

≡放荡痞女 提交于 2019-12-08 06:38:35
问题 I'm trying to use PowerMockito to create a spy of a final class but I keep getting the following error, even though I am using PowerMockito's spy() method in place of Mockito's: java.lang.IllegalArgumentException: Cannot subclass final class class com.whoever.WidgetUploadClient My test case looks something like this: ... import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.spy; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate

How to test method Jsoup.connect (static) using PowerMock?

若如初见. 提交于 2019-12-08 02:53:32
问题 This is my code: public void analyze(String url) throws SiteBusinessException { Document doc = null; Response response = null; try { response = Jsoup.connect(url).execute(); doc = Jsoup.connect(url).get(); } catch (IOException e) { LOGGER.warn("Cannot analyze site [url={}, statusCode={}, statusMessage={} ]", new Object[] {url, response.statusCode(), response.statusMessage()}); throw new SiteBusinessException(response.statusMessage(), String.valueOf(response.statusCode())); } } How can I test

NoClassDefFoundError: org/hamcrest/Matchers using PowerMock-OSGi

天涯浪子 提交于 2019-12-08 02:39:40
问题 I get an NoClassDefFoundError for org.hamcrest.Matchers, when i run my Test as OSGi PlugIn test, but when i run it as plain JUnit test everthing works as expected. I am using the OSGi version of PowerMock and have all neccessary dependencies in my launch config. What i am doing wrong? It seems like the Testrunner doesnt see the class, for some reason. Edit: I created a reduced sample project and figured out that the Problem only appear when i use @PrepareForTest(XXX.class) in my class

PowerMock + Robolectric + Dagger2

末鹿安然 提交于 2019-12-08 00:25:35
问题 I test custom view class which contain: android ui elements some logic static methods callings dagger2 dependencies So i use next tools for testing Robolectric for UI elements mocking unit tests for logic testing PowerMock for static methods mocking Robolectric + PowerMock integration problem is known and solution is known - https://github.com/robolectric/robolectric/wiki/Using-PowerMock But with this solution dagger2 dependencies fail. Attention to code. My custom view: public class