• 请不要在回答技术问题时复制粘贴 AI 生成的内容
git00ll
V2EX  ›  程序员

Spock 为啥在 when 块中定义的原始类型不能正确初始化

  •  
  •   git00ll · Jul 9, 2021 · 1078 views
    This topic created in 1774 days ago, the information mentioned may be changed or developed.

    Spock 为啥在 when 块中定义的原始类型需要手动初始化

    这条会执行出错

        def test() {
            given:
            int a
            when:
            int b
            then:
            noExceptionThrown()
        }
    
    
    

    这条执行没问题

        def test() {
            given:
            int a
            when:
            int b = 0
            then:
            noExceptionThrown()
        }
    
    

    他们区别是,没问题的测试对变量 b 进行初始化了,而第一个没有初始化。 报 NullPointerException

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   974 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 20:16 · PVG 04:16 · LAX 13:16 · JFK 16:16
    ♥ Do have faith in what you're doing.