Java 如何模拟ElasticSeach客户端
我正在努力模仿下面的代码:Java 如何模拟ElasticSeach客户端,java,unit-testing,elasticsearch,junit,Java,Unit Testing,elasticsearch,Junit,我正在努力模仿下面的代码: Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); 我的方法: Settings.Builder settingsBuilder = mock(Settings.Builder.class);
Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
我的方法:
Settings.Builder settingsBuilder = mock(Settings.Builder.class);
when(settingsBuilder.put(new String("test"), new String("test"))).thenReturn(settings.settingsBuilder());
when(settingsBuilder.build()).thenReturn(settings);
TransportClient.Builder clientBuilder = mock(TransportClient.Builder.class);
when(clientBuilder.settings(settings)).thenReturn(clientBuilder);
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
到目前为止,测试工作正常,但当尝试模拟客户端上使用的其他类时,Mockito抛出以下异常:
错误:
Settings.Builder settingsBuilder = mock(Settings.Builder.class);
when(settingsBuilder.put(new String("test"), new String("test"))).thenReturn(settings.settingsBuilder());
when(settingsBuilder.build()).thenReturn(settings);
TransportClient.Builder clientBuilder = mock(TransportClient.Builder.class);
when(clientBuilder.settings(settings)).thenReturn(clientBuilder);
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
问题:
Settings.Builder settingsBuilder = mock(Settings.Builder.class);
when(settingsBuilder.put(new String("test"), new String("test"))).thenReturn(settings.settingsBuilder());
when(settingsBuilder.build()).thenReturn(settings);
TransportClient.Builder clientBuilder = mock(TransportClient.Builder.class);
when(clientBuilder.settings(settings)).thenReturn(clientBuilder);
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
现在,我明白了,这可能会被抛出,因为我没有正确地模仿某些东西,但我无法找出我这次模仿的错误。如何正确模拟客户机,以便模拟所有依赖类并返回新的模拟客户机
谢谢。我想根据你们的类名在这里猜一下
Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
假设TransportClient.builder()返回TransportClient.builder,则需要:
when(clientBuilder.settings(anyOf(Settings.class)).thenReturn(clientBuilder);
when(clientBuilder.build()).thenReturn(a transport client mock);
我假设TransportClient.addTransportAddress不需要模拟——它只需要验证。我将根据您的类名猜一猜
Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
假设TransportClient.builder()返回TransportClient.builder,则需要:
when(clientBuilder.settings(anyOf(Settings.class)).thenReturn(clientBuilder);
when(clientBuilder.build()).thenReturn(a transport client mock);
我假设TransportClient.addTransportAddress不需要模拟,它只需要验证。Hi@Jerry Andrews感谢您的建议。它解决了问题,但现在我得到以下错误:需要但未调用:transportClient.addTransportAddress(localhost/127.0.0.1:9300);我已经验证了addTransportAddress,请参见下面的验证(mockedTransportClient).addTransportAddress(新的InetSocketTransportAddress(InetAddress.getByName(“localhost”),9300));你知道是什么导致了这个问题吗?谢谢我建议你在addTransportAddress中添加一个captor,而不是精确地验证入站地址,然后看看它是如何被调用的(如果它被调用的话)。嗨@Jerry Andrews谢谢你的建议。它解决了问题,但现在我得到以下错误:需要但未调用:transportClient.addTransportAddress(localhost/127.0.0.1:9300);我已经验证了addTransportAddress,请参见下面的验证(mockedTransportClient).addTransportAddress(新的InetSocketTransportAddress(InetAddress.getByName(“localhost”),9300));你知道是什么导致了这个问题吗?谢谢,我建议您在addTransportAddress中添加一个captor,而不是精确地验证入站地址,并查看它实际上是如何被调用的(如果它被调用的话)。