by Anonymous » 29 Jul 2025, 05:41
Ich schreibe einen Unit -Test in Groovy mit Spock für eine Java -Klasse mit privaten Abhängigkeiten. Hier ist eine vereinfachte Version des Code: < /p>
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(_)
}
}
Das
Problem ist, dass B.GetName () während des Tests Null zurückgibt. Es scheint, dass der Schein nicht ordnungsgemäß in Komponenta .>
Ich schreibe einen Unit -Test in Groovy mit Spock für eine Java -Klasse mit privaten Abhängigkeiten. Hier ist eine vereinfachte Version des Code: < /p>
[code]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(_)
}
}
[/code]
Das [url=viewtopic.php?t=26065]Problem[/url] ist, dass B.GetName () während des Tests Null zurückgibt. Es scheint, dass der Schein nicht ordnungsgemäß in Komponenta .>