【问题标题】:Java.lang.nullpointer exception in junit testing [duplicate]junit测试中的Java.lang.nullpointerexception [重复]
【发布时间】:2017-11-09 18:56:27
【问题描述】:

我的 junit 代码是

@Autowired
private ABC abc;

@Test
public void checkZonePresent() {
    assertThat(abc.getZoneList().size()).isEqualTo(1);
}

所以,这里我收到类似 java.lang.nullpointer 异常的错误

【问题讨论】:

标签: java spring-boot junit


【解决方案1】:

乍一看 abc.getZoneList() 为空。但是,如果您没有使用 spring 配置测试(没有),NPE 也可能会发生。没有代码很难说,请告诉您如何配置测试类

零步(在您的情况下它会有所帮助,但通常如果您配置了弹簧测试,则不需要测试它):

assertThat(abc ,is(notNullValue());

首先你应该检查:

assertThat(abc.getZoneList(),is(notNullValue());

看起来像,abc.getZoneList(),这是个问题 下一个检查应该是

assertThat(abc.getZoneList().size()).isEqualTo(1)

你的测试失败了,没有解释为什么,但测试应该解释出了什么问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 2018-02-26
    相关资源
    最近更新 更多