Java Code Examples for org.easymock.EasyMock.createMock()

The following are Jave code examples for showing how to use createMock() of the org.easymock.EasyMock class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: cruise-control   File: KafkaCruiseControlServletDataFromTest.java   View Source Code Vote up 6 votes
@Test
public void test() {
  KafkaCruiseControl mockKCC = EasyMock.createMock(KafkaCruiseControl.class);
  KafkaCruiseControlState kccState = getState(_numReadyGoals, _totalGoals, _numValidWindows);
  EasyMock.expect(mockKCC.state()).andReturn(kccState).anyTimes();
  EasyMock.replay(mockKCC);
  
  KafkaCruiseControlServlet servlet = new KafkaCruiseControlServlet(mockKCC);
  KafkaCruiseControlServlet.GoalsAndRequirements goalsAndRequirements = 
      servlet.getGoalsAndRequirements(Collections.emptyList(),
                                      _dataFrom,
                                      false);
  
  assertEquals(new HashSet<>(goalsAndRequirements.goals()), new HashSet<>(_expectedGoalsToUse));
  if (_expectedRequirements != null) {
    assertEquals(_expectedRequirements.minRequiredNumSnapshotWindows(), 
                 goalsAndRequirements.requirements().minRequiredNumSnapshotWindows());
    assertEquals(_expectedRequirements.minMonitoredPartitionsPercentage(), 
                 goalsAndRequirements.requirements().minMonitoredPartitionsPercentage(), 0.0);
    assertEquals(_expectedRequirements.includeAllTopics(), 
                 goalsAndRequirements.requirements().includeAllTopics());
  } else {
    assertNull("The requirement should be null", goalsAndRequirements.requirements());
  }
}
 
Example 2
Project: dubbocloud   File: CompatibleFilterFilterTest.java   View Source Code Vote up 6 votes
@Test
public void testInvokerGeneric() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("$enumlength").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] { Enum.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setValue("High");
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertEquals(filterResult, result);
}
 
Example 3
Project: dubbocloud   File: ExceptionFilterTest.java   View Source Code Vote up 6 votes
@SuppressWarnings("unchecked")
@Test
public void testRpcException() {
    Logger logger = EasyMock.createMock(Logger.class);
    RpcContext.getContext().setRemoteAddress("127.0.0.1", 1234);
    RpcException exception = new RpcException("TestRpcException");
    logger.error(EasyMock.eq("Got unchecked and undeclared exception which called by 127.0.0.1. service: " + DemoService.class.getName() + ", method: sayHello, exception: " + RpcException.class.getName() + ": TestRpcException"), EasyMock.eq(exception));
    ExceptionFilter exceptionFilter = new ExceptionFilter(logger);
    RpcInvocation invocation = new RpcInvocation("sayHello", new Class<?>[]{String.class}, new Object[]{"world"});
    Invoker<DemoService> invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class);
    EasyMock.expect(invoker.invoke(EasyMock.eq(invocation))).andThrow(exception);
    
    EasyMock.replay(logger, invoker);
    
    try {
        exceptionFilter.invoke(invoker, invocation);
    } catch (RpcException e) {
        assertEquals("TestRpcException", e.getMessage());
    }
    EasyMock.verify(logger, invoker);
    RpcContext.removeContext();
}
 
Example 4
Project: dubbo2   File: ContextFilterTest.java   View Source Code Vote up 6 votes
@SuppressWarnings("unchecked")
@Test
public void testSetContext() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("$enumlength").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] { Enum.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.expect(invocation.getAttachments()).andReturn(null).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setValue("High");
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    contextFilter.invoke(invoker, invocation);
    assertNull(RpcContext.getContext().getInvoker());
}
 
