powermockito

Instantiating a Class with private constructor using Java/Mockito/PowerMockito

大城市里の小女人 提交于 2020-08-07 05:47:38
问题 I am writing a test case using JUnit and the method under test takes a final class with a private constructor as a parameter. Since I cannot instantiate it with the new keyword I tried using Mockito but found out that Mockito doesn't like final class . I went to use PowerMockito which seemed reasonable to me but PowerMockito.mockStatic(Field.class); is a void method and I need a reference of Field so that I can pass it as an argument while invoking the method. I want to catch

Instantiating a Class with private constructor using Java/Mockito/PowerMockito

白昼怎懂夜的黑 提交于 2020-08-07 05:47:34
问题 I am writing a test case using JUnit and the method under test takes a final class with a private constructor as a parameter. Since I cannot instantiate it with the new keyword I tried using Mockito but found out that Mockito doesn't like final class . I went to use PowerMockito which seemed reasonable to me but PowerMockito.mockStatic(Field.class); is a void method and I need a reference of Field so that I can pass it as an argument while invoking the method. I want to catch

Mockito, mock and change Calendar.getIstance() [duplicate]

橙三吉。 提交于 2020-06-26 07:07:09
问题 This question already has answers here : java: how to mock Calendar.getInstance()? (6 answers) Closed 3 years ago . How can i mock and Change time? I use Calendar.getInstance() inside the Method under test this is the method public List<Object> chechAuth(){ Calendar from = Calendar.getInstance(); if(jobService.findJobByCheckAuth(from.getTime(), true).isEmpty()){ from.add(Calendar.YEAR, -10); } Calendar to = Calendar.getInstance(); to.add(Calendar.MONTH, 2); System.out.println("from: "+from

Junit Mockito for global java.util.Map

为君一笑 提交于 2020-06-17 09:40:29
问题 I am trying to Test a method but it has a global variable which is null, Please guide me so I can assign value to global variable i.e. a Map My Junit: public class ErrorTest { @Mock private DataSource db; @Mock private JdbcTemplate jdbcTemplate; @InjectMocks private RateServiceImpl rateService = new RateServiceImpl(); @Mock private RaterDao raterDao; @Resource private MessageSource msg ; @Mock Map<String, StringAttribute> errorMap = new HashMap<String, StringAttribute>(); @Before public void

why i am getting org.powermock.api.mockito.expectation.WithOrWithoutExpectedArguments?

眉间皱痕 提交于 2020-04-13 06:49:13
问题 Currently, i am using the following dependency for my project But i am getting org.powermock.api.mockito.expectation.WithOrWithoutExpectedArguments not found please look on the dependency, <properties> <java.version>1.8</java.version> <powermock.version>1.7.0RC2</powermock.version> </properties> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <version>${powermock.version}</version> <scope>compile</scope> </dependency> <!-- https://mvnrepository.com

NoClassDefFoundError or NoSuchMethodError when trying to mock static (incompatible dependencies)

。_饼干妹妹 提交于 2020-04-11 12:41:13
问题 When a class with static method is mocked, an exception been thrown. The version 2.0.0 of PowerMock displays NoClassDefFoundError 1 . The versions 2.0.1 and 2.0.2 displays NoSuchMethodError 2 . In project that I work is used Mockito on version 2.25.0 , but I've tried using new versions of Mockito following the compatible versions table (https://github.com/powermock/powermock/wiki/Mockito#supported-versions). In addition, I tested the downgrade to versions 2.8.9 of Mockito with 1.7.x of

MockClassLoader cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl

耗尽温柔 提交于 2020-04-04 11:57:32
问题 I am in the middle of migrating a project into Java9, The Tests start failing after I switched to the new Java version, it seems like PowerMock is trying to access some classes it does not have access to. Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.973 sec <<< FAILURE! - in com.Test initializationError(com.Test) Time elapsed: 0.007 sec <<< ERROR! org.objenesis.ObjenesisException: java.lang.reflect.InvocationTargetException Caused by: java.lang.reflect

MockClassLoader cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl

喜你入骨 提交于 2020-04-04 11:56:14
问题 I am in the middle of migrating a project into Java9, The Tests start failing after I switched to the new Java version, it seems like PowerMock is trying to access some classes it does not have access to. Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.973 sec <<< FAILURE! - in com.Test initializationError(com.Test) Time elapsed: 0.007 sec <<< ERROR! org.objenesis.ObjenesisException: java.lang.reflect.InvocationTargetException Caused by: java.lang.reflect

How to fix java.lang.IllegalStateException: Cannot clear JavaAgentClassRegister. Set method has not been called.?

£可爱£侵袭症+ 提交于 2020-03-22 09:12:09
问题 I am using JunitRunner for running unit tests written using PowerMock and Mockito . Spring Boot Version used is <version>2.0.5.RELEASE</version> pom.xml has below dependencies <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.0-beta.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.0-beta.5</version> <scope>test</scope> <dependency>