我有一个单元测试,在该测试中,我正在模拟java.net.URI
类。进一步地,我创建了一个jMockit的NonStrictExpectation
,在此处期望调用URI.getPath()
并返回一个特定的字符串。
被测试的代码会调用URI.getPath()
两次,我需要每次发送不同的字符串。
这是我的实际测试方法:
public void validateResource() {
// some code
URI uri = new URI(link1.getHref());
String path1 = uri.getPath();
// some more code
uri = new URI(link2.getHref());
String path2 = uri.getPath();
}
以下是单元测试代码:
@Mocked URI uri;
@Test
public void testValidateResource() {
new NonStrictExpectations() {
{
// for the first invocation
uri.getPath(); returns("/resourceGroup/1");
// for the second invocation [was hoping this would work]
uri.getPath(); returns("/resource/2");
}
};
myObject.validateResource();
}
现在,我希望当第二次调用URI.getPath()
时,我的期望是返回"/resource/2"
。但它总是命中第一个期望并返回"/recourceGroup/1"
。这是我的问题。
我该如何做到这一点?由于种种原因,我不能使用StrictExpectations
,必须坚持使用NonStrictExpectations
。