Đối với Java Xuân folks thử nghiệm trong Spock:
constructorArgs là con đường để đi, nhưng sử dụng constructor tiêm. Spy() sẽ không cho phép bạn thiết lập các trường tự động trực tiếp.
// **Java Spring**
class A {
private ARepository aRepository;
@Autowire
public A(aRepository aRepository){
this.aRepository = aRepository;
}
public String getOne(String id) {
tryStubMe(id) // STUBBED. WILL RETURN "XXX"
...
}
public String tryStubMe(String id) {
return aRepository.findOne(id)
}
public void tryStubVoid(String id) {
aRepository.findOne(id)
}
}
// **Groovy Spock**
class ATest extends Specification {
def 'lets stub that sucker' {
setup:
ARepository aRepository = Mock()
A a = Spy(A, constructorArgs: [aRepository])
when:
a.getOne()
then:
// Stub tryStubMe() on a spy
// Make it return "XXX"
// Verify it was called once
1 * a.tryStubMe("1") >> "XXX"
}
}
Spock - stubbing trống phương pháp trên Spy đối tượng
// **Groovy Spock**
class ATest extends Specification {
def 'lets stub that sucker' {
setup:
ARepository aRepository = Mock()
A a = Spy(A, constructorArgs: [aRepository]) {
1 * tryStubVoid(_) >> {}
}
when:
...
then:
...
}
}
Cảm ơn rất nhiều - Tôi vừa mới xem xét lại này và đã học được rất nhiều về thiết kế kể từ khi tôi đặt câu hỏi. Bây giờ tôi đồng ý với bạn rằng thiết kế tốt hơn sẽ là một bulkMessageProcessingService và một individualMessageProcessingService. Do đó, thử nghiệm là tầm thường với một mô hình. – John