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

내가 블록에 기대치를 지정할 수 없다고 생각합니다. 그리고 이것이 근본 원인입니다.

확인 Spock interaction testing tutorial 은 "선언하는 상호 작용"이라는 섹션을 가지고 있으며, 그것은 당신이 여기에만

을 "설정"에 기대를 선언 (주어진) 또는 "다음"블록 수 있다고의 간단한 예입니다 내 컴퓨터에서 작동 이러한 상호 작용 :

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) 
} 

} 여기

일부 서비스 (이면 MyService)를 볼 수 있습니다 테스트 할하고 DependantService 인터페이스에 따라 달라집니다 (I 인터페이스와 함께 일

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 
    } 
} 

보시다시피 : 내 샘플 프로젝트의 클래스 패스에 CGLIB이 없기 때문에, 정말 당신의 질문을 위해) 여기

그리고 문제가되지 않는 것은 스팍의 테스트입니다 , 내가 여기에 주어진 블록에 설정된 내 기대를 가지고

희망이

+0

완벽한 대답, 저를 잡았습니다. @Autowired 클래스 중 하나를 조롱하여 테스트를 위해 스텁 된 객체를 반환 할 수 있습니다. 큰 도움을 주셔서 감사합니다! – sonoerin