when(someMock.someMethod()).doAnswer(new Answer() { private int count = 0; public Object answer(InvocationOnMock invocation) { if (count++ == 1) return 1; return 2; } });
VendorObject vo = mock(VendorObject.class, new Answer() { @Override public Object answer(InvocationOnMock invocation) { // 1. Check if method exists on RedirectToObject. // 2a. If it does, call the method with the args and return the // result. // 2b. If it does not, throw an exception to fail the unit test. } });
Mockito.when(reader.document(anyInt())).thenAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); return document(fakeIndex((int)(Integer)args[0])); } });
@Override public Answer execute(final CreateStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) { return new Answer(command, true, "success"); } }
@Override public Answer execute(final ModifySshKeysCommand command, final CitrixResourceBase citrixResourceBase) { return new Answer(command); } }
@Override public Answer execute(final UpgradeSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) { return new Answer(command, true, "success"); } }
doAnswer(new Answer() { Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); ((MyClass)args[0]).myClassSetMyField(NEW_VALUE); return null; // void method, so return null } }).when(mock).someMethod();
/** * SR scan in xenserver */ @Override public Answer introduceObject(IntroduceObjectCmd cmd) { LOGGER.debug("execute introduceObject: "+ cmd.getClass()); return new Answer(cmd, false, "not implemented yet"); } /**
/** * Only copies in case of dest is NfsTO, xenserver also unmounts secstorage */ @Override public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) { LOGGER.debug("execute copyVolumeFromPrimaryToSecondary: "+ cmd.getClass()); return new Answer(cmd); } /**
/** * createprivatetemplate, also needs template.properties */ @Override public Answer createTemplateFromVolume(CopyCommand cmd) { LOGGER.debug("execute createTemplateFromVolume: "+ cmd.getClass()); return new Answer(cmd); } /**
doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); List<?> list = (List<?>) args[0]; list.clear(); return null; } }).when(test).clearList(any(List.class));
when(mockHttpClient.executeMethod(any(GetMethod.class))).thenAnswer(new Answer() { Object answer(InvocationOnMock invocation) { GetMethod getMethod = (GetMethod) invocation.getArguments()[0]; Field respStream = HttpMethodBase.class.getDeclaredField("responseStream"); respStream.setAccessible(true); respStream.set(getMethod, new FileInputStream(source)); return HttpStatus.SC_OK; } });
protected Answer createErrorAnswer(final Command cmd, final String msg, final Throwable th) { final StringWriter writer = new StringWriter(); if (msg != null) { writer.append(msg); } writer.append("===>Stack<==="); th.printStackTrace(new PrintWriter(writer)); return new Answer(cmd, false, writer.toString()); }
@Override public Answer execute(final CleanupNetworkRulesCmd command, final LibvirtComputingResource libvirtComputingResource) { final boolean result = libvirtComputingResource.cleanupRules(); return new Answer(command, result, ""); } }
@Override public Answer execute(final SetHostParamsCommand command, final LibvirtComputingResource libvirtComputingResource) { final Map<String, String> params = command.getParams(); boolean success = libvirtComputingResource.getVirtRouterResource().configureHostParams(params); if (!success) { return new Answer(command, false, "Failed to set host parameters"); } else { return new Answer(command, true, null); } } }
@Override public Answer execute(final OvsDestroyBridgeCommand command, final LibvirtComputingResource libvirtComputingResource) { final boolean result = libvirtComputingResource.destroyTunnelNetwork(command.getBridgeName()); if (!result) { s_logger.debug("Error trying to destroy OVS Bridge!"); } return new Answer(command, result, null); } }
@Override public Answer sendMessage(Command cmd) { if ((cmd instanceof CopyCommand) || (cmd instanceof DownloadCommand)) { return resource.executeRequest(cmd); } // TODO Auto-generated method stub return new Answer(cmd, false, "unsupported command:" + cmd.toString()); }
private Answer execute(DownloadCommand cmd) { DataStoreTO dstore = cmd.getDataStore(); if (dstore instanceof NfsTO || dstore instanceof S3TO) { return _dlMgr.handleDownloadCommand(this, cmd); } else if (dstore instanceof SwiftTO) { return registerTemplateOnSwift(cmd); } else { return new Answer(cmd, false, "Unsupported image data store: " + dstore); } }
@Override public Answer execute(final OvsSetupBridgeCommand command, final LibvirtComputingResource libvirtComputingResource) { final boolean findResult = libvirtComputingResource.findOrCreateTunnelNetwork(command.getBridgeName()); final boolean configResult = libvirtComputingResource.configureTunnelNetwork(command.getNetworkId(), command.getHostId(), command.getBridgeName()); final boolean finalResult = findResult && configResult; if (!finalResult) { s_logger.debug("::FAILURE:: OVS Bridge was NOT configured properly!"); } return new Answer(command, finalResult, null); } }