Example 5
Project: dubbocloud   File: FailSafeClusterInvokerTest.java   View Source Code Vote up 6 votes
@Test()
public void testNoInvoke() {
    dic = EasyMock.createMock(Directory.class);
    
    EasyMock.expect(dic.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(dic.list(invocation)).andReturn(null).anyTimes();
    EasyMock.expect(dic.getInterface()).andReturn(DemoService.class).anyTimes();
    
    invocation.setMethodName("method1");
    EasyMock.replay(dic);
    
    resetInvokerToNoException();
    
    FailsafeClusterInvoker<DemoService> invoker = new FailsafeClusterInvoker<DemoService>(dic);
    LogUtil.start();
    invoker.invoke(invocation);
    assertTrue(LogUtil.findMessage("No provider") > 0);
    LogUtil.stop();
}
 
Example 6
Project: devops-cm-client   File: GetChangeTransportsTest.java   View Source Code Vote up 6 votes
private ClientFactory setupMock() throws Exception {

        ArrayList<CMODataTransport> transports = new ArrayList<>();
        transports.add(new CMODataTransport("L21K900026", false, "Description", "Owner"));
        transports.add(new CMODataTransport("L21K900028", false, "Description", "Owner"));
        transports.add(new CMODataTransport("L21K900029", false, "Description", "Owner"));
        transports.add(new CMODataTransport("L21K90002A", false, "Description", "Owner"));
        transports.add(new CMODataTransport("L21K90002B", false, "Description", "Owner"));
        transports.add(new CMODataTransport("L21K90002C", false, "Description", "Owner"));
        transports.add(new CMODataTransport("L21K90002D", false, "Description", "Owner"));
        transports.add(new CMODataTransport("L21K90002E", true, "Description", "Owner"));

        CMODataClient clientMock = createMock(CMODataClient.class);
        expect(clientMock.getChangeTransports(capture(changeId))).andReturn(transports);

        ClientFactory factoryMock = EasyMock.createMock(ClientFactory.class);
        expect(factoryMock
                .newClient(capture(host),
                        capture(user),
                        capture(password))).andReturn(clientMock);
        clientMock.close(); expectLastCall();

        replay(clientMock, factoryMock);

        return factoryMock;
    }
 
Example 7
Project: EatDubbo   File: FailoverClusterInvokerTest.java   View Source Code Vote up 6 votes
@Test()
public void testNoInvoke() {
    dic = EasyMock.createMock(Directory.class);
    
    EasyMock.expect(dic.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(dic.list(invocation)).andReturn(null).anyTimes();
    EasyMock.expect(dic.getInterface()).andReturn(FailoverClusterInvokerTest.class).anyTimes();
    invocation.setMethodName("method1");
    EasyMock.replay(dic);
    
    invokers.add(invoker1);
    
    
    FailoverClusterInvoker<FailoverClusterInvokerTest> invoker = new FailoverClusterInvoker<FailoverClusterInvokerTest>(dic);
    try {
        invoker.invoke(invocation);
        fail();
    } catch (RpcException expected) {
        assertFalse(expected.getCause() instanceof RpcException);
    }
}
 
Example 8
Project: github-test   File: AbstractClusterInvokerTest.java   View Source Code Vote up 5 votes
@Test
public void testCloseAvailablecheck() {
    LoadBalance lb = EasyMock.createMock(LoadBalance.class);
    EasyMock.expect(lb.select(invokers, url, invocation)).andReturn(invoker1);
    EasyMock.replay(lb);
    initlistsize5();

    Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
    Assert.assertEquals(false, sinvoker.isAvailable());
    Assert.assertEquals(invoker1, sinvoker);

}
 
Example 9
Project: dubbocloud   File: LogTelnetHandlerTest.java   View Source Code Vote up 5 votes
@Test
public void testChangeLogLevel() throws RemotingException {
    mockChannel = EasyMock.createMock(Channel.class);
    EasyMock.replay(mockChannel);
    String result = log.telnet(mockChannel, "error");
    assertTrue(result.contains("\r\nCURRENT LOG LEVEL:ERROR"));
    String result2 = log.telnet(mockChannel, "warn");
    assertTrue(result2.contains("\r\nCURRENT LOG LEVEL:WARN"));
    EasyMock.reset(mockChannel);
}
 
Example 10
Project: dubbox-hystrix   File: CurrentTelnetHandlerTest.java   View Source Code Vote up 5 votes
@Test
public void testMessageError() throws RemotingException {
    mockChannel = EasyMock.createMock(Channel.class);
    EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
    EasyMock.replay(mockChannel);
    String result = count.telnet(mockChannel, "test");
    assertEquals("Unsupported parameter test for pwd.", result);
}
 
Example 11
Project: athena   File: PsnpTest.java   View Source Code Vote up 5 votes
@Before
public void setUp() throws Exception {
    isisHeader = new IsisHeader();
    isisHeader.setIsisPduType(IsisPduType.L1PSNP.value());
    psnp = new Psnp(isisHeader);
    tlvHeader = new TlvHeader();
    channelBuffer = EasyMock.createMock(ChannelBuffer.class);
    isisTlv = new AdjacencyStateTlv(tlvHeader);

}
 
Example 12
Project: dubbox-hystrix   File: AbstractClusterInvokerTest.java   View Source Code Vote up 5 votes
@Test
public void testCloseAvailablecheck(){
    LoadBalance lb = EasyMock.createMock(LoadBalance.class);
    EasyMock.expect(lb.select(invokers, url, invocation)).andReturn(invoker1);
    EasyMock.replay(lb);
    initlistsize5();
    
    Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
    Assert.assertEquals(false,sinvoker.isAvailable());
    Assert.assertEquals(invoker1,sinvoker);
    
}
 
Example 13
Project: athena   File: OspfInterfaceChannelHandlerTest.java   View Source Code Vote up 5 votes
/**
 * Tests channelConnected() method.
 */
@Test(expected = Exception.class)
public void testChannelConnected() throws Exception {
    channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
    channelStateEvent = EasyMock.createMock(ChannelStateEvent.class);
    ospfInterfaceChannelHandler.channelConnected(channelHandlerContext, channelStateEvent);
}
 
Example 14
Project: athena   File: HostNameTlvTest.java   View Source Code Vote up 5 votes
@Before
public void setUp() throws Exception {
    tlvHeader = new TlvHeader();
    tlvHeader.setTlvLength(tlv.length);
    hostNameTlv = new HostNameTlv(tlvHeader);
    channelBuffer = EasyMock.createMock(ChannelBuffer.class);
}
 
Example 15
Project: kafka-0.11.0.0-src-with-comment   File: FileRecordsTest.java   View Source Code Vote up 5 votes
/**
 * see #testTruncateNotCalledIfSizeIsSameAsTargetSize
 */
@Test
public void testTruncateIfSizeIsDifferentToTargetSize() throws IOException {
    FileChannel channelMock = EasyMock.createMock(FileChannel.class);

    EasyMock.expect(channelMock.size()).andReturn(42L).atLeastOnce();
    EasyMock.expect(channelMock.position(42L)).andReturn(null).once();
    EasyMock.expect(channelMock.truncate(23L)).andReturn(null).once();
    EasyMock.replay(channelMock);

    FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false);
    fileRecords.truncateTo(23);

    EasyMock.verify(channelMock);
}
 
Example 16
Project: athena   File: NetconfControllerImplTest.java   View Source Code Vote up 5 votes
public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
    netconfDeviceInfo = deviceInfo;
    if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
        netconfSession = EasyMock.createMock(NetconfSession.class);
        deviceState = true;
    } else {
        throw new NetconfException("Cannot create Connection and Session");
    }
}
 
Example 17
Project: EatDubbo   File: AbstractClusterInvokerTest.java   View Source Code Vote up 4 votes
@SuppressWarnings({ "unchecked" })
@Before
public void setUp() throws Exception {
	invocation.setMethodName("sayHello");
    
    invoker1 = EasyMock.createMock(Invoker.class);
    invoker2 = EasyMock.createMock(Invoker.class);
    invoker3 = EasyMock.createMock(Invoker.class);
    invoker4 = EasyMock.createMock(Invoker.class);
    invoker5 = EasyMock.createMock(Invoker.class);
    mockedInvoker1 = EasyMock.createMock(Invoker.class);
    
    URL turl = URL.valueOf("test://test:11/test");
    
    EasyMock.expect(invoker1.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(invoker1.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker1.getUrl()).andReturn(turl.addParameter("name", "invoker1")).anyTimes();
    
    EasyMock.expect(invoker2.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker2.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker2.getUrl()).andReturn(turl.addParameter("name", "invoker2")).anyTimes();
    
    EasyMock.expect(invoker3.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(invoker3.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker3.getUrl()).andReturn(turl.addParameter("name", "invoker3")).anyTimes();
    
    EasyMock.expect(invoker4.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker4.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker4.getUrl()).andReturn(turl.addParameter("name", "invoker4")).anyTimes();
    
    EasyMock.expect(invoker5.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(invoker5.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker5.getUrl()).andReturn(turl.addParameter("name", "invoker5")).anyTimes();
    
    EasyMock.expect(mockedInvoker1.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(mockedInvoker1.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(mockedInvoker1.getUrl()).andReturn(turl.setProtocol("mock")).anyTimes();
    
    EasyMock.replay(invoker1,invoker2,invoker3,invoker4,invoker5,mockedInvoker1);
    
    invokers.add(invoker1);
    dic = new StaticDirectory<IHelloService>(url, invokers, null);
    cluster = new AbstractClusterInvoker(dic) {
        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance)
                throws RpcException {
            return null;
        }
    };
    
    cluster_nocheck = new AbstractClusterInvoker(dic,url.addParameterIfAbsent(Constants.CLUSTER_AVAILABLE_CHECK_KEY, Boolean.FALSE.toString())) {
        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance)
                throws RpcException {
            return null;
        }
    };
    
}
 
Example 18
Project: github-test   File: LoadBalanceTest.java   View Source Code Vote up 4 votes
/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {

    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("method1").anyTimes();

    invoker1 = EasyMock.createMock(Invoker.class);
    invoker2 = EasyMock.createMock(Invoker.class);
    invoker3 = EasyMock.createMock(Invoker.class);
    invoker4 = EasyMock.createMock(Invoker.class);
    invoker5 = EasyMock.createMock(Invoker.class);

    URL url1 = URL.valueOf("test://127.0.0.1:1/DemoService");
    URL url2 = URL.valueOf("test://127.0.0.1:2/DemoService");
    URL url3 = URL.valueOf("test://127.0.0.1:3/DemoService");
    URL url4 = URL.valueOf("test://127.0.0.1:4/DemoService");
    URL url5 = URL.valueOf("test://127.0.0.1:5/DemoService");

    EasyMock.expect(invoker1.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker1.getInterface()).andReturn(LoadBalanceTest.class).anyTimes();
    EasyMock.expect(invoker1.getUrl()).andReturn(url1).anyTimes();

    EasyMock.expect(invoker2.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker2.getInterface()).andReturn(LoadBalanceTest.class).anyTimes();
    EasyMock.expect(invoker2.getUrl()).andReturn(url2).anyTimes();

    EasyMock.expect(invoker3.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker3.getInterface()).andReturn(LoadBalanceTest.class).anyTimes();
    EasyMock.expect(invoker3.getUrl()).andReturn(url3).anyTimes();

    EasyMock.expect(invoker4.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker4.getInterface()).andReturn(LoadBalanceTest.class).anyTimes();
    EasyMock.expect(invoker4.getUrl()).andReturn(url4).anyTimes();

    EasyMock.expect(invoker5.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker5.getInterface()).andReturn(LoadBalanceTest.class).anyTimes();
    EasyMock.expect(invoker5.getUrl()).andReturn(url5).anyTimes();

    EasyMock.replay(invocation, invoker1, invoker2, invoker3, invoker4, invoker5);

    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    invokers.add(invoker4);
    invokers.add(invoker5);
}
 
Example 19
Project: dubbocloud   File: FailfastClusterInvokerTest.java   View Source Code Vote up 4 votes
/**
 * @throws java.lang.Exception
 */

@Before
public void setUp() throws Exception {
    
    dic = EasyMock.createMock(Directory.class);
    
    EasyMock.expect(dic.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(dic.list(invocation)).andReturn(invokers).anyTimes();
    EasyMock.expect(dic.getInterface()).andReturn(FailfastClusterInvokerTest.class).anyTimes();
    
    invocation.setMethodName("method1");
    EasyMock.replay(dic);
    
    invokers.add(invoker1);
}
 
Example 20
Project: fresco_floodlight   File: MockOFSwitchImpl.java   View Source Code Vote up 4 votes
public MockOFSwitchImpl(MockOFConnection connection) {
    super(connection, OFFactories.getFactory(OFVersion.OF_10),
          EasyMock.createMock(IOFSwitchManager.class), connection.getDatapathId());
    statsMap = new HashMap<OFStatsType, List<OFStatsReply>>();
}