Code: Select all
class ComponentA {
private ComponentB b;
private ComponentC c;
void calculation() {
if(b==null) {
logger.info("b is nulll"); // this is not getting executed so b is not null
}
logger.info(b.getName()); // value of b.getName() is null
}
}
< /code>
Und hier ist der Spock-Test: < /p>
class ComponentASpec extends Specification {
@Mock
ComponentB b
@Mock
ComponentC c
@InitMocks
ComponentA a
def "test calculation"() {
given:
b.getName() >> "name"
when:
a.calculation()
then:
1 * logger.info(_)
}
}