当前位置: 首页 > 知识库问答 >
问题:

使用Spock的Spring Boot集成测试:stubbed方法返回null

张银龙
2023-03-14
@SpringBootTest
class RoboApplicationTest extends Specification {

    @Autowired
    ApplicationContext context

    @SpringBean
    GpioController gpioController = Stub()

    def "test context loads"() {
        given:
        gpioController.provisionDigitalInputPin(_, _, _) >> { throw new RuntimeException("I'm stubbed!") }

        expect:
        context != null
        context.containsBean('gpioController')
    }
}

您可以在下面找到整个应用程序:https://github.com/ahlinist/raspberry-pi4j/tree/feature/integration-tests

我试图实现以下描述的结果:https://github.com/spockframework/spock/blob/master/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/springbeanintegrationspec.groovy

我试过很多方法都没有结果。用Mockito进行类似的测试效果很好。我错过了什么?

./gradlew integrationTest

共有1个答案

周朗
2023-03-14

当我克隆你的项目并运行测试时,我还会看到一个NPE,······

java.lang.IllegalStateException: Failed to execute ApplicationRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:778) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    (...)
Caused by: java.lang.NullPointerException: null
    at robot.controller.impl.RaspberryPiInputImpl.addListener(RaspberryPiInputImpl.java:19) ~[main/:na]
    at robot.sensor.AbstractSensor.init(AbstractSensor.java:21) ~[main/:na]
    at robot.TwoWheelRobot.init(TwoWheelRobot.java:20) ~[main/:na]
    at robot.RoboApplication.run(RoboApplication.java:26) ~[main/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:775) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    ... 61 common frames omitted

...但不是在你的测试中而是在这堂课上:

package robot.controller.impl;

import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import lombok.RequiredArgsConstructor;
import robot.controller.Input;
import robot.controller.Listener;

@RequiredArgsConstructor
public class RaspberryPiInputImpl implements Input {

    private final GpioPinDigitalInput gpioPinDigitalInput;

    @Override
    public void addListener(Listener listener) {
        GpioPinListenerDigital gpioListener = (GpioPinListenerDigital) listener;
        gpioPinDigitalInput.addListener(gpioListener);
    }
}

您有一个最后的GPIOPIndigitalInput字段,该字段永远不会初始化,即始终为NULL。那么,在调用AddListener(..)时,除了NPE之外,您还期望什么呢?也许你应该在那里注入一个值...

 类似资料:
  • 在我的应用程序中有,它有一个操作,如下所示: 现在,我正在测试视图和模型,如下所示: 但是我的测试用例失败了,stacktrace如下: 正在运行2个spock测试。。。第1页,共2页 有什么问题吗。

  • 测试失败,因为第二次调用var仍然返回Billy Bob而不是Tommy Jackson。我知道有一种方法可以通过调用顺序返回不同的值,但我想让它依赖于给定的模拟。 使用普通值--没有模拟/存根代理--作为参数值实际上是有效的。我假设Spock引擎在两个模拟之间不会有差异。但我不确定这一点,因为代理确实有ID作为实例字段。

  • 我有几个繁重的Spring集成测试(是的,这不是最好的方法,我没有时间正确地模拟所有外部dep) 下面是测试的典型注释 由于以下原因,测试会定期失败: 这里有两个问题:1、让测试共存的正确方式是什么?我在surefire插件中设置了forkCount=0。好像有帮助 2.1. 在每次测试期间,我实际上不需要启动所有的

  • 下面是我的应用程序主类: 但当我运行这个程序时,我得到了一个非常奇怪的错误: groovyx.net.http.restclient:解析'application/json'响应时出错

  • 问题内容: 我使用spock编写测试用例,使用jenkins运行和发布我的测试用例。我能够得到报告的代码覆盖率,但是声纳 仅 向我显示 Java 单元测试用例;在 常规测试案例是完全缺失 以下pom.xml用作参考 https://github.com/kkapelon/java-testing-with- spock/blob/master/chapter7/spring-standalone-