私はモックアウトしたい別のサービスを利用するサービスを持っています。ここでスプリングブートとSpockを使ったモックサービス
@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と私ドンためである – Dongqing
をそれをhttp://www.baeldung.com/injecting-mocks-in-springチェックできます私が上でやっていることとは何か違うと思う。 – sonoerin