简体   繁体   English

NockPointerException当Mockito嘲笑ObjectOutputStream.writeObject吗?

[英]NullPointerException when Mockito mocking ObjectOutputStream.writeObject?

I'm writing a jUnit test, checking what a class writes to ObjectOutputStream. 我正在编写一个jUnit测试,检查类将什么写入ObjectOutputStream。

Specifically, I'm checking that a class is calling writeObject with a certain type: 具体来说,我正在检查一个类是否正在以某种类型调用writeObject:

ObjectOutputStream out = mock(ObjectOutputStream.class);

out.writeObject(new Something());

verify(out).writeObject(isA(Something.class));

But Java is complaining of a NullPointerException inside of writeObject . 但是Java抱怨writeObject内部存在NullPointerException。

What do I need to stub to fix this? 我需要存根解决此问题吗?

This isn't possible with Mockito. Mockito无法做到这一点。 According to the ObjectOutputStream javadocs , the writeOutput method is final: 根据ObjectOutputStream javadocswriteOutput方法是最终的:

public final void writeObject(Object obj) throws IOException

Internally, Mockito actually mocks these objects by creating a Proxy , which dynamically overrides ObjectOutputStream. 在内部,Mockito实际上通过创建Proxy来模拟这些对象,该Proxy动态覆盖ObjectOutputStream。 Because the writeObject method is final, though, you aren't able to override or mock it and Java calls the original. 但是,由于writeObject方法是最终的,因此您无法覆盖或模拟它,而Java会调用原始方法。 This is listed as one of the limitations of Mockito . 这被列为Mockito限制之一

Though you may be able to verify against writeObjectOverride , your better bet is actually to refactor your code to use the interface ObjectOutput instead of ObjectOutputStream directly. 尽管您可以针对writeObjectOverride进行验证,但更好的选择实际上是重构代码,以直接使用ObjectOutput接口而不是ObjectOutputStream接口。 ObjectOutputStream implements ObjectOutput, and Mockito can mock any method of an interface. ObjectOutputStream实现ObjectOutput,而Mockito可以模拟接口的任何方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何模拟objectOutputStream.writeObject()? - How to mock objectOutputStream.writeObject()? 尝试在类之间写入 object 时 ObjectOutputStream.writeObject() 冻结 - ObjectOutputStream.writeObject() freezing when trying to write object between classes 为什么ObjectOutputStream.writeObject不采用Serializable? - Why does ObjectOutputStream.writeObject not take a Serializable? ObjectOutputStream.writeObject 对文件线程安全吗? - Is ObjectOutputStream.writeObject to a file thread safe? 使用ObjectOutputStream.writeObject()无法进行数据传输 - data transfer not working using ObjectOutputStream.writeObject() 我应该同步 ObjectOutputStream.writeObject(Object) 吗? - Should I synchronize ObjectOutputStream.writeObject(Object)? 为什么在写字节数组时ObjectOutputStream.writeObject比写字节快? - Why is ObjectOutputStream.writeObject faster than write bytes when writing a byte array? 编写可序列化的对象时,ObjectOutputStream.writeObject()(在socket.getOutputStream()顶部)抛出断线(IO异常) - ObjectOutputStream.writeObject() (on top of socket.getOutputStream()) throws broken pipe (IO Exception) when writing a serializable object Java,ObjectOutputStream.writeObject将随机符号打印到文件 - Java, ObjectOutputStream.writeObject prints random symbols to file 如何使用ObjectOutputStream.writeObject()+ Base64(Java)对对象进行深度序列化? - How to deep serialize object using ObjectOutputStream.writeObject()+Base64 (Java)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM