我正在阅读一些Java文本并获得以下代码:
int[] a = {4,4};
int b = 1;
a[b] = b = 0;
Run Code Online (Sandbox Code Playgroud)
在文中,作者没有给出明确的解释,最后一行的效果是: a[1] = 0;
我不太清楚我理解:评估是如何发生的?
这将是一个简单的,但我找不到它们和使用哪一个,如果我有两个lib包含在我的类路径中?
在BundleProcessorTest.java中的以下两个测试用例中,我得到的是异常,但是,我的第一个测试用例成功通过了.
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:此处检测到错位的参数匹配器:
- > at bundle.test.BundleProcessorTest.bundlePluginShouldNotBeNull(BundleProcessorTest.java:22)
您不能在验证或存根之外使用参数匹配器.正确使用参数匹配器的示例:when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); 验证(模拟).someMethod(包含( "富"))
此外,此错误可能会显示,因为您使用参数匹配器与无法模拟的方法.以下方法无法进行存根/验证:final/private/equals()/ hashCode().
at the package.test.BundleProcessorTest.bundlePluginCollectionShouldNotBeNull(BundleProcessorTest.java:28)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
请在下面找到简化的代码清单: -
BundlePlugin.java
package bundle;
import java.util.List;
public class BundlePlugin {
private final String pluginName ;
private final List<String> featureContent ;
public BundlePlugin(String pluginName, List<String> featureContent) {
super();
this.pluginName = pluginName;
this.featureContent = featureContent;
}
public String getPluginName() {
return pluginName;
}
public List<String> getFeatureContent() {
return featureContent;
}
}
Run Code Online (Sandbox Code Playgroud)
BundleProcessor.java
package bundle;
import java.util.ArrayList;
import …Run Code Online (Sandbox Code Playgroud) 我有一个Foo方法接口int Foo.bar(int),我想用Mockito模拟.99如果我传入,我希望模拟的方法返回1,但所有其他值将抛出异常.我可以这样做吗?
final Foo foo = mock(Foo.class);
when(foo.bar(1)).thenReturn(99);
when(foo.bar(anyInt())).thenThrow(new IllegalArgumentException());
Run Code Online (Sandbox Code Playgroud)
换句话说,1优先考虑anyInt()?我不希望它抛出异常1.该文档说,对于多个定义,最后的定义是比较重要的,但我不能告诉这是否意味着对于相同的参数或没有.如果它适用于此,我是否需要先定义通配符anyInt()?或者两者甚至有任何关系,因为其中一个是匹配器而另一个只是一个值?
我试图验证具有以下签名的方法被调用:
public void process(Map<String, Set<String>> data) {
...
}
Run Code Online (Sandbox Code Playgroud)
嵌套的参数化Set给我带来了困难.我可以使用any()匹配器来正确验证它,如下所示:
verify(dataProcessor).process(Matchers.<Map<String, Set<String>>> any());
Run Code Online (Sandbox Code Playgroud)
如Mockito中所述:使用泛型参数进行验证虽然令人讨厌,但如果我直接静态导入Matchers.any并将其称为以下内容则无效:
verify(dataProcessor).process(<Map<String, Set<String>>> any())
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,anyMapOf(clazz,clazz)似乎更合适.既然你不能做Set.class我不知道你会怎么做.以下不起作用,因为缺乏通用:
verify(dataProcessor).process(anyMapOf(String.class, Set.class));
Run Code Online (Sandbox Code Playgroud)
是否可以使用anyMapOf验证这种情况,还是应该坚持使用Matchers.<> any()?
我有以下代码:
private MyService myService;
@Before
public void setDependencies() {
myService = Mockito.mock(MyService.class, new StandardServiceAnswer());
Mockito.when(myService.mobileMethod(Mockito.any(MobileCommand.class), Mockito.any(Context.class)))
.thenAnswer(new MobileServiceAnswer());
}
Run Code Online (Sandbox Code Playgroud)
我的意图是,所有对被嘲笑者的召唤myService都应以标准方式回答.但是mobileMethod,应以特定方式回答(公开的)呼叫.
我发现的是,当我到达行添加调用的答案mobileMethod而不是附加时MobileServiceAnswer,Java实际上是在调用myService.mobileMethod,这会导致NPE.
这可能吗?看起来应该可以覆盖默认答案.如果有可能,这样做的正确方法是什么?
这是我的Answers:
private class StandardServiceAnswer implements Answer<Result> {
public Result answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Command command = (Command) args[0];
command.setState(State.TRY);
Result result = new Result();
result.setState(State.TRY);
return result;
}
}
private class MobileServiceAnswer implements Answer<MobileResult> {
public MobileResult answer(InvocationOnMock invocation) {
Object[] args …Run Code Online (Sandbox Code Playgroud) 我需要在Mockito中验证一个带有多个参数的方法,但需要只捕获一个参数,其他我只需要一个简单的匹配器.那可能吗?
例如,如果我有:
@Mock
private Map<K,V> mockedMap;
...
ArgumentCaptor<K> argument = ArgumentCaptor.forClass(K.class);
verify(mockedMap).put(argument.capture(), any(V.class));
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我是否需要为每个参数编写一个captor,尽管我只需要捕获第一个参数?
以下代码:
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
Mockito.doThrow(new IOException()).when(mapper).writeValue((OutputStream) Matchers.anyObject(), Matchers.anyObject());
Mockito.doNothing().when(mapper).writeValue((OutputStream) Matchers.anyObject(), Matchers.anyObject());
try {
mapper.writeValue(new ByteArrayOutputStream(), new Object());
} catch (Exception e) {
System.out.println("EXCEPTION");
}
try {
mapper.writeValue(new ByteArrayOutputStream(), new Object());
} catch (Exception e) {
System.out.println("EXCEPTION");
}
Run Code Online (Sandbox Code Playgroud)
预期的产出是
例外
对?
但我一无所获
如果我在doNothing之后做了doThrow我得到了
例外情况
除外
所以它看起来像是最后一个被嘲笑的模拟......我认为它会按照他们注册的顺序进行模拟吗?
我想制作一个模拟第一次抛出异常,第二次正常完成...
我正在尝试使用以下参数模拟静态方法:
Mockito.when(StaticClass.staticMethod(Mockito.any(A.class),
Mockito.any(B.class), SomeEnum.FOO))
.thenReturn(true);
Run Code Online (Sandbox Code Playgroud)
我添加了以下注释:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Parameterized.class)
@PrepareForTest({StaticClass.class, A.class, B.class})
Run Code Online (Sandbox Code Playgroud)
但是 Mockito.any 总是返回null。为什么 ?
我正在使用Mockito进行单元测试,我得到以下异常.
org.mockito.exceptions.base.MockitoException:
`'setResponseTimeStampUtc'` is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
doThrow(exception).when(mock).someVoidMethod();
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. The method you are trying to stub is *overloaded*. Make sure you are calling the right overloaded version.
2. Somewhere in your test you are stubbing *final methods*. Sorry, Mockito does not verify/stub …Run Code Online (Sandbox Code Playgroud)