简体   繁体   English

HttpEntity <String> request = new HttpEntity <String> (); 我如何在java中模拟它

[英]HttpEntity<String> request = new HttpEntity<String>( ); How do I mock this in java

我试图模拟泛型我能够模拟一些通用表达式,但我不知道如何模拟通用初始化

Generic in java are a compiler trick implemented using erasure (with the option of some runtime artifacts, true). java中的泛型是使用擦除实现的编译器技巧(可选择一些运行时工件,true)。 So there isn't really a separate class definition for HttpEntity<String> vs HttpEntity<Foo> . 因此, HttpEntity<String>HttpEntity<Foo>之间并没有真正的单独的类定义。

So you would simply need to (A) simply cast and (B) make sure a mock that represents a certain type is only used with that type and not cast and re-used for another type. 所以你只需要(A)简单地转换和(B)确保代表某种类型的模拟只用于那种类型而不是强制转换并重新用于另一种类型。

If this is not answering your question, please paste the exact source code you are trying to unit test and indicate which component are you testing and which ones you are trying to mock. 如果这没有回答您的问题,请粘贴您尝试进行单元测试的确切源代码,并指出您正在测试哪个组件以及您尝试模拟哪些组件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM