2016-12-21 6 views
1

私はモックアウトしたい別のサービスを利用するサービスを持っています。ここでスプリングブートと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クラスをモック行う方法ですが、代わりに単にスタブを返します。オブジェクト?

+0

はMockitoと私ドンためである – Dongqing

+0

をそれをhttp://www.baeldung.com/injecting-mocks-in-springチェックできます私が上でやっていることとは何か違うと思う。 – sonoerin

答えて

2

私はあなたがwhenブロックに期待を指定することはできないと思いますが、これが根本的な原因です。

チェックSpock interaction testing tutorial それは「相互作用を宣言する」というセクションがあり、それはあなたがここで「セットアップ」(与えられた)または「その後」のブロックのみ

に期待を宣言できると述べているの簡略化した例であります私のマシン上で動作するような相互作用:あなたは、いくつかのサービス(MyServiceで)を試験するために見ることができ、それは(私はインターフェイスで働いていたDependantServiceインターフェースに依存ここ

interface Account {} 

class SampleAccount implements Account {} 


interface DependentService { 
    Account createAccount(int someParam) 
} 

class DependentServiceImpl implements DependentService { 

    Account createAccount(int someParam) { 
     new SampleAccount() 
    } 
} 


class MyService { 

    private DependentService service 

    public MyService(DependentService dependentService) { 
     this.service = dependentService 
} 

public Account testMe(int someParam) { 
    service.createAccount(someParam) 
} 

}

私は私のサンプルプロジェクトのクラスパスにCGLIBを持っていないので、それは本当にあなたの質問のために重要ではありません)

そして、ここでは、スポックでテストです:ご覧のとおり

class SampleSpec extends Specification { 

def "check"() { 
    setup: 
    def mockDependentService = Mock(DependentService) 
    def mockAccount   = Mock(Account) 
    1 * mockDependentService.createAccount(5) >> mockAccount 
    MyService testedObject = new MyService(mockDependentService) 
    when: 
    def expectedAccount = testedObject.testMe(5) 
    then: 
    expectedAccount == mockAccount 
    } 
} 

、私はここに与えられたブロックに設定された私の期待を持っている

希望これは

+0

完璧な答えは、私を悩まされてしまったので、@Autowiredクラスの1つを模擬して、テストのためにスタブアウトされたオブジェクトを返すことができます。大きな助けをありがとう! – sonoerin

関連する問題