/** * Creates a mock object that implements the given interface. * * @param <T> * the interface or class that the mock object should * implement/extend. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @param toMock * the interface or class that the mock object should * implement/extend. * @return the mock object. * @since 4.0 */ default <T, R> R mock(Class<T> toMock) { return createMock(toMock); }
/** * Creates a mock object that implements the given interface. * * @param <T> * the interface or class that the mock object should * implement/extend. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @param name * the name of the mock object. * @param toMock * the interface or class that the mock object should * implement/extend. * @return the mock object. * @throws IllegalArgumentException * if the name is not a valid Java identifier. * @since 4.0 */ default <T, R> R mock(String name, Class<T> toMock) { return createMock(name, toMock); }
/** * Creates a mock object that implements the given class. Using this method directly in a test class * is not recommended. Only frameworks extending EasyMock should use it. Final users should use * the more convenient {@link EasyMock#partialMockBuilder(Class)} method instead * * @param <T> * the class that the mock object should extend. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @param name * the name of the mock object. * @param toMock * the class that the mock object should extend. * @param constructorArgs * constructor and parameters used to instantiate the mock. If null, no constructor will be called * @param mockedMethods * methods that will be mocked, other methods will behave * normally. If empty, all methods will be mocked * @return the mock object. * @since 4.0 */ default <T, R> R mock(String name, Class<T> toMock, ConstructorArgs constructorArgs, Method... mockedMethods) { return createMock(name, toMock, constructorArgs, mockedMethods); }
public <R> R createMock(String name, IMocksControl control) { Method[] mockedMethodArray = (mockedMethods == null ? new Method[0] : mockedMethods .toArray(new Method[0])); // Create a mock with no default {@code withConstructor} was not called. if (constructor == null) { return control.createMock(name, toMock, null, mockedMethodArray); } // If the constructor is defined, so must be its arguments if (constructorArgs == null) { throw new IllegalStateException("Picked a constructor but didn't pass arguments to it"); } return control.createMock(name, toMock, constructorArgs, mockedMethodArray); }
/** * Creates a mock object that implements the given interface, order checking * is disabled by default. * * @param <T> * the interface that the mock object should implement. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @param toMock * the class of the interface that the mock object should * implement. * @return the mock object. * * @since ${project.version} */ public <T, R> R mock(Class<T> toMock) { return createControl().createMock(toMock); }
/** * Creates a mock object that implements the given interface, order checking * is enabled by default. * * @param <T> * the interface that the mock object should implement. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @param toMock * the class of the interface that the mock object should * implement. * @return the mock object. * * @since ${project.version} */ public <T, R> R strictMock(Class<T> toMock) { return createStrictControl().createMock(toMock); }
/** * Creates a mock object that implements the given interface, order checking * is disabled by default, and the mock object will return {@code 0}, * {@code null} or {@code false} for unexpected invocations. * * @param <T> * the interface that the mock object should implement. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @param toMock * the class of the interface that the mock object should * implement. * @return the mock object. * * @since ${project.version} */ public <T, R> R niceMock(Class<T> toMock) { return createNiceControl().createMock(toMock); }
/** * Creates a mock object that implements the given interface, order checking * is enabled by default. * * @param name * the name of the mock object. * @param toMock * the class of the interface that the mock object should * implement. * @param <T> * the interface that the mock object should implement. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @return the mock object. * @throws IllegalArgumentException * if the name is not a valid Java identifier. * * @since ${project.version} */ public <T, R> R strictMock(String name, Class<T> toMock) { return createStrictControl().createMock(name, toMock); }
/** * Creates a mock object that implements the given interface, order checking * is disabled by default. * * @param name * the name of the mock object. * @param toMock * the class of the interface that the mock object should * implement. * * @param <T> * the interface that the mock object should implement. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @return the mock object. * @throws IllegalArgumentException * if the name is not a valid Java identifier. * * @since ${project.version} */ public <T, R> R mock(String name, Class<T> toMock) { return createControl().createMock(name, toMock); }
/** * Creates a mock object of the requested type that implements the given interface or extends * the given class * * @param type * the type of the mock to be created. * @param toMock * the class of the interface that the mock object should * implement. * @param <T> * the interface that the mock object should implement. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @return the mock object. * @throws IllegalArgumentException * if the name is not a valid Java identifier. * * @since ${project.version} */ public <T, R> R mock(MockType type, Class<T> toMock) { return createControl(type).createMock(toMock); }
/** * Creates a mock object that implements the given interface, order checking * is disabled by default, and the mock object will return {@code 0}, * {@code null} or {@code false} for unexpected invocations. * * @param name * the name of the mock object. * @param toMock * the class of the interface that the mock object should * implement. * * @param <T> * the interface that the mock object should implement. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @return the mock object. * @throws IllegalArgumentException * if the name is not a valid Java identifier. * * @since ${project.version} */ public <T, R> R niceMock(String name, Class<T> toMock) { return createNiceControl().createMock(name, toMock); }
/** * Creates a mock object of the requested type that implements the given interface or extends * the given class * * @param name * the name of the mock object. * @param type * the type of the mock to be created. * @param toMock * the class of the interface that the mock object should * implement. * @param <T> * the interface that the mock object should implement. * @param <R> * the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the * caller, we return a different type * @return the mock object. * @throws IllegalArgumentException * if the name is not a valid Java identifier. * * @since ${project.version} */ public <T, R> R mock(String name, MockType type, Class<T> toMock) { return createControl(type).createMock(name, toMock); }
backingManager = ctrl.createMock(KeyColumnValueStoreManager.class); lockerProvider = ctrl.createMock(LockerProvider.class); globalConfig = GraphDatabaseConfiguration.buildGraphConfiguration(); localConfig = GraphDatabaseConfiguration.buildGraphConfiguration(); inconsistentTx = ctrl.createMock(StoreTransaction.class); consistentTx = ctrl.createMock(StoreTransaction.class); expect(backingManager.beginTransaction(capture(txConfigCapture))).andReturn(inconsistentTx); expect(backingManager.beginTransaction(capture(txConfigCapture))).andReturn(consistentTx); backingLocker = ctrl.createMock(Locker.class); backingStore = ctrl.createMock(KeyColumnValueStore.class); expect(backingManager.openDatabase(STORE_NAME)).andReturn(backingStore); expect(backingStore.getName()).andReturn(STORE_NAME);
backingManager = ctrl.createMock(KeyColumnValueStoreManager.class); LockerProvider lockerProvider = ctrl.createMock(LockerProvider.class); ModifiableConfiguration globalConfig = GraphDatabaseConfiguration.buildGraphConfiguration(); ModifiableConfiguration localConfig = GraphDatabaseConfiguration.buildGraphConfiguration(); inconsistentTx = ctrl.createMock(StoreTransaction.class); consistentTx = ctrl.createMock(StoreTransaction.class); expect(backingManager.beginTransaction(capture(txConfigCapture))).andReturn(inconsistentTx); expect(backingManager.beginTransaction(capture(txConfigCapture))).andReturn(consistentTx); backingLocker = ctrl.createMock(Locker.class); backingStore = ctrl.createMock(KeyColumnValueStore.class); expect(backingManager.openDatabase(STORE_NAME)).andReturn(backingStore); expect(backingStore.getName()).andReturn(STORE_NAME);
Injector injector = ctrl.createMock(Injector.class); Map<String, Key<? extends Filter>[]> chainMap = new LinkedHashMap<String, Key<? extends Filter>[]>(); chainMap.put(chainThree, new Key[]{key3a, key3b}); PatternMatcher patternMatcher = ctrl.createMock(PatternMatcher.class); ServletRequest request = ctrl.createMock(HttpServletRequest.class); ServletResponse response = ctrl.createMock(HttpServletResponse.class); FilterChain originalChain = ctrl.createMock(FilterChain.class); expect(patternMatcher.matches(chainTwo, "/mychain")).andReturn(true); Filter filter2a = ctrl.createMock(Filter.class); Filter filter2b = ctrl.createMock(Filter.class);
@Test public void testPropertySetting() throws Exception { IMocksControl control = createControl(); TypeEncounter<SomeInjectableBean> encounter = control.createMock(TypeEncounter.class); Provider<Injector> injectorProvider = control.createMock(Provider.class); Injector injector = control.createMock(Injector.class); expect(encounter.getProvider(Injector.class)).andReturn(injectorProvider); expect(injectorProvider.get()).andReturn(injector).anyTimes(); Capture<MembersInjector<SomeInjectableBean>> capture = new Capture<MembersInjector<SomeInjectableBean>>(); encounter.register(and(anyObject(MembersInjector.class), capture(capture))); SecurityManager securityManager = control.createMock(SecurityManager.class); String property = "myPropertyValue"; expect(injector.getInstance(Key.get(SecurityManager.class))).andReturn(securityManager); expect(injector.getInstance(Key.get(String.class, Names.named("shiro.myProperty")))).andReturn(property); expect(injector.getInstance(Key.get(String.class, Names.named("shiro.unavailableProperty")))) .andThrow(new ConfigurationException(Collections.singleton(new Message("Not Available!")))); expect((Map)injector.getInstance(BeanTypeListener.MAP_KEY)).andReturn(Collections.EMPTY_MAP).anyTimes(); control.replay(); BeanTypeListener underTest = new BeanTypeListener(); underTest.hear(TypeLiteral.get(SomeInjectableBean.class), encounter); SomeInjectableBean bean = new SomeInjectableBean(); capture.getValue().injectMembers(bean); assertSame(securityManager, bean.securityManager); assertSame(property, bean.myProperty); assertNull(bean.unavailableProperty); control.verify(); }
@Test public void testDoFilter() throws Exception { IMocksControl ctrl = createStrictControl(); FilterChain originalChain = ctrl.createMock(FilterChain.class); Filter filter1 = ctrl.createMock("filter1", Filter.class); Filter filter2 = ctrl.createMock("filter2", Filter.class); ServletRequest request = ctrl.createMock(ServletRequest.class); ServletResponse response = ctrl.createMock(ServletResponse.class); Capture<FilterChain> fc1 = new Capture<FilterChain>(); Capture<FilterChain> fc2 = new Capture<FilterChain>(); filter1.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc1))); filter2.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc2))); originalChain.doFilter(request, response); ctrl.replay(); SimpleFilterChain underTest = new SimpleFilterChain(originalChain, Arrays.asList(filter1, filter2).iterator()); // all we actually care about is that, if we keep calling the filter chain, everything is called in the right // order - we don't care what fc actually contains underTest.doFilter(request, response); fc1.getValue().doFilter(request, response); fc2.getValue().doFilter(request, response); ctrl.verify(); } }
public void testSimple() throws Exception { IMocksControl testControl = createControl(); TestFilterChain testFilterChain = new TestFilterChain(); HttpServletRequest req = testControl.createMock(HttpServletRequest.class); HttpServletResponse res = testControl.createMock(HttpServletResponse.class); expect(req.getMethod()).andReturn("GET").anyTimes(); expect(req.getRequestURI()).andReturn("/bar/foo").anyTimes(); expect(req.getServletPath()).andReturn("/bar/foo").anyTimes(); expect(req.getContextPath()).andReturn("").anyTimes(); testControl.replay(); guiceFilter.doFilter(req, res, testFilterChain); assertFalse(testFilterChain.isTriggered()); assertFalse(fooServlet.isTriggered()); assertTrue(barServlet.isTriggered()); testControl.verify(); }
private void runRequestForPath(FilterPipeline pipeline, String value, boolean matches) throws IOException, ServletException { assertEquals(0, doFilters); //create ourselves a mock request with test URI HttpServletRequest requestMock = control.createMock(HttpServletRequest.class); expect(requestMock.getRequestURI()).andReturn(value).anyTimes(); expect(requestMock.getContextPath()).andReturn("").anyTimes(); // dispatch request FilterChain filterChain = control.createMock(FilterChain.class); filterChain.doFilter(requestMock, null); control.replay(); pipeline.dispatch(requestMock, null, filterChain); control.verify(); control.reset(); if (matches) { assertEquals("filter was not run", 1, doFilters); doFilters = 0; } else { assertEquals("filter was run", 0, doFilters); } }
private void runTest( final String requestURI, final String servletPath, final String contextPath, final boolean filterResult, final boolean fooResult, final boolean barResult) throws Exception { IMocksControl testControl = createControl(); barServlet.clear(); fooServlet.clear(); TestFilterChain testFilterChain = new TestFilterChain(); HttpServletRequest req = testControl.createMock(HttpServletRequest.class); HttpServletResponse res = testControl.createMock(HttpServletResponse.class); expect(req.getMethod()).andReturn("GET").anyTimes(); expect(req.getRequestURI()).andReturn(requestURI).anyTimes(); expect(req.getServletPath()).andReturn(servletPath).anyTimes(); expect(req.getContextPath()).andReturn(contextPath).anyTimes(); testControl.replay(); guiceFilter.doFilter(req, res, testFilterChain); assertEquals(filterResult, testFilterChain.isTriggered()); assertEquals(fooResult, fooServlet.isTriggered()); assertEquals(barResult, barServlet.isTriggered()); testControl.verify(); }