Search code examples
jenkins-pipelinepowermockito

Mocking jenkins.model.Job's getLastSuccessfulBuild results in No signature of method is applicable for argument types


How do you handle using PowerMockito to mock out Jenkins Job's methods that return a RunT object when that object doesn't seem to be exported from the jar.

When trying to mock getLastUnstableBuild I'm running into trouble as it returns a RunT and I'm not sure how to mock that since I cannot access RunT.

I've tried using a mock run, a mock of Build and nothing seems to work.

@RunWith(PowerMockRunner.class)
@PrepareForTest([Jenkins.class, Job.class, Run.class, Build.class, UserIdCause.class, BuildServerGlobals.class])
class BuildServerJobManagerTest {
    def thisScript

    def expectedJob = 'job'
    def expecteBuildNumber = '1'
    def expectedUser = 'user'

    @Mock
    private Jenkins jenkins

    @Mock
    private Job job

    @Mock
    private Run run

    @Mock
    private Build build

    ...

PowerMockito.when(jenkins.getItemByFullName(expectedJob)).thenReturn(job)
PowerMockito.when(run.getLastUnstableBuild()).thenReturn(run) //fails
PowerMockito.when(run.getLastUnstableBuild()).thenReturn(build) //fails

Failure

groovy.lang.MissingMethodException: 
No signature of method: hudson.model.Run$$EnhancerByMockitoWithCGLIB$$d8adb2d7
.getLastUnstableBuild() is applicable for argument types: () values: []

Solution

  • The generic RunT was not the problem. The fact that the method in question is a Job class method and not a Run class method was the problem

    PowerMockito.when(job.getLastUnstableBuild()).thenReturn(...)