模拟 class 未在 Mono.map() 内调用

Mocked class not called within Mono.map()

我有一个 Spring Webflux 控制器,它将业务逻辑和映射委托给一对 @Service 类:

@RestController
@RequestMapping("/v1/transactions")
class TransactionController {

    @Autowired
    TransactionService service

    @Autowired
    TransactionMapper mapper

    Flux<Transaction> getTransactions(@PathVariable String id) {
        service.getTransactions(id)
               .map({ mapper.mapTransaction(it) })
    
    }
}

我的单元测试一直有效,直到它需要进入 map() 函数。我收到的调用太少 1 * mockMapper.mapTransaction(_) >> Flux.just(new MappedTransaction()).

class TransactionControllerSpec extends Specification {
    def mockService = Mock(TransactionService)
    def mockMapper = Mock(TransactionMapper)

    def controller = new TransactionController(service: mockService, mapper: mockMapper)

    def "should call the transactions service to fetch a list of transactions"() {
        given: "an id"
        def id = "123"

        when: "the controller is called to fetch data"
        def result = controller.getTransactions(id)

        and: "code is executed"
        StepVerifier
            .create(result)
            .consumeNextWith({
               // some assertions here
            })

        then: "the service layer is called"
        1 * mockService.getTransactions(id) >> Flux.just(new Transaction())
        1 * mockMapper.mapTransaction(_) >> Flux.just(new MappedTransaction())
        //  Returns "Too few invocations (0 invocations)"
    }
}

我注意到如果我在 Mono.map() 函数中设置断点,我的断点不会命中。

好吧,这是我的一个巨大疏忽。但是,缺少的部分是我忘记告诉 StepVerifier 以验证 Flux 是否完整:

        and: "code is executed"
        StepVerifier
            .create(result)
            .consumeNextWith({
               // some assertions here
            })
            .verifyComplete()
            //  subscribes to the Mono/Flux