简体   繁体   English

如何避免在JUnit Groovy中进行外部调用

[英]How to avoid external call in JUnit Groovy

Finally I am able to get Groovy class in my Java file and able to perform JUnit on my groovy classes. 最终,我能够在Java文件中获取Groovy类,并能够对我的Groovy类执行JUnit。 Now my problem is I want to test my method which triggers RPC calls.but I want to avoid that call in unit test. 现在我的问题是我想测试触发RPC调用的方法,但是我想避免在单元测试中调用该方法。 How should I do that? 我应该怎么做? Below is my sample code for logout how I am proceeding 以下是我注销程序的示例代码

@Test
public void logoutTest() {
    GroovyObject loginOject =new Login();
    GroovyObject logoutObject =new Logout();
    XMLRPCServerProxy serverProxy = (XMLRPCServerProxy) loginOject.invokeMethod(
        "getServerProxy",
        "https://urlproxy"
    );
    String sessionId = (String) loginOject.invokeMethod(
        "getSession",
        new Object[]{"username","password",serverProxy}
    );
    logoutObject.invokeMethod("logout",new Object[]{sessionId,serverProxy});
}

You can do so by using mocks. 您可以通过使用模拟来做到这一点。 I assume you are testing logoutObject.invokeMethod("logout",new Object[]{sessionId,serverProxy}); 我假设您正在测试logoutObject.invokeMethod("logout",new Object[]{sessionId,serverProxy}); method which calls a method on your XMLRPCServerProxy serverProxy . XMLRPCServerProxy serverProxy上调用方法的XMLRPCServerProxy serverProxy If you a have an option to introduce a dependency on mocking framework such as Mockito you can then create a mock instead of 如果您可以选择引入对模拟框架(例如Mockito)的依赖,则可以创建一个模拟而不是

XMLRPCServerProxy serverProxy = (XMLRPCServerProxy) oginOject.invokeMethod("getServerProxy", "https://urlproxy");

You can mock it 你可以嘲笑它

XMLRPCServerProxy serverProxy = mock(XMLRPCServerProxy.class);
when(serverProxy.sendRequest("payload")).thenReturn("result");

If you cannot use a mock library you can extend XMLRPCServerProxy in the test and override a method that does rpc call to do whatever you want (this is effectively what Mockito would do for you) 如果您不能使用模拟库,则可以在测试中扩展XMLRPCServerProxy并覆盖执行rpc调用以执行所需操作的方法(这实际上是Mockito会为您执行的操作)

XMLRPCServerProxy proxyServer = new XMLRPCServerProxy() {
        public String request(String payload) {
            // your mock code
            return "result";
        }
    };

I'd recommend using Spock for unit-testing Groovy (and Java) code. 我建议使用Spock进行Groovy(和Java)代码的单元测试。 Latest when it comes to mocking, no Java tool / framework I know is capable of properly mocking Groovy stuff as the language is simply too dynamic to be handled by tools written to hook into Java code. 关于模拟的最新信息,据我所知,没有任何Java工具/框架能够正确模拟Groovy的东西,因为该语言过于动态,以至于无法通过编写工具插入钩挂Java代码来处理。

Spock is written in Groovy and with Groovy in mind when it comes to mocking and stubbing. Spock是用Groovy编写的,在嘲笑和存根时要牢记Groovy。 Spock might look a bit alien if you first look at it, but if you get used to the syntax it is quite amazing what you can do and how easy you can write tests with variying input data and so on. 如果您先看一下Spock,可能看起来有些陌生,但是如果您习惯了语法,则可以做的事情以及使用可变的输入数据编写测试的难易程度等等都非常令人惊讶。

Spock also is based on JUnit, so every tool that is able to execute and evaluate JUnit tests is also able to cope with Spock tests. Spock也是基于JUnit的,因此每个能够执行和评估JUnit测试的工具都能够应对Spock测试。

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

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