【发布时间】:2021-06-12 15:12:13
【问题描述】:
// class under specification
public class TeamService {
// method under specification
public void deleteTeam(String id) {
/* some other calls */
this.moveAssets(team) // calls method within the class under spec.
}
// I would like to stub / mock this method
public void moveAssets(Team team){
// logic
}
}
Spock 规范
def deleteTeam(){
given:
TeamService teamService = new TeamService()
when: 'I delete the team'
teamService.deleteTeam()
then: 'I want to check that moveAssets gets called(team resources going to be preserved
and that deleteTeam (external class that deletes the team) gets called (has no issues here)'
1 * teamService.moveAssets(id) >> {} //See Q1
/*
Q1: I want to test that this call is made
but currently it does not count as interaction for some reason so I get an error.
From what I read you can't stub the calls unless the class is mocked -
but here I do have an important need to check on the method within the class under specification
(that is not mocked). What are my options?
I know I can in theory move moveAssets to some other class,
which I can then mock and accomplish it but that does not feel right.
*/
}
所以在Spock对这个类的Spec中,TeamService是规范下的类,deleteTeam是规范下的方法。
问题 我收到 moveAssets 方法的错误 0 调用。 我希望能够存根/测试 moveAssets 方法,没有太多关于如何完成该方法的信息。 我不想将 moveAssets 重新定位到 anotherClass (所以我可以模拟它)。 任何指导表示赞赏。
必须有一些更好的方法来处理这种情况。
总结:
如何测试从“规范下的方法 (teamServices.deleteTeam())”调用的“规范下的类”teamServices.moveAssets()) 方法?
【问题讨论】:
标签: java unit-testing groovy mocking spock