【发布时间】:2020-08-29 18:22:00
【问题描述】:
这是我正在测试的方法,我正在使用 mockito 进行模拟:
/**
* Sync get all children under single zk node.
*
* @param zk
* zookeeper client
* @param node
* node path
* @return direct children
* @throws InterruptedException
* @throws IOException
*/
public static List<String> getChildrenInSingleNode(final ZooKeeper zk, final String node, long zkOpTimeoutMs)
throws InterruptedException, IOException, KeeperException.NoNodeException {
final GetChildrenCtx ctx = new GetChildrenCtx();
getChildrenInSingleNode(zk, node, new GenericCallback<List<String>>() {
@Override
public void operationComplete(int rc, List<String> ledgers) {
synchronized (ctx) {
if (Code.OK.intValue() == rc) {
ctx.children = ledgers;
}
ctx.rc = rc;
ctx.done = true;
ctx.notifyAll();
}
}
});
synchronized (ctx) {
long startTime = System.currentTimeMillis();
while (!ctx.done) {
try {
ctx.wait(zkOpTimeoutMs > 0 ? zkOpTimeoutMs : 0);
} catch (InterruptedException e) {
ctx.rc = Code.OPERATIONTIMEOUT.intValue();
ctx.done = true;
}
// timeout the process if get-children response not received
// zkOpTimeoutMs.
if (zkOpTimeoutMs > 0 && (System.currentTimeMillis() - startTime) >= zkOpTimeoutMs) {
ctx.rc = Code.OPERATIONTIMEOUT.intValue();
ctx.done = true;
}
}
}
if (Code.NONODE.intValue() == ctx.rc) {
throw new KeeperException.NoNodeException("Got NoNode on call to getChildren on path " + node);
} else if (Code.OK.intValue() != ctx.rc) {
throw new IOException("Error on getting children from node " + node);
}
return ctx.children;
}
这是我的测试课:
@RunWith(value= Parameterized.class)
public class ZkUtilsGetChildrenTest {
private boolean expectedResult;
private ZooKeeper zkc ;
private String path;
private long timeout;
private static List<String> paths = Arrays.asList("/ledgers/000/000/000/001", "/ledgers/000/000/000/002",
"/ledgers/000/000/000/003");
private static List<String> childPaths = Arrays.asList("001", "002", "003");
// ZooKeeper related variables
private static ZooKeeperUtil zkUtil = new ZooKeeperUtil();
@Mock
ZkUtils.GetChildrenCtx mocked = mock(ZkUtils.GetChildrenCtx.class) ;
@BeforeClass
public static void setUp() throws Exception {
zkUtil.startCluster();
ZooKeeper initializerZkc = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 10000, null);
for (String path : paths ){
ZkUtils.createFullPathOptimistic(initializerZkc, path, "data".getBytes() , ZooDefs.Ids.OPEN_ACL_UNSAFE ,
CreateMode.CONTAINER);
}
}
@AfterClass
public static void tearDown() throws Exception {
zkUtil.killCluster();
}
@Parameterized.Parameters
public static Collection<Object[]> getTestParameters() throws IOException {
return Arrays.asList(new Object[][]{
{false , "null" , "/ledgers/000/000/000/004" , 0},
{true , "new" , "/ledgers/000/000/000" , 1000 },
{false , "wrong" , "/ledgers/000/000/000/00b" , -1},
{false , "new" , "/ledgers/000/000/00b" , 0 },//aggiunto per migliorare statement e branch coverage
{false , "new" , "/ledgers/000/000/003" , 1 },//aggiunto per migliorare statement e branch coverage
{false , "mock" , "/ledgers/000/000" , 1000 },//aggiunto per migliorare statement coverage
});
}
public ZkUtilsGetChildrenTest(boolean expectedResult ,String zkc , String path , long timeout) throws IOException {
if(zkc == "null"){
this.zkc = null;
}else if( zkc == "wrong"){
this.zkc = new ZooKeeper("wrongString", 10000, null);
}else if(zkc == "new"){
this.zkc = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 10000, null);
}else if(zkc == "mock"){
//TODO MOCK THE INNER METHOD
this.zkc = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 10000, null);
when(mocked).thenThrow(new InterruptedException());
}
this.expectedResult = expectedResult;
this.path = path;
this.timeout = timeout;
}
@Test
public void testGetChildrenInSingleNode() {
boolean realResult;
try {
List<String> children = ZkUtils.getChildrenInSingleNode(zkc, path, timeout);
assertThat(children, is(childPaths));
} catch (Exception e) {
realResult = false;
e.printStackTrace();
assertEquals(expectedResult, realResult);
}
}
}
我想问你如何模拟方法 ctx.wait(zkOpTimeoutMs > 0 ? zkOpTimeoutMs : 0);这样我就可以触发该语句后面的 catch 块:如果我调用此方法,我将模拟此类的一个 istance 并在该模拟上调用该方法,但因为不是我调用此方法,而是由下面的方法调用测试,我怎样才能正确地模拟它?
祝大家今天好!
【问题讨论】:
标签: java unit-testing intellij-idea mocking mockito