Skip to content

Commit

Permalink
switch to mockito
Browse files Browse the repository at this point in the history
issue with power mock / jdk 11 / reflection
see powermock/powermock#969
  • Loading branch information
shaikhu committed Jun 6, 2024
1 parent 2e2f719 commit 0d613eb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ dependencies {
testImplementation gradleTestKit()
testImplementation "junit:junit:$junitVersion"
testImplementation "org.assertj:assertj-core:$assertJVersion"
testImplementation "org.powermock:powermock-module-junit4:$powermockVersion"
testImplementation "org.powermock:powermock-api-mockito2:$powermockVersion"
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ logbackVersion=1.3.14
commonsIoVersion=2.16.1

junitVersion=4.13.2
powermockVersion=2.0.9
mockitoVersion=5.12.0
assertJVersion=3.25.3
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import com.sonatype.nexus.api.iq.internal.InternalIqClientBuilder;
import com.sonatype.nexus.api.iq.scan.ScanResult;

import org.junit.After;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import org.sonatype.gradle.plugins.scan.common.DependenciesFinder;

import com.google.common.collect.Sets;
Expand All @@ -40,12 +43,6 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.Logger;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -66,13 +63,14 @@
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(InternalIqClientBuilder.class)
@RunWith(MockitoJUnitRunner.class)
public class NexusIqScanTaskTest
{
private static final String USER_AGENT_REGEX =
"Sonatype_Nexus_Gradle/[^\\s]+ \\(Java [^;]+; [^;]+ [^;]+; Gradle [^;]+\\)";

private MockedStatic<InternalIqClientBuilder> internalIqClientBuilder;

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

Expand All @@ -87,7 +85,7 @@ public class NexusIqScanTaskTest

@Before
public void setup() throws IqClientException {
PowerMockito.mockStatic(InternalIqClientBuilder.class);
internalIqClientBuilder = Mockito.mockStatic(InternalIqClientBuilder.class);

InternalIqClientBuilder builderMock = mock(InternalIqClientBuilder.class);
when(builderMock.withServerConfig(any(ServerConfig.class))).thenReturn(builderMock);
Expand All @@ -106,6 +104,11 @@ public void setup() throws IqClientException {
.thenReturn(Collections.emptyList());
}

@After
public void cleanup() {
internalIqClientBuilder.close();
}

@Test
public void testScan_simulated() throws Exception {
NexusIqScanTask task = buildScanTask(true);
Expand Down Expand Up @@ -162,7 +165,6 @@ public void testScan_ErrorConnectingToIqWithCause() throws Exception {
public void testScan_realWithResultFilePath() throws Exception {
NexusIqScanTask task = buildScanTask(false, "some/path/file.json");
task.setDependenciesFinder(dependenciesFinderMock);
when(iqClientMock.verifyOrCreateApplication(eq(task.getApplicationId()))).thenReturn(true);

task.scan();

Expand Down Expand Up @@ -198,7 +200,6 @@ public void testScan_realUnableToCreateAppWithOrganization() throws Exception {
public void testScan_realWithDirIncludes() throws Exception {
NexusIqScanTask task = buildScanTask(false, null, "dir-include", null, null);
task.setDependenciesFinder(dependenciesFinderMock);
when(iqClientMock.verifyOrCreateApplication(eq(task.getApplicationId()))).thenReturn(true);

task.scan();

Expand All @@ -214,7 +215,6 @@ public void testScan_realWithDirIncludes() throws Exception {
public void testScan_realWithDirExcludes() throws Exception {
NexusIqScanTask task = buildScanTask(false, null, null, "dir-exclude", null);
task.setDependenciesFinder(dependenciesFinderMock);
when(iqClientMock.verifyOrCreateApplication(eq(task.getApplicationId()))).thenReturn(true);

task.scan();

Expand Down Expand Up @@ -242,7 +242,6 @@ public void testScan_realWithScanTargets() throws Exception {
});

task.setDependenciesFinder(dependenciesFinderMock);
when(iqClientMock.verifyOrCreateApplication(eq(task.getApplicationId()))).thenReturn(true);

task.scan();

Expand All @@ -258,7 +257,6 @@ public void testScan_realWithScanTargets() throws Exception {
public void testScan_realWithExcludeCompileOnly() throws Exception {
NexusIqScanTask task = buildScanTask(extension -> extension.setExcludeCompileOnly(true));
task.setDependenciesFinder(dependenciesFinderMock);
when(iqClientMock.verifyOrCreateApplication(eq(task.getApplicationId()))).thenReturn(true);

task.scan();

Expand Down

0 comments on commit 0d613eb

Please sign in to comment.