【发布时间】:2016-12-21 04:45:43
【问题描述】:
我有一个使用另一个服务的服务,我想模拟出来。
@Service
public class CustomerService {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private PatientRepository patientRepository;
@Autowired
private MyHelper myHelper;
public Office createOfficeAccount(Office commonOffice) throws Exception {
// this line calls another service via http, I want to mock it:
Account newAccount = myHelper.createAccount(officeAccount);
return customerRepository.save(customer);
}
这是我的测试课:
class KillBillCustomerServiceTest extends BaseSpecification {
@Autowired
private CustomerService customerService = Mock(CustomerService)
@Autowired
private PatientRepository patientRepository = Mock(PatientRepository)
@Autowired
private MyHelper kbHelper = Mock(MyHelper)
def setup() {
customerService.setPatientRepository(patientRepository)
customerService.setMyHelper(kbHelper)
}
def "create new Account from Common Office"() {
def commonOffice = createOfficeForTests()
CustomerService customerService = new CustomerService (myHelper: kbHelper)
when:
kbHelper.createAccount(commonOffice) >> new Account() // want to mock it, but it is still calling the actual class to try and make the network call
}
我的问题是如何模拟我的 MyHelper 类,以便它实际上不会尝试进行真正的调用,而只是返回一个存根对象?
【问题讨论】:
-
那是给 Mockito 的,我认为它与我上面所做的没有什么不同。
标签: spring-boot spock