Java Wiosna ludzie testów w Spocka:
constructorArgs jest droga, ale używać zastrzyk konstruktora. Spy() nie zezwoli ci na bezpośrednie ustawianie pól autowych.
// **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 void metody na Spy sprzeciw
// **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:
...
}
}
Dzięki bardzo - Właśnie powrócił to i nauczyliśmy się wiele o projektowaniu od zadałem pytanie. Zgadzam się z tobą, że lepszym projektem będzie usługa bulkMessageProcessingService i individualMessageProcessingService. Testowanie jest zatem trywialne z pozorowaniem. – John