powermock

Unit tests coverage using Jacoco for test classes written using Powermock

一曲冷凌霜 提交于 2019-12-19 04:14:48
问题 I am trying to get the code coverage report on the sonarqube dashboard on jenkins. The code coverage report is coming up but showing only 4.6% coverage. On investigating I found out that the test classes written using PowerMocks are getting skipped. On further investigation I found that "JaCoCo doesn't play well with dynamically modified/created classes (this is the way how powermock works). This is a known limitation we can't currently do anything about". Is there any work around for this so

Test if another method was called

泄露秘密 提交于 2019-12-18 19:46:10
问题 So I'm sure there is something like this out there but I have been searching for an hour and haven't found exactly what I am looking for. say I have a class that looks like this: public class MyClass { public void myMethod(boolean shouldCallOtherMethod) { if(shouldCallOtherMethod) { otherMethod(); } } public void otherMethod() { System.out.println("Called"); } } How do I make something like this work? @Test public void shouldCallMethod() { MyClass myClass = new MyClass(); myClass.myMethod

Android测试配置的文件(Robolectric+jacoco)

試著忘記壹切 提交于 2019-12-18 10:40:46
1、使用Robolectric (1)在app\build.gradle里面配置 testImplementation "org.robolectric:robolectric:3.8" testImplementation 'org.robolectric:shadows-support-v4:3.4-rc2' testImplementation 'org.robolectric:shadows-multidex:3.0' (2)在"...\xxxProject\build.gradle"中添加 repositories{ ... maven { url 'https://maven.google.com' } // Robolectric下载所需 } 2、使用数据库ObjectBox (1)在app\build.gradle里面配置 apply plugin: 'io.objectbox' implementation "io.objectbox:objectbox-android:${OBJECTBOX_VERSION}" implementation "io.objectbox:objectbox-linux:${OBJECTBOX_VERSION}" implementation "io.objectbox:objectbox-macos:$OBJECTBOX

Mockito + PowerMock LinkageError while mocking system class

对着背影说爱祢 提交于 2019-12-18 09:56:09
问题 I've got such a code snippet: @RunWith(PowerMockRunner.class) @PrepareForTest({Thread.class}) public class AllMeasuresDataTest { @Before public void setUp() throws Exception { } @Test public void testGetMeasures() { AllMeasuresData measure = new AllMeasuresData(); assertEquals(measure.getMeasures(), null); HashMap<String, Measure> map = new HashMap<String, Measure>(); measure.setMeasures(map); assertEquals(measure.getMeasures(), map); measure.setMeasures(null); assertEquals(measure

PowerMock PrepareForTest annotation causing problems with AmazonSQSClient constructor

风流意气都作罢 提交于 2019-12-17 19:23:59
问题 I'm having some trouble with the PrepareForTest annotation and creating a new instance of an AmazonSQSClient. I'm writing a Jenkins plugin and unfortunately need to mock the FormValidation static class in order to ensure that warning and error messages are produced on field validation of my plugin. However when creating an instance of AmazonSQSClient I get an org.apache.http.conn.ssl.SSLInitializationException I've abstracted it down to a very simple example, here's my test file: package com

When is it appropriate to bypass encapsulation in unit tests?

一笑奈何 提交于 2019-12-17 18:58:42
问题 I've recently become aware of powermock's Whitebox functionality. (In summary, it allows you to directly test private methods or modify private members directly from unit tests--while keeping them private!) I know that there are some frames of thought that frown upon unit testing anything other than visible methods, but darn it, sometimes you just want a simple test to ensure a deep level helper method is doing what it is supposed to do...without going through what could be huge overhead to

Mocking Logger and LoggerFactory with PowerMock and Mockito

妖精的绣舞 提交于 2019-12-17 15:32:14
问题 I have the following Logger I want to mock out, but to validate log entries are getting called, not for the content. private static Logger logger = LoggerFactory.getLogger(GoodbyeController.class); I want to Mock ANY class that is used for LoggerFactory.getLogger() but I could not find out how to do that. This is what I ended up with so far: @Before public void performBeforeEachTest() { PowerMockito.mockStatic(LoggerFactory.class); when(LoggerFactory.getLogger(GoodbyeController.class)).

PowerMock: mock out private static final variable, a concrete example

£可爱£侵袭症+ 提交于 2019-12-17 09:36:22
问题 what is the absolute minimal mocking that must be done to pass this test? code: class PrivateStaticFinal { private static final Integer variable = 0; public static Integer method() { return variable + 1; } } test: @RunWith(PowerMockRunner.class) @PrepareForTest(PrivateStaticFinal.class) class PrivateStaticFinalTest { @Test public void testMethod() { //TODO PrivateStaticFinal.variable = 100 assertEquals(PrivateStaticFinal.method(), 101); } } related: Mock private static final variables in the

Mocking private field of a class

柔情痞子 提交于 2019-12-14 02:36:31
问题 I have the following field in my class @Resource(name = "lobIdCancellationNodeMap") private Map<String, List<String>> lobIdCancellationNodeMap; After this line we have directly accessed this field lobIdCancellationNodeMap I am not able to set value for this using Mockito, and there is no construtor available which can set this value. This is how it is used lobIdCancellationNodeMap.get(lobId) I am not allowed to change implementation class 回答1: While I agree with the notes by other users that

What's the use of the configuration in classpath for Eclipse project: “org.eclipse.jdt.junit.JUNIT_CONTAINER/4”

寵の児 提交于 2019-12-13 20:45:27
问题 I have finished one unit test class, but when I run it, it returns with an Exception. the following is my test cases: import java.util.Date; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.rule.PowerMockRule; import com