powermock

Can we create a mocked instance of java.lang.Class with PowerMock?

若如初见. 提交于 2021-02-20 13:53:37
问题 I need to write a test that mocks a instance of the java.lang.Class class. Is this possible via PowerMock? I tried to do following: PowerMock.createMock(Class.class); And the result is: java.lang.IllegalAccessError: java.lang.Class at sun.reflect.GeneratedSerializationConstructorAccessor12.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator

Can we create a mocked instance of java.lang.Class with PowerMock?

空扰寡人 提交于 2021-02-20 13:53:26
问题 I need to write a test that mocks a instance of the java.lang.Class class. Is this possible via PowerMock? I tried to do following: PowerMock.createMock(Class.class); And the result is: java.lang.IllegalAccessError: java.lang.Class at sun.reflect.GeneratedSerializationConstructorAccessor12.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator

Can we create a mocked instance of java.lang.Class with PowerMock?

不想你离开。 提交于 2021-02-20 13:53:11
问题 I need to write a test that mocks a instance of the java.lang.Class class. Is this possible via PowerMock? I tried to do following: PowerMock.createMock(Class.class); And the result is: java.lang.IllegalAccessError: java.lang.Class at sun.reflect.GeneratedSerializationConstructorAccessor12.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator

Mocking SecureRandom::nextInt()

安稳与你 提交于 2021-02-08 01:57:37
问题 I have a class that uses a SecureRandom instance and grabs the next random number. Lets say the example is: public class ExampleClass() { public void method() { Random sr = new SecureRandom(); System.out.printf("%d %n", sr.nextInt(1)); System.out.printf("%d %n", sr.nextInt(1)); } } Test code @RunWith(PowerMockRunner.class) public class ExampleClassTest { ... @Test @PrepareOnlyThisForTest(SecureRandom.class) public void mockedTest() throws Exception { Random spy = PowerMockito.spy(new

Mocking SecureRandom::nextInt()

試著忘記壹切 提交于 2021-02-08 01:57:04
问题 I have a class that uses a SecureRandom instance and grabs the next random number. Lets say the example is: public class ExampleClass() { public void method() { Random sr = new SecureRandom(); System.out.printf("%d %n", sr.nextInt(1)); System.out.printf("%d %n", sr.nextInt(1)); } } Test code @RunWith(PowerMockRunner.class) public class ExampleClassTest { ... @Test @PrepareOnlyThisForTest(SecureRandom.class) public void mockedTest() throws Exception { Random spy = PowerMockito.spy(new

Error while using powermock in JUnit

心已入冬 提交于 2021-02-07 07:59:35
问题 While I am running JUnit file I am getting the following type of error. Here is the complete stacktrace below: org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not found in class org.junit.internal.runners.MethodValidator. at org.powermock.reflect.internal.WhiteboxImpl.getInternalState(WhiteboxImpl.java:643) at org.powermock.reflect.Whitebox.getInternalState(Whitebox.java:308) at org.powermock.modules.junit4.internal.impl.testcaseworkaround

Can JMockit mock constructors with any argument?

淺唱寂寞╮ 提交于 2021-01-29 13:46:03
问题 I am replacing PowerMock with JMockit in old unit testing case. Below is the PowerMock sample code which mock the File.class constructors with any argument . Can JMockit mock constructors with any argument like it ? The situation is this:myFile is a mock. And I want to simulate returning myFile when calling any constructor in the File class..So What is the code like. // PowerMock whenNew(File.class).withAnyArguments().thenReturn(myfile); // JMockit new Expectations() {{ new File(anyString);

How to mock external dependencies for final objects?

走远了吗. 提交于 2021-01-28 12:31:43
问题 public class A{ private final B b; public void meth() { //Some code Integer a = b.some_method(a,fun(b)); //Some code } private fun(int b) { return b; } } when(b.some_method(anyInt(),anyInt())).thenReturn(100) How to mock the externally dependency when writing unit tests for class A. When i mock dependency in the above way, the value of "a" is not getting assigned to 100 as expected. 回答1: Actually the answer of Jakub is correct. Maybe you need an example to understand how to do it. Check the

How can I mock Google's Geocoding API request using mockito/powermock?

不打扰是莪最后的温柔 提交于 2021-01-27 04:25:30
问题 I want to unit test this method using mockito/powermock: @Service public class GoogleApiService { private static final Logger logger = LoggerFactory.getLogger(GoogleApiService.class); private static final String LANGUAGE = "es"; private List<AddressType> addressTypes = Arrays.asList( AddressType.LOCALITY, AddressType.ADMINISTRATIVE_AREA_LEVEL_2, AddressType.ADMINISTRATIVE_AREA_LEVEL_1, AddressType.COUNTRY ); @Autowired private GeoApiContext geoApiContext; public String getLocalityFromLatLng

Mockito 'Misplaced argument detected here' in Java

﹥>﹥吖頭↗ 提交于 2020-12-25 01:39:05
问题 So I have this Mockito unit test: @Test public void createCard() { when(jwtServiceMock.getId(anyString())).thenReturn(validUserToken); when(profileServiceMock.getProfile(validUserToken)).thenReturn(mock(Profile.class)); when(cardServiceMock.countViewableCardsCreatedOrOwnedBy(anyObject())).thenReturn(5L); when(cardServiceMock.countCardsCreatedOrOwned(anyObject())).thenReturn(10L); final Card expectedCard = getCard(); when(cardServiceMock.createCard(anyString(), anyListOf(String.class),