Skip to content

Commit

Permalink
[tests] Replace jmockit with powermock whitebox and use version 2.0.9…
Browse files Browse the repository at this point in the history
… of powermock

As the current maintainer of jmockit, I highly advice not using jmockit any more.  The original author vanished back in 2020 and its a considerable mess thus your own comment stuck on old.  While deencapsulation is being added back in next release provided open pull request works out, its native replacement is whiteboxing and this project already used powermock.  Switching was trivial.
  • Loading branch information
hazendaz committed Dec 22, 2023
1 parent 83d7aed commit 0ad324d
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 102 deletions.
13 changes: 6 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
<maven.compile.encoding>UTF-8</maven.compile.encoding>

<powermock.version>1.7.3</powermock.version>
<jackson.version>2.16.0</jackson.version>
<jmockit.version>1.34</jmockit.version> <!-- last version that supports reflections -->
<powermock.version>2.0.9</powermock.version>

<test.integration.pattern>**/*IntegrationTest*.java</test.integration.pattern>
</properties>
Expand Down Expand Up @@ -272,20 +271,20 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<artifactId>powermock-reflect</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
Expand Down
94 changes: 47 additions & 47 deletions src/test/java/net/masterthought/cucumber/ReportBuilderTest.java

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/test/java/net/masterthought/cucumber/ReportParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import java.util.List;
import java.util.Map;

import mockit.Deencapsulation;
import net.masterthought.cucumber.json.Element;
import net.masterthought.cucumber.json.Feature;
import net.masterthought.cucumber.reducers.ReducingMethod;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.data.Index;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

/**
* @author Damian Szczepanik (damianszczepanik@github)
Expand Down Expand Up @@ -127,31 +127,31 @@ public void parseJsonFiles_OnEmptyFile_SkipsJSONReport() {
}

@Test
public void extractTarget_ReturnsFileNameWithoutExtension() {
public void extractTarget_ReturnsFileNameWithoutExtension() throws Exception {

// given
String jsonFile = SAMPLE_JSON;
initWithJson(SAMPLE_JSON);
ReportParser reportParser = new ReportParser(configuration);

// when
String qualifier = Deencapsulation.invoke(reportParser, "extractQualifier", jsonFile);
String qualifier = Whitebox.invokeMethod(reportParser, "extractQualifier", jsonFile);


// then
assertThat(qualifier).isEqualTo(jsonFile.substring(0, jsonFile.length() - 5));
}

@Test
public void extractTarget_OnNoJSONFile_ReturnsFileName() {
public void extractTarget_OnNoJSONFile_ReturnsFileName() throws Exception {

// given
String jsonFile = SAMPLE_JSON + ".txt";
initWithJson(SAMPLE_JSON);
ReportParser reportParser = new ReportParser(configuration);

// when
String qualifier = Deencapsulation.invoke(reportParser, "extractQualifier", jsonFile);
String qualifier = Whitebox.invokeMethod(reportParser, "extractQualifier", jsonFile);


// then
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/net/masterthought/cucumber/TrendsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import mockit.Deencapsulation;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

/**
* @author Damian Szczepanik (damianszczepanik@github)
Expand Down Expand Up @@ -54,7 +54,7 @@ public void addBuild_OnMissingDataForSteps_FillsMissingDataForSteps() {
Reportable result = ReportableBuilder.buildSample();
trends.addBuild("buildName", result);
final String[] buildNumbers = new String[]{"a", "b", "e"};
Deencapsulation.setField(trends, "buildNumbers", buildNumbers);
Whitebox.setInternalState(trends, "buildNumbers", buildNumbers);

// when
trends.addBuild("the build!", result);
Expand Down Expand Up @@ -112,49 +112,49 @@ public void limitItems_ReducesNumberOfItems() {
}

@Test
public void copyLastElements_OnBigLimit_ReturnsPassedIntArray() {
public void copyLastElements_OnBigLimit_ReturnsPassedIntArray() throws Exception {

// given
final int[] array = new int[]{3, 4, 5, 6, 7, 8};
final int limit = array.length + 1;

// when
int[] limitedArray = Deencapsulation.invoke(Trends.class, "copyLastElements", array, limit);
int[] limitedArray = Whitebox.invokeMethod(Trends.class, "copyLastElements", array, limit);

// then
assertThat(limitedArray).isSameAs(array);
}

@Test
public void copyLastElements_OnBigLimit_ReturnsPassedLongArray() {
public void copyLastElements_OnBigLimit_ReturnsPassedLongArray() throws Exception {

// given
final long[] array = new long[]{3, 4, 5, 6, 7, 8};
final int limit = array.length + 1;

// when
long[] limitedArray = Deencapsulation.invoke(Trends.class, "copyLastElements", array, limit);
long[] limitedArray = Whitebox.invokeMethod(Trends.class, "copyLastElements", array, limit);

// then
assertThat(limitedArray).isSameAs(array);
}

@Test
public void copyLastElements_OnBigLimit_ReturnsPassedStringArray() {
public void copyLastElements_OnBigLimit_ReturnsPassedStringArray() throws Exception {

// given
final String[] array = new String[]{"3", "4", "5", "6", "7", "8"};
final int limit = array.length + 1;

// when
String[] limitedArray = Deencapsulation.invoke(Trends.class, "copyLastElements", array, limit);
String[] limitedArray = Whitebox.invokeMethod(Trends.class, "copyLastElements", array, limit);

// then
assertThat(limitedArray).isSameAs(array);
}

@Test
public void applyPatchForFeatures_OnFailedGreaterThanTotal_ChangesTotalFeatureAndFailed() {
public void applyPatchForFeatures_OnFailedGreaterThanTotal_ChangesTotalFeatureAndFailed() throws Exception {

// given
final int totalFeatures = 1000;
Expand All @@ -164,7 +164,7 @@ public void applyPatchForFeatures_OnFailedGreaterThanTotal_ChangesTotalFeatureAn
trends.addBuild("buildNumber", result);

// when
Deencapsulation.invoke(trends, "applyPatchForFeatures");
Whitebox.invokeMethod(trends, "applyPatchForFeatures");

// then
assertThat(trends.getTotalFeatures()[0]).isGreaterThan(trends.getFailedFeatures()[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import java.io.File;
import java.util.Properties;

import mockit.Deencapsulation;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.junit.Before;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.Trends;
Expand Down Expand Up @@ -91,18 +91,18 @@ public String getWebPage() {
};

// when & then
assertThatThrownBy(() -> Deencapsulation.invoke(page, "generatePage"))
assertThatThrownBy(() -> Whitebox.invokeMethod(page, "generatePage"))
.isInstanceOf(ValidationException.class);
}

@Test
public void buildProperties_ReturnsProperties() {
public void buildProperties_ReturnsProperties() throws Exception {

// given
page = new FeaturesOverviewPage(reportResult, configuration);

// when
Properties props = Deencapsulation.invoke(page, "buildProperties");
Properties props = Whitebox.invokeMethod(page, "buildProperties");

// then
assertThat(props).hasSize(3);
Expand Down Expand Up @@ -213,11 +213,11 @@ public void buildGeneralParameters_OnInvalidBuildNumber_DoesNotAddPreviousBuildN
}

@Test
public void buildGeneralParameters_OnTrendsStatsFile_AddsTrendsFlag() {
public void buildGeneralParameters_OnTrendsStatsFile_AddsTrendsFlag() throws Exception {

// given
configuration.setTrendsStatsFile(TRENDS_FILE);
Trends trends = Deencapsulation.invoke(ReportBuilder.class, "loadTrends", TRENDS_FILE);
Trends trends = Whitebox.invokeMethod(ReportBuilder.class, "loadTrends", TRENDS_FILE);
page = new TrendsOverviewPage(reportResult, configuration, trends);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.io.IOException;

import mockit.Deencapsulation;
import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.ReportResult;
import net.masterthought.cucumber.Trends;
Expand All @@ -12,6 +11,7 @@
import org.apache.velocity.VelocityContext;
import org.junit.Before;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -44,14 +44,14 @@ public void getWebPage_ReturnsTrendsOverviewFileName() {
}

@Test
public void prepareReport_AddsCustomProperties() {
public void prepareReport_AddsCustomProperties() throws Exception {

// given
configuration.setBuildNumber("myBuild");
Trends trends = Deencapsulation.invoke(ReportBuilder.class, "loadTrends", new File(TRENDS_TMP_FILE));
Trends trends = Whitebox.invokeMethod(ReportBuilder.class, "loadTrends", new File(TRENDS_TMP_FILE));
page = new TrendsOverviewPage(reportResult, configuration, trends);

Deencapsulation.setField(page, "reportResult", new ReportResult(features, configuration));
Whitebox.setInternalState(page, "reportResult", new ReportResult(features, configuration));

// when
page.prepareReport();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import mockit.Deencapsulation;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.Trends;
Expand All @@ -18,11 +18,11 @@
public class TrendsOverviewPageIntegrationTest extends PageTest {

@Test
public void generatePage_generatesTitle() {
public void generatePage_generatesTitle() throws Exception {

// given
setUpWithJson(SAMPLE_JSON);
Trends trends = Deencapsulation.invoke(ReportBuilder.class, "loadTrends", TRENDS_FILE);
Trends trends = Whitebox.invokeMethod(ReportBuilder.class, "loadTrends", TRENDS_FILE);
page = new TrendsOverviewPage(reportResult, configuration, trends);
final String titleValue = String.format("Cucumber Reports - Trends Overview",
configuration.getBuildNumber());
Expand All @@ -38,11 +38,11 @@ public void generatePage_generatesTitle() {
}

@Test
public void generatePage_generatesLead() {
public void generatePage_generatesLead() throws Exception {

// given
setUpWithJson(SAMPLE_JSON);
Trends trends = Deencapsulation.invoke(ReportBuilder.class, "loadTrends", TRENDS_FILE);
Trends trends = Whitebox.invokeMethod(ReportBuilder.class, "loadTrends", TRENDS_FILE);
page = new TrendsOverviewPage(reportResult, configuration, trends);

// when
Expand All @@ -57,11 +57,11 @@ public void generatePage_generatesLead() {
}

@Test
public void generatePage_generatesCharts() {
public void generatePage_generatesCharts() throws Exception {

// given
setUpWithJson(SAMPLE_JSON);
Trends trends = Deencapsulation.invoke(ReportBuilder.class, "loadTrends", TRENDS_FILE);
Trends trends = Whitebox.invokeMethod(ReportBuilder.class, "loadTrends", TRENDS_FILE);
page = new TrendsOverviewPage(reportResult, configuration, trends);

// when
Expand All @@ -77,11 +77,11 @@ public void generatePage_generatesCharts() {
}

@Test
public void generatePage_insertsChartData() {
public void generatePage_insertsChartData() throws Exception {

// given
setUpWithJson(SAMPLE_JSON);
Trends trends = Deencapsulation.invoke(ReportBuilder.class, "loadTrends", TRENDS_FILE);
Trends trends = Whitebox.invokeMethod(ReportBuilder.class, "loadTrends", TRENDS_FILE);
page = new TrendsOverviewPage(reportResult, configuration, trends);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import static org.assertj.core.api.Assertions.assertThat;

import mockit.Deencapsulation;
import org.junit.Before;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import net.masterthought.cucumber.generators.integrations.PageTest;
import net.masterthought.cucumber.json.support.Argument;
Expand All @@ -24,7 +24,7 @@ public void getRows_ReturnsRows() {

// given
Step step = features.get(0).getElements()[1].getSteps()[5];
Argument[] arguments = Deencapsulation.getField(step, "arguments");
Argument[] arguments = Whitebox.getInternalState(step, "arguments");

// when
Row[] rows = arguments[0].getRows();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import static org.assertj.core.api.Assertions.assertThat;

import mockit.Deencapsulation;
import net.masterthought.cucumber.generators.integrations.PageTest;
import net.masterthought.cucumber.json.support.Status;
import org.junit.Before;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

/**
* @author Damian Szczepanik (damianszczepanik@github)
Expand Down Expand Up @@ -61,14 +61,14 @@ public void getElements_ReturnsElements() {
}

@Test
public void calculateReportFileName_ReturnsFileName() {
public void calculateReportFileName_ReturnsFileName() throws Exception {

// given
Feature feature = features.get(1);
final int jsonFileNo = 3;

// when
String reportFileName = Deencapsulation.invoke(feature, "calculateReportFileName", jsonFileNo);
String reportFileName = Whitebox.invokeMethod(feature, "calculateReportFileName", jsonFileNo);

// then
assertThat(reportFileName).isEqualTo("report-feature_3_1515379431.html");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.masterthought.cucumber.json.support;

import mockit.Deencapsulation;
import org.powermock.reflect.Whitebox;

import net.masterthought.cucumber.json.Match;
import net.masterthought.cucumber.json.Output;
Expand Down Expand Up @@ -29,7 +29,7 @@ private static class ResultsableMock implements Resultsable {

ResultsableMock(Status status) {
this.result = new Result();
Deencapsulation.setField(this.result, "status", status);
Whitebox.setInternalState(this.result, "status", status);
}

@Override
Expand Down
Loading

0 comments on commit 0ad324d

Please sign in to comment.