gpt4 book ai didi

java - Mockito 规则未应用

转载 作者:行者123 更新时间:2023-12-01 11:35:56 25 4
gpt4 key购买 nike

我正在使用 Mockito 编写一个测试用例,在我看来,一个 Mockito 规则没有被定义。以下是我的场景。

public class DummyTest {
private final String[] alphabet= {"a", "b", "c", "d", "e" };
private final String[] numeric= {"1", "2", "3", "4", "5" };
AtomicInteger idx;
String currentAlphabet;
String currentNumeric;
int currentIndex;
Dummy dummyObj;

private ruleSetUp() {

dummyObj = Mockito.spy(new Dummy());
// Rule for rs.next()
Mockito.doAnswer(new Answer<Boolean>() {

@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
currentIndex = idx.getAndIncrement();
if (alphabet.length > currentIndex) {
currentAlphabet= alphabet[currentIndex];
currentNumeric= numeric[currentIndex];
return true;
} else
return false;
};
}).when(rs).next();

// Rule for rs.getInt()
Mockito.doAnswer(new Answer<Integer>() {

@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return idx.get() - 1;
};
}).when(rs).getInt(2);

// Rule for rs.getByte()
Mockito.doAnswer(new Answer<byte[]>() {

@Override
public byte[] answer(InvocationOnMock invocation) throws Throwable {
return currentNumeric.getBytes(StandardCharsets.UTF_8);
};
}).when(rs).getBytes(1);

Mockito.doReturn(currentAplhabet).when(dummyObj).innerMethod(Mockito.anyInt(), Mockito.anyInt());
}

@Test
public void methodTest(){


ruleSetUp();

}
}

我正在测试的方法如下:

class Dummy {

public void method(int param1) {

/* some code */
param2 = xyz() ; // Some mathematical calculation
while(rs.next()) {

byte[] strByte = rs.getBytes(1);
int number = rs.getInt(2);

String str= new String(strByte, StandardCharset.UTF-8);
// TEst Case rule causes below value to be always null
String alphabet = innerMethod(param1, param2);
}

}
}

执行此测试方法时,发现被测试方法中的alphabet值始终为空。根据 currentNumeric 的正确值,正确填充“str”的值。该循环也运行了正确的次数。

我很困惑为什么没有应用innerMethd()的规则。我没有收到任何错误,只是填充的值始终为空,无论循环正在经历哪一次迭代。

你们能指出我错在哪里以及解决方法吗?

谢谢

最佳答案

 String currentAlphabet;

private ruleSetUp() {
// [snip]
Mockito.doReturn(currentAlphabet).when(dummyObj).innerMethod(
Mockito.anyInt(), Mockito.anyInt());
}

每次调用 innerMethod 时,Mockito 都会返回 null 因为你告诉它。尽管每次调用 next 时都会设置 currentAlphabet,但在此之前您无需设置任何内容。在调用 ruleSetup 时,currentAlphabet 为 null,因此 Mockito 为每次调用 innerMethod stub null

您需要使用答案来 stub doReturn,而不是:

doAnswer(new Answer<String>() {
@Override public void answer(InvocationOnMock invocation) {
// This field on the test changes based on other invocations.
return currentAlphabet;
}
}).when(dummyObj).innerMethod(anyInt(), anyInt());

顺便说一句,由于您要替换多个相关方法的行为,因此为了可读性和易于维护,请考虑使用普通重写:

private DummyObj createDummy() {
return new DummyObj() {
private final String[] alphabet= {"a", "b", "c", "d", "e" };
private final String[] numeric= {"1", "2", "3", "4", "5" };
AtomicInteger idx;
String currentAlphabet;
String currentNumeric;
int currentIndex;

@Override public boolean next() { /* ... */ }
@Override public int nextInt() { /* ... */ }
@Override public byte nextByte() { /* ... */ }
@Override public String innerMethod() { /* ... */ }
};
}

关于java - Mockito 规则未应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29994578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com