/** Tests Service.callMethod(). */ public void testCallMethod() throws Exception { FooRequest fooRequest = FooRequest.newBuilder().build(); BarRequest barRequest = BarRequest.newBuilder().build(); MockCallback<Message> fooCallback = new MockCallback<Message>(); MockCallback<Message> barCallback = new MockCallback<Message>(); TestService mockService = control.createMock(TestService.class); mockService.foo(EasyMock.same(mockController), EasyMock.same(fooRequest), this.<FooResponse>wrapsCallback(fooCallback)); mockService.bar(EasyMock.same(mockController), EasyMock.same(barRequest), this.<BarResponse>wrapsCallback(barCallback)); control.replay(); mockService.callMethod(fooDescriptor, mockController, fooRequest, fooCallback); mockService.callMethod(barDescriptor, mockController, barRequest, barCallback); control.verify(); }
public void testNewReflectiveBlockingService() throws ServiceException { ServiceWithNoOuter.BlockingInterface impl = control.createMock(ServiceWithNoOuter.BlockingInterface.class); RpcController controller = control.createMock(RpcController.class); BlockingService service = ServiceWithNoOuter.newReflectiveBlockingService(impl); MethodDescriptor fooMethod = ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance(); TestAllTypes expectedResponse = TestAllTypes.getDefaultInstance(); EasyMock.expect(impl.foo(EasyMock.same(controller), EasyMock.same(request))) .andReturn(expectedResponse); control.replay(); Message response = service.callBlockingMethod(fooMethod, controller, request); assertEquals(expectedResponse, response); control.verify(); }
public void testLaziness() throws InvalidProtocolBufferException { LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() .setNum(2) .build(); LazyMessageLite outer = LazyMessageLite.newBuilder() .setNum(1) .setInner(inner) .setOneofInner(inner) .build(); ByteString bytes = outer.toByteString(); // The parser for inner / oneofInner message shouldn't be used if // getInner / getOneofInner is not called. LazyInnerMessageLite.PARSER = EasyMock.createStrictMock(Parser.class); EasyMock.replay(LazyInnerMessageLite.PARSER); LazyMessageLite deserialized = LazyMessageLite.parseFrom(bytes); assertEquals(1, deserialized.getNum()); assertEquals(421, deserialized.getNumWithDefault()); EasyMock.verify(LazyInnerMessageLite.PARSER); }
@Test public void testSetCharacterEncoding_allowsExceptionToPassBack() throws UnsupportedEncodingException { final UnsupportedEncodingException expected = new UnsupportedEncodingException(); mockSubset.setCharacterEncoding(EasyMock.eq(VALUE)); EasyMock.expectLastCall().andThrow(expected); EasyMock.replay(mockSubset); try { adapter.setCharacterEncoding(VALUE); fail("Should pass exception through"); } catch (UnsupportedEncodingException actual) { assertThat(actual, sameInstance(expected)); } EasyMock.verify(mockSubset); }
public void testRunCallsQueuedRunnables() throws Exception { RunnableQueue q = EasyMock.createMock(RunnableQueue.class); Runnable task1 = EasyMock.createMock(Runnable.class); Runnable task2 = EasyMock.createMock(Runnable.class); EasyMock.expect(q.remove()).andReturn(task1); task1.run(); EasyMock.expectLastCall(); EasyMock.expect(q.remove()).andReturn(task2); task2.run(); EasyMock.expectLastCall(); EasyMock.expect(q.remove()).andThrow(new InterruptedException()); EasyMock.replay(q, task1, task2); QueueExtractor extractor = new QueueExtractor(q); extractor.run(); EasyMock.verify(q, task1, task2); }
public void testRun() throws Exception { RunnableQueue q = EasyMock.createMock(RunnableQueue.class); Runnable task1 = EasyMock.createMock(Runnable.class); Runnable task2 = EasyMock.createMock(Runnable.class); EasyMock.expect(q.remove()).andReturn(task1); task1.run(); EasyMock.expectLastCall(); EasyMock.expect(q.remove()).andReturn(task2); task2.run(); EasyMock.expectLastCall(); EasyMock.expect(q.remove()).andThrow(new InterruptedException()); EasyMock.replay(q, task1, task2); WorkerThread worker = new WorkerThread(q, "test thread"); worker.run(); EasyMock.verify(q, task1, task2); }
public void testGetEntryInvalidUrl() throws Exception { // Setup URL badUrl = new URL("http://badUrl"); String errorMsg = "invalid URL"; EasyMock.expect(mockService.getEntry(badUrl, FeedServerEntry.class)).andThrow( new IOException(errorMsg)); EasyMock.replay(mockService); // Perform Test try { @SuppressWarnings("unused") Map<String, Object> fetchedMap = feedServerClient.getEntry(badUrl); } catch (FeedServerClientException e) { assertTrue(e.getCause().getMessage().equals(errorMsg)); EasyMock.verify(mockService); return; } fail("Did not get FeedServerClientException"); }
public void testCheckAccessSuccessOnRetrieveEntry() throws Exception { String[][] principalx = new String[][] { new String[]{USER1_EMAIL}, new String[]{USER1_EMAIL, USER2_EMAIL}}; for (String[] principals: principalx) { prepare(new AdapterCall() { @Override public void invoke(AbstractManagedCollectionAdapter adapter) throws FeedServerAdapterException { EasyMock.expect(adapter.retrieveEntry(isA(RequestContext.class), isA(Object.class))) .andReturn(null); } }); ResourceConnectionInfoWrapper aclWrapper = new ResourceConnectionInfoWrapper(targetAdapterMock, getWrapperConfig( AuthorizedEntity.OPERATION_RETRIEVE, principals)); aclWrapper.retrieveEntry(requestMock, "123"); finish(); } }
protected void prepare(AdapterCall adapterCall) throws Exception { targetAdapterMock = EasyMock.createMock(AbstractManagedCollectionAdapter.class); requestMock = EasyMock.createMock(RequestContext.class); userInfoMock = EasyMock.createMock(UserInfo.class); entryMock = EasyMock.createMock(Entry.class); EasyMock.expect(targetAdapterMock.getAbdera()).andReturn(null); EasyMock.expect(targetAdapterMock.getConfiguration()).andReturn(null); EasyMock.expect(requestMock.getAttribute( RequestContext.Scope.REQUEST, AbstractManagedCollectionAdapter.USER_INFO)) .andReturn(userInfoMock); EasyMock.expect(userInfoMock.getEmail()).andReturn(USER1_EMAIL); adapterCall.invoke(targetAdapterMock); EasyMock.replay(targetAdapterMock); EasyMock.replay(requestMock); EasyMock.replay(userInfoMock); EasyMock.replay(entryMock); }
public void testGetEntryInvalidUrl() throws Exception { // Setup URL badUrl = new URL("http://badUrl"); String errorMsg = "invalid URL"; EasyMock.expect(mockService.getEntry(badUrl, FeedServerEntry.class)).andThrow( new IOException(errorMsg)); EasyMock.replay(mockService); // Perform Test try { feedServerClient.getEntity(badUrl); } catch (FeedServerClientException e) { assertTrue(e.getCause().getMessage().equals(errorMsg)); EasyMock.verify(mockService); return; } fail("Did not get FeedServerClientException"); }
/** Tests generated stubs. */ public void testStub() throws Exception { FooRequest fooRequest = FooRequest.newBuilder().build(); BarRequest barRequest = BarRequest.newBuilder().build(); MockCallback<FooResponse> fooCallback = new MockCallback<FooResponse>(); MockCallback<BarResponse> barCallback = new MockCallback<BarResponse>(); RpcChannel mockChannel = control.createMock(RpcChannel.class); TestService stub = TestService.newStub(mockChannel); mockChannel.callMethod( EasyMock.same(fooDescriptor), EasyMock.same(mockController), EasyMock.same(fooRequest), EasyMock.same(FooResponse.getDefaultInstance()), this.<Message>wrapsCallback(fooCallback)); mockChannel.callMethod( EasyMock.same(barDescriptor), EasyMock.same(mockController), EasyMock.same(barRequest), EasyMock.same(BarResponse.getDefaultInstance()), this.<Message>wrapsCallback(barCallback)); control.replay(); stub.foo(mockController, fooRequest, fooCallback); stub.bar(mockController, barRequest, barCallback); control.verify(); }
/** Tests generated blocking stubs. */ public void testBlockingStub() throws Exception { FooRequest fooRequest = FooRequest.newBuilder().build(); BarRequest barRequest = BarRequest.newBuilder().build(); BlockingRpcChannel mockChannel = control.createMock(BlockingRpcChannel.class); TestService.BlockingInterface stub = TestService.newBlockingStub(mockChannel); FooResponse fooResponse = FooResponse.newBuilder().build(); BarResponse barResponse = BarResponse.newBuilder().build(); EasyMock.expect(mockChannel.callBlockingMethod( EasyMock.same(fooDescriptor), EasyMock.same(mockController), EasyMock.same(fooRequest), EasyMock.same(FooResponse.getDefaultInstance()))).andReturn(fooResponse); EasyMock.expect(mockChannel.callBlockingMethod( EasyMock.same(barDescriptor), EasyMock.same(mockController), EasyMock.same(barRequest), EasyMock.same(BarResponse.getDefaultInstance()))).andReturn(barResponse); control.replay(); assertSame(fooResponse, stub.foo(mockController, fooRequest)); assertSame(barResponse, stub.bar(mockController, barRequest)); control.verify(); }
public void testNewReflectiveService() { ServiceWithNoOuter.Interface impl = control.createMock(ServiceWithNoOuter.Interface.class); RpcController controller = control.createMock(RpcController.class); Service service = ServiceWithNoOuter.newReflectiveService(impl); MethodDescriptor fooMethod = ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance(); RpcCallback<Message> callback = new RpcCallback<Message>() { public void run(Message parameter) { // No reason this should be run. fail(); } }; RpcCallback<TestAllTypes> specializedCallback = RpcUtil.specializeCallback(callback); impl.foo(EasyMock.same(controller), EasyMock.same(request), EasyMock.same(specializedCallback)); EasyMock.expectLastCall(); control.replay(); service.callMethod(fooMethod, controller, request, callback); control.verify(); }
public void testNewReflectiveBlockingService() throws ServiceException { ServiceWithNoOuter.BlockingInterface impl = control.createMock(ServiceWithNoOuter.BlockingInterface.class); RpcController controller = control.createMock(RpcController.class); BlockingService service = ServiceWithNoOuter.newReflectiveBlockingService(impl); MethodDescriptor fooMethod = ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance(); RpcCallback<Message> callback = new RpcCallback<Message>() { public void run(Message parameter) { // No reason this should be run. fail(); } }; TestAllTypes expectedResponse = TestAllTypes.getDefaultInstance(); EasyMock.expect(impl.foo(EasyMock.same(controller), EasyMock.same(request))) .andReturn(expectedResponse); control.replay(); Message response = service.callBlockingMethod(fooMethod, controller, request); assertEquals(expectedResponse, response); control.verify(); }
@Test public void testRest() throws Exception { MockTest mockTest = new MockTest(); easyMockModule.createAndInjectMocksIntoTest(mockTest); EasyMock.expect(mockTest.testMock.isEmpty()).andReturn(Boolean.TRUE); easyMockModule.reset(); //without the reset the call would fail. easyMockModule.verify(); }
/** * Test method for {@link org.unitils.core.junit.BeforeTestMethodStatement#evaluate()}. */ @Test(expected = NullPointerException.class) public void testEvaluateExceptionOnListener() throws Throwable { TestClass2 testObject = new TestClass2(); Method testMethod = TestClass2.class.getMethod("test1"); listener.beforeTestMethod(testObject, testMethod); EasyMock.expectLastCall().andThrow(new NullPointerException()); EasyMockUnitils.replay(); new BeforeTestMethodStatement(listener, statement, testMethod, testObject).evaluate(); }
@Test(expected = NullPointerException.class) public void testEvaluateExceptionOnStatement() throws Throwable { TestClass2 testObject = new TestClass2(); Method testMethod = TestClass2.class.getMethod("test1"); listener.beforeTestMethod(testObject, testMethod); statement.evaluate(); EasyMock.expectLastCall().andThrow(new NullPointerException()); EasyMockUnitils.replay(); new BeforeTestMethodStatement(listener, statement, testMethod, testObject).evaluate(); }
@Test(expected = NullPointerException.class) public void testEvaluateExceptionOnListener() throws Throwable { TestClass2 testObject = new TestClass2(); Method testMethod = TestClass2.class.getMethod("test1"); listener.beforeTestSetUp(testObject, testMethod); EasyMock.expectLastCall().andThrow(new NullPointerException()); EasyMockUnitils.replay(); new BeforeTestSetUpStatement(testObject, testMethod, listener, statement).evaluate(); }
@Test(expected = NullPointerException.class) public void testEvaluateExceptionOnStatement() throws Throwable { TestClass2 testObject = new TestClass2(); Method testMethod = TestClass2.class.getMethod("test1"); listener.beforeTestSetUp(testObject, testMethod); statement.evaluate(); EasyMock.expectLastCall().andThrow(new NullPointerException()); EasyMockUnitils.replay(); new BeforeTestSetUpStatement(testObject, testMethod, listener, statement).evaluate(); }
/** * Test method for {@link org.unitils.core.junit.AfterTestTearDownStatement#evaluate()}. */ @Test(expected = NullPointerException.class) public void testEvaluateStatementException() throws Throwable { TestClass2 testObject = new TestClass2(); Method testMethod = TestClass2.class.getMethod("test1"); statement.evaluate(); EasyMock.expectLastCall().andThrow(new NullPointerException()); listener.afterTestTearDown(testObject, testMethod); EasyMockUnitils.replay(); new AfterTestTearDownStatement(listener, statement, testObject, testMethod).evaluate(); }
/** * Test method for {@link org.unitils.core.junit.AfterTestTearDownStatement#evaluate()}. */ @Test(expected = NullPointerException.class) public void testEvaluateAfterTestTearDowntException() throws Throwable { TestClass2 testObject = new TestClass2(); Method testMethod = TestClass2.class.getMethod("test1"); statement.evaluate(); listener.afterTestTearDown(testObject, testMethod); EasyMock.expectLastCall().andThrow(new NullPointerException()); EasyMockUnitils.replay(); new AfterTestTearDownStatement(listener, statement, testObject, testMethod).evaluate(); }
public DalConnection mockDalConnection(){ DalConnection conn = EasyMock.createMock(DalConnection.class); DbMeta meta = EasyMock.createMock(DbMeta.class); EasyMock.expect(meta.getDataBaseKeyName()).andReturn(dbName).times(1); EasyMock.expect(meta.getDatabaseCategory()).andReturn(DatabaseCategory.MySql).times(1); EasyMock.expect(conn.getMeta()).andReturn(meta).times(3); EasyMock.replay(meta, conn); return conn; }
@Test public void testHandleRequest() throws Exception { FilterChain chain = EasyMock.createMock(FilterChain.class); HttpRequest request = EasyMock.createMock(HttpRequest.class); HttpResponse response = EasyMock.createMock(HttpResponse.class); HttpRequestHandlerAdapter adapter = new HttpRequestHandlerAdapter(chain); chain.doFilter(EasyMock.isA(HttpServletRequestAdapter.class), EasyMock.isA(HttpServletResponseAdapter.class)); EasyMock.expectLastCall(); EasyMock.expect(request.getConnectionInformation()).andReturn(null); EasyMock.replay(chain, request, response); adapter.handleRequest(request, response); EasyMock.verify(chain, request, response); }
@Before public void setUp() { mockSubset = EasyMock.createStrictMock(HttpRequest.class); EasyMock.expect(mockSubset.getConnectionInformation()).andReturn(null); EasyMock.replay(mockSubset); adapter = new HttpServletRequestAdapter(mockSubset); EasyMock.verify(mockSubset); // We have to do a replay and reset here because HttpServletRequestAdapter // calls getConnectionInformation() on the HttpRequest in its constructor. EasyMock.reset(mockSubset); }
@Test public void testGetHeader() { EasyMock.expect(mockSubset.getHeader(NAME)).andReturn(VALUE); EasyMock.replay(mockSubset); assertThat(adapter.getHeader(NAME), is(VALUE)); EasyMock.verify(mockSubset); }
@Test public void testGetHeaders() { final Enumeration<String> mockEnumeration = createMockEnumeration(); EasyMock.expect(mockSubset.getHeaders(NAME)).andReturn(mockEnumeration); EasyMock.replay(mockSubset, mockEnumeration); assertThat(adapter.getHeaders(NAME), sameInstance(mockEnumeration)); EasyMock.verify(mockSubset, mockEnumeration); }
@Test public void testGetHeaderNames() { final Enumeration<String> mockEnumeration = createMockEnumeration(); EasyMock.expect(mockSubset.getHeaderNames()).andReturn(mockEnumeration); EasyMock.replay(mockSubset, mockEnumeration); assertThat(adapter.getHeaderNames(), sameInstance(mockEnumeration)); EasyMock.verify(mockSubset, mockEnumeration); }
@Test public void testGetMethod() { EasyMock.expect(mockSubset.getMethod()).andReturn(NAME); EasyMock.replay(mockSubset); assertThat(adapter.getMethod(), is(NAME)); EasyMock.verify(mockSubset); }