Skip to content

Commit

Permalink
Upgrade Mockito & PowerMock (#63)
Browse files Browse the repository at this point in the history
Mockito is more strict now, therefore some small changes have to be applied.

1) use `Run` instead of `AbstractBuild` for `WorkflowJob`
Otherwise a `ClassCastException` is thrown.

2) add 'support-core' as test dependency
This is an optional dependency of 'workflow-cps' and therefore not a
transitive dependency of this plugin. It's required for mocking
`CpsFlowExecution` now, otherwise the `Component` class cannot be found.
  • Loading branch information
darxriggs authored and jeffpearce committed Oct 18, 2019
1 parent 804013c commit 7b614a0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<properties>
<jenkins.version>2.138.4</jenkins.version>
<java.level>8</java.level>
<powermock.version>1.7.4</powermock.version>
<powermock.version>2.0.2</powermock.version>
<hpi.compatibleSinceVersion>4.0</hpi.compatibleSinceVersion>
</properties>
<name>Job and Stage monitoring Plugin</name>
Expand Down Expand Up @@ -203,10 +203,16 @@
<artifactId>script-security</artifactId>
<version>1.42</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>support-core</artifactId>
<version>2.32</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<artifactId>mockito-core</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -215,12 +221,6 @@
<version>2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
Expand All @@ -229,7 +229,7 @@
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl;
import jenkins.scm.api.SCMRevision;
Expand Down Expand Up @@ -95,7 +94,7 @@ public void tearDown() {

@Test
public void testConfigBranchSource() throws Exception {
AbstractBuild build = Mockito.mock(AbstractBuild.class);
Run build = Mockito.mock(Run.class);
SCMRevisionAction mockSCMRevisionAction = mock(SCMRevisionAction.class);
when(build.getAction(SCMRevisionAction.class)).thenReturn(mockSCMRevisionAction);

Expand Down Expand Up @@ -127,20 +126,20 @@ public void testConfigBranchSource() throws Exception {
PowerMockito.when(builder.build()).thenReturn(github);
PowerMockito.when(builder.withEndpoint(any())).thenReturn(builder);

GithubNotificationConfig instance = GithubNotificationConfig.fromRun((Run<WorkflowJob, ?>) build, builder);
GithubNotificationConfig instance = GithubNotificationConfig.fromRun(build, builder);
assertEquals("what-the-hash", instance.getShaString());
assertEquals("test-branch", instance.getBranchName());
}

@Test
public void testDisabledInConfig() {
when(config.getEnableGithub()).thenReturn(false);
assertNull(GithubNotificationConfig.fromRun(mock(AbstractBuild.class), null));
assertNull(GithubNotificationConfig.fromRun(mock(Run.class), null));
}

// @Test
// public void testConfigPullRequest() throws Exception {
// AbstractBuild build = Mockito.mock(AbstractBuild.class);
// Run build = Mockito.mock(Run.class);
// SCMRevisionAction mockSCMRevisionAction = mock(SCMRevisionAction.class);
// when(build.getAction(SCMRevisionAction.class)).thenReturn(mockSCMRevisionAction);
//
Expand Down Expand Up @@ -169,6 +168,6 @@ public void testDisabledInConfig() {
// PowerMockito.when(builder.withEndpoint(any())).thenReturn(builder);
//
//
// GithubNotificationConfig instance = GithubNotificationConfig.fromRun(build, null, builder);
// GithubNotificationConfig instance = GithubNotificationConfig.fromRun(build, builder);
// }
}

0 comments on commit 7b614a0

Please sign in to comment.