模拟jenkins.model.Job的getLastSuccessfulBuild结果:方法签名不适用于参数类型

时间:2019-05-14 11:45:45

标签: jenkins-pipeline powermockito

如何使用PowerMockito模拟Jenkins Job的方法,该方法似乎没有从jar中导出 RunT 对象。

尝试模拟getLastUnstableBuild时遇到麻烦,因为它返回了RunT,而且由于无法访问RunT,因此不确定如何模拟它。

我尝试使用模拟运行,Build的模拟,但似乎没有任何作用。

@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

失败

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

1 个答案:

答案 0 :(得分:0)

通用RunT并不是问题。问题所在的方法是Job类方法而不是Run类方法

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