powermock

Cant mock static functions with powermock-easymock-testng (non-maven project)

匿名 (未验证) 提交于 2019-12-03 01:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: To tell you first, i have tried and tried it again and now i need some help Heres my code package staticPkg; public class Static { public static final String staticMethod() { System.out.println("Static method called"); return "Static called"; } } package staticPkg; public class TargetClass { Static staticClass; public String callHere() { return Static.staticMethod(); } } package staticPkg; import org.easymock.EasyMock; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org

JUnit & Powermock: Native Library already loaded in another classloader

匿名 (未验证) 提交于 2019-12-03 01:39:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: 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 ( GLFW . class ); target

How to mock static method without powermock

邮差的信 提交于 2019-12-03 01:34:52
Is there any way we can mock the static util method while testing in JUnit? I know Powermock can mock static calls, but I don't want to use Powermock. Are there any alternatives? (I assume you can use Mockito though) Nothing dedicated comes to my mind but I tend to use the following strategy when it comes to situations like that: 1) In the class under test, replace the static direct call with a call to a package level method that wraps the static call itself: public class ToBeTested{ public void myMethodToTest(){ ... String s = makeStaticWrappedCall(); ... } String makeStaticWrappedCall(){

Mockito AbstractMethodError on initMocks

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: So I've been struggling pretty much all day trying to get Mockito to work for my Android project. I added everything to my Gradle build file: androidTestCompile 'org.mockito:mockito-core:2.0.29-beta' androidTestCompile "junit:junit:4.12-beta-3" androidTestCompile 'com.google.dexmaker:dexmaker:1.2' androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2' and have tried running a test that doesn't really do anything: @RunWith ( MockitoJUnitRunner . class ) public class LoginActivityTest extends ActivityInstrumentationTestCase2 {

PowerMock java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

匿名 (未验证) 提交于 2019-12-03 01:21:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I created a mock HttpsURLConnection based on an StackExchange answer : import java.net.URL; import javax.net.ssl.HttpsURLConnection; ... @RunWith(PowerMockRunner.class) public class DialogTest { public void mockHttpsUrlConnectionExample() throws Exception { URL mockUrl = PowerMockito.mock(URL.class); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockUrl); HttpsURLConnection mockUrlConnection = PowerMockito.mock(HttpsURLConnection.class); PowerMockito.when(mockUrl.openConnection()).thenReturn(mockUrlConnection); PowerMockito

Unable to get Jacoco to work with Powermockito using offline instrumentation

匿名 (未验证) 提交于 2019-12-03 01:14:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Given that Jacoco doesn't play nicely with PowerMockito when instrumenting "on the fly", I've been trying to configure offline instrumentation in the hope this will give me proper unit test coverage for classes that use PowerMockito. I've setup my pom as below but I still get zero % coverage on my test class. Any help much appreciated as it's driving me slowly bonkers! 4.0.0 mandy jacoco-test war 1.0-SNAPSHOT jacoco-test Maven Webapp http://maven.apache.org 1.5.4 0.7.1.201405082137 org.jacoco org.jacoco.agent runtime ${jacoco.version} test

Mockito + PowerMock + TestNG + Libgdx

匿名 (未验证) 提交于 2019-12-03 01:06:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying add the powermock library to the working project, but I'm getting errors. How I add it: 1) AbsTest extends PowerMockTestCase 2) Build.gradle dependencies 3) In some test add @PrepareForTest({SomeClass.class}) . After this step this error occured. In build.gradle all libraries are included. Error occured in a place where initialize HeadlessApplication for use Gdx.* static vars. All tests in project extend this class: abstract public class AbsTest extends PowerMockTestCase { static { initGdx(); } protected static void initGdx() { //

PowerMock fails with JerseyTest

匿名 (未验证) 提交于 2019-12-03 01:06:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We're writing unit tests for a Jersey Java REST service using PowerMock. We have to use PowerMock because we use numerous static methods in a legacy system. We're using PowerMock with EasyMock. Whenever we try to inherit from JerseyTest, the whole framework fails. It's a gradle based project, and the pertinent build.gradle lines are below. testCompile 'junit:junit:4.12' testCompile group: 'org.easymock', name: 'easymock', version: '3.5.1' testCompile group: 'org.easymock', name: 'easymockclassextension', version: '3.2' testCompile group:

How to mock riak java client?

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to unit test code that uses com.basho.riak:riak-client:2.0.0. I mocked all riak client classes and was hoping to get a useless but working test. However, this fails with a null pointer: java.lang.NullPointerException at com.basho.riak.client.api.commands.kv.KvResponseBase.convertValues(KvResponseBase.java:243) at com.basho.riak.client.api.commands.kv.KvResponseBase.getValue(KvResponseBase.java:150) at com.basho.riak.client.api.commands.kv.FetchValue$Response.getValue(FetchValue.java:171) My test looks like this: @Test public void

Mocking behaviour resets after each test with PowerMock

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm writing unit tests using PowerMock, mocking behaviour of some util classes. Defining behaviour once for test class (by @BeforeClass annotation) causes: first test invocation to return mocked value second test invocation to return real method return value Sample code: import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner