Java 设置this.updateTime,但在运行测试时会引发NullPointerException
在我的测试中我有:Java 设置this.updateTime,但在运行测试时会引发NullPointerException,java,junit,nullpointerexception,this,Java,Junit,Nullpointerexception,This,在我的测试中我有: final MockKeyReader mockReader=(MockApiKeyReader)getKeyReader(); mockReader.setUpdateTime(100L); MockKeyReader有以下方法: public void setUpdateTime(最终长更新时间){ this.updateTime=updateTime; } 我认为是因为setUpdateTime设置了this.updateTime的值,当mockReader.set
final MockKeyReader mockReader=(MockApiKeyReader)getKeyReader();
mockReader.setUpdateTime(100L);
MockKeyReader
有以下方法:
public void setUpdateTime(最终长更新时间){
this.updateTime=updateTime;
}
我认为是因为setUpdateTime
设置了this.updateTime
的值,当mockReader.setUpdateTime(100L)时当我试图测试的类查找它时,它将可用。我是否误解了这个的工作原理
这是引发异常的行:
如果(!this.updateTime.equals)(ProductionBlocking
.get(this.keyReader.updateTime)(this.config.configId.toString(),
keyType.PLUSSTAR))){
我刚刚意识到,我正在尝试将两个长度
与.equals()
进行比较。我将行更改为:
if(this.updateTime!=ProductionBlocking
.get(this.keyReader.updateTime)(this.config.configId.toString(),
keyType.PLUSSTAR))){
但是问题仍然存在。this.updateTime
和ProductionBlocking
.get(this.keyReader.updateTime)(this.config.configId.toString(),
keyType.PLUSSTAR))
为null
我希望mockReader.setUpdateTime(100L);
在setUpdateTime
中设置this.updateTime
,但这并没有发生
我觉得我的理解可能不正确
ProductionBlocking
.get(this.keyReader.updateTime)(this.config.configId.toString(),
键类型(PLUSSTAR)
使用接口ApiKeyReader
并且MockApiKeyReader
是一个模拟的接口。这个。updateTime应该等于updateTime而不是LastUpdateTime,或者最好将方法参数重命名为LastUpdateTime。很抱歉,之前已经更改了。错过了最后一个。我已经在查询中修复了这个问题,但仍然存在问题e、 您确定NPE在抛出的MockKeyReader中?可能方法getKeyReader()返回null。可能您可以添加NPE抛出的行问题已用其他信息更新。