powermock

PowerMock - Mocking static system class throws IllegalStateException

百般思念 提交于 2019-12-22 12:26:18
问题 I have the following code public class A{ public void createFile() { File tempXmlFile = null; String extension = ".xml"; String name = "someName"; try { tempXmlFile = File.createTempFile(name, extension); if (tempXmlFile.exists()) { tempXmlFile.delete(); } } catch (IOException e) { System.out.println(e.getStackTrace()); } } } @RunWith(PowerMockRunner.class) @PrepareForTest(A.class) public class testA extends TestCase{ private A classUnderTest; @Override @Before public void setUp() {

when use junit4 + powermock to execute all test suites , I got an error : swt-win32-3650.dll already loaded in another classloader

强颜欢笑 提交于 2019-12-22 08:12:04
问题 when use junit4 + powermock to execute all test suites , I got an error : swt-win32-3650.dll already loaded in another classloader alltest.java: @RunWith( Suite.class ) @SuiteClasses( {test1.class, test2.class} ) public class AllTests { } test1.java @RunWith( PowerMockRunner.class ) @PrepareOnlyThisForTest( {Object.class} ) public class test1 extends TestCase { @Test public void testcase() { Shell sh = Mockito.mock( Shell.class ); PowerMockito.when( sh.getText() ) .thenReturn( this.getClass()

How to mock An Interface Java PowerMockito

删除回忆录丶 提交于 2019-12-22 05:06:15
问题 I m trying to mock an interface. public interface FlowCopyParamsBusinessManager { List<FlowCopyParams> findByAppli(String application, String sourcePattern) throws FlowCopyParamsBusinessException; } In my code, when i call this method findByAppli , i would like to return a list of FlowCopyParams. List<FlowCopyParams> lstFlowCopyParams = flowCopyParamsBusinessManager.findByAppli( "TOTO","TATA); Here my try in the class test: @BeforeClass public static void mockBeanIn() throws Exception { List

Using PowerMock and Mockito in an Android Instrumentation test - Error - Duplicate files - org.mockito.plugins.MockMaker

霸气de小男生 提交于 2019-12-22 04:44:10
问题 I”m trying to use PowerMock to mock a class with a static method, but specifically I wish to do this within an Android Instrumentation test. To be clear I wish to run the test on a real Android device or an emulator. I am using Android Studio (1.5.1) and Gradle (1.5.0). To avoid any red herrings I have created a really basic and rather crude ‘hello world’ app. This app simply shows 2 pieces of text, one retrieved from a static method and one from a non-static method. I have written

RunWith(PowerMockRunner.class) does not work with package annotation

自古美人都是妖i 提交于 2019-12-22 04:38:18
问题 I am trying to have RunWith(PowerMockRunner.class) working with my existing package annotation. Versions: powermock 1.4.12 mockito 1.9.0 junit 4.8.2 package-info.java // this is for the package annotation @TestAnnotation(version="1.0") package com.smin.dummy; TestAnnotation.java // this is the the metadata annotation class for package "com.smin.dummy" package com.smin.dummy; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PACKAGE) public @interface

NoClassDefFoundError for MockitoInvocationHandler class

梦想的初衷 提交于 2019-12-21 12:18:50
问题 I am using mockito-all-1.9.5-rc1.jar and powermock-mockito-1.4.12-full.jar . When I run this simple unit test for mocking final method in non-final class. import static org.junit.Assert.assertEquals; 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; @RunWith(PowerMockRunner.class) @PrepareForTest(ABC.class) public class

Can I mock a superclass's constructor with Mockito/Powermock?

不羁的心 提交于 2019-12-21 12:13:33
问题 Is it possible using Mockito and optionally Powermock to mock a superclass S such that any calls to the superclass to S (including calls to the S() constructor) are mocked? So using the below example, if I replace S with MockS using Mockito, will the call to super() use the constructor in MockS ? class S { S() { // Format user's hard drive, call 911, and initiate self-destruct } } class T extends S { T() { super(); } } class Test { @Mock private S mockS; new T(); // T's call to super() should

Mocking a Private Variable that is Assumed to Exist

独自空忆成欢 提交于 2019-12-21 06:58:07
问题 How can you get a mock object in at runtime when it is not created/initialized in the class you are testing, it is not static (singleton pattern), or you don't have some sort of test constructor to hook into? In a class that I am writing some unit testing for, I have come across a scenario I haven't encountered/solved yet. I have a JMS resource (a QueueConnectionFactory for reference, but it shouldn't matter), that is a private variable of the class I am testing. Since it has the javax

How to mock super reference (on super class)?

我们两清 提交于 2019-12-21 05:21:11
问题 Sometimes when I write unit tests I should to mock reference to superclass. I have read this question: question This answer answer with DI advice to refactor code. But I cannot it this answer another answer is not suitable if superclass method is enough big. In my case I have very big code. Yes I know that it is brokes SOLID OOD principes but I just should to write test. I have not enough time for refactor. said question was asked 4 years ago! Does currently Mockito or Powermock can resolve

How to mock super reference (on super class)?

蓝咒 提交于 2019-12-21 05:21:05
问题 Sometimes when I write unit tests I should to mock reference to superclass. I have read this question: question This answer answer with DI advice to refactor code. But I cannot it this answer another answer is not suitable if superclass method is enough big. In my case I have very big code. Yes I know that it is brokes SOLID OOD principes but I just should to write test. I have not enough time for refactor. said question was asked 4 years ago! Does currently Mockito or Powermock can resolve