问题描述
我正在为遗留代码编写单元测试,例如:
Im writing a unit test for my legacy code which is something like:
public class SomeClass {
public SomeResponse logToServer() {
SomeResponse response = null;
try{
RestTemplate restTemplate = new RestTemplate();
SomeRequestBean request = new SomeRequestBean();
response = restTemplate.postForEntity("http://someUrl", request, SomeResponse.class);
System.out.println(response.toString());
} catch(Exception e) {
e.printStackTrace();
}
return response;
}
}
我知道我可以将RestTemplate移到上方并将其注释为@AutoWire
并将其声明为Bean类,或使用Power Mockito模拟新实例的创建.但是我不想更改此遗留代码,并且我不惜一切代价避免使用Power Mockito,因为仅对于一次测试,我就不想在我的pom.xml
上添加一个全新的依赖项.因此,我想知道有什么方法可以模拟此 restTemplate 吗?
I know I can move RestTemplate above and annotate it as @AutoWire
and declare it as bean class or use power mockito to mock the new instance creation. But I don't wanna change this legacy code and I'm avoiding using power Mockito at all cost because just for one test I don't wanna add a whole new dependency to my pom.xml
. So just wondering is there any way using which I can mock this restTemplate ?
推荐答案
要回答您的问题,在您发布的限制下,没有办法模拟RestTemplate和单元测试.
To answer you question, no under the restrictions you've posted there is no way to mock RestTemplate and unit test it.
我确实认为您可以略微更改旧版代码,因为此更改无法正常工作,在这种情况下可能值得这样做.但是我会坚持你不能做到的.
I do think that you can slightly change the legacy code because this change is not functional and in this case it might worth it. But I'll stick with you that you can't.
关于电源模拟和电源模拟.尽管我同意应避免使用这些工具,但并非出于您已发布的原因.请注意,这种依赖关系属于测试范围,即使对于旧版环境,它也不会到达生产环境.因此,如果优先事项是不更改遗留代码,那么引入PowerMock就是最少的麻烦".
Regarding power mock and power mockito. Although I agree that these tools should be avoided but not for a reason that you've posted. Note, that this dependency is of test scope, it won't reach the production anyway even for legacy envrionments. So if the priority is to not change the legacy code, then introducing PowerMock is the "least evil".
但是,如果我们专门谈论rest模板,则可以利用有关spring rest模板的一些事实,这些事实无论如何都可以用于测试它.
If we're talking specifically about the rest template though, you can take advantage of some facts about spring rest template specifically that can be used for testing it anyway.
选项1
第一种技术(如果环境允许)是使用@RestClientTest
注释.它将允许指定被测服务,并将提供名为MockRestServiceServer
的模拟实现,该模拟表示您要在模拟环境中尝试连接的服务器.然后,您将能够从该服务器指定期望值,并希望代码能够运行.注意:这不是单元测试-这是一个从spring上下文开始的集成测试,因此它将比常规单元测试重/慢.
The first technique (if the environment permits) is using @RestClientTest
annotation. It will allow specifying the service under test and will provide a mock implemetation of something called MockRestServiceServer
that will represent the server you're trying to connect to in the mocked environment. Then you'll be able to specify the expectations from this server and hopefully the code will run. Caution: this is not a unit test - this is an integration test that starts spring context, so it will much heavier/slower than regular unit test.
此处,您可以找到此方法的有效示例,请查看本文还包含其他技术.
Here you can find a working example of this approach, check out this article it contains also other techniques.
选项2
第二种技术背后的思想是RestTemplate
实际上是客户端库之上的包装器,它本身不进行任何HTTP互通.
The idea behind the second technique is that RestTemplate
is actually a wrapper above client libraries, it doesn't do any http intercommunication by itself.
可以将其配置为与apache的HttpClient,OkHttpClient一起使用,默认情况下,它与URLConnection一起为每个请求打开连接.因此,您可以创建一个测试,该测试将配置其余客户端以使其与您的兴趣/选择的某些特定引擎一起运行,然后检查如何测试直接使用此引擎的代码.解决方案将根据项目中使用的实际引擎而有所不同.
It can be configured to work with HttpClient of apache, OkHttpClient, by default it works with URLConnection opening connection for each request. So You could create a test that would configure the rest client to run with some particular engine of your interest/choice and then check out how to test code that uses this engine directly. The solutions will be different depending on the actual engine used in the project.
这篇关于如何模拟RestTemplate的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!