public MocksControl.MockType getMockType() { final MocksControl control = invocationHandler.getControl(); if (WhiteboxImpl.getFieldsOfType(control, MocksControl.MockType.class).isEmpty()) { // EasyMock is of version 3.2+ final MockType mockType = WhiteboxImpl.getInternalState(control, MockType.class); switch (mockType) { case DEFAULT: return MocksControl.MockType.DEFAULT; case NICE: return MocksControl.MockType.NICE; case STRICT: return MocksControl.MockType.STRICT; default: throw new IllegalStateException("PowerMock doesn't seem to work with the used EasyMock version. Please report to the PowerMock mailing list"); } } else { return WhiteboxImpl.getInternalState(control, MocksControl.MockType.class); } }
private static <T> T doCreateMock(Class<T> type, ConstructorArgs constructorArgs, final IMocksControl control, Method... methods) { T mock; MocksControl mocksControl = ((MocksControl) control); if (constructorArgs == null) { if (methods == null) { mock = mocksControl.createMock(type); } else { mock = mocksControl.createMock(type, methods); } } else { if (methods == null) { mock = mocksControl.createMock(type, constructorArgs); } else { mock = mocksControl.createMock(type, constructorArgs, methods); } } return mock; }
/** * Initializes the module */ @Override @SuppressWarnings("unchecked") public void init(Properties configuration) { mocksControls = new ArrayList<MocksControl>(); defaultAnnotationPropertyValues = getAnnotationPropertyDefaults(EasyMockModule.class, configuration, RegularMock.class, Mock.class); autoVerifyAfterTestEnabled = PropertyUtils.getBoolean(PROPKEY_AUTO_VERIFY_AFTER_TEST_ENABLED, configuration); }
@Override public void setUp() throws Exception { super.setUp(); myFixture.configureByFile("/refactoring/pullup/pyPullUpInfoModel.py"); final PyClass childClass = getClassByName("ChildWithDependencies"); final PyClass parentClass = getClassByName("SomeParent"); mySut = new PyPullUpInfoModel(childClass, new MocksControl(MocksControl.MockType.NICE).createMock(PyPullUpView.class)); mySut.setSuperClass(parentClass); myMemberInfos = new PyMemberInfoStorage(childClass).getClassMemberInfos(childClass); }
/** * Initializes test. Always run it <strong>first</strong> if overwrite. */ @Override public void setUp() throws Exception { super.setUp(); myFixture.copyDirectoryToProject("refactoring/" + myRefactoringName + "/presenter/", ""); myFixture.configureFromTempProjectFile("file.py"); myMocksControl = new MocksControl(MocksControl.MockType.NICE); myView = myMocksControl.createMock(myViewClass); configureMockCapture(); }
@Test public void testMockDateWithEasyMock() throws Exception { Date someDate = new Date(); MocksControl c = (MocksControl) org.easymock.EasyMock.createControl(); Date date = c.createMock(Date.class); EasyMock.expect(date.after(someDate)).andReturn(false); PowerMock.replay(date); date.after(someDate); PowerMock.verify(date); }
@Test(expected = IllegalStateException.class) public void testMockDateWithEasyMockFails() { Date someDate = new Date(); MocksControl c = (MocksControl) org.easymock.EasyMock.createControl(); Date date = c.createMock(Date.class, new Method[0]); EasyMock.expect(date.after(someDate)).andReturn(false); Assert.fail("EasyMock with no methods mocked should not be possible to mock"); }
public Object invoke(Class<?> type, Object[] args, Class<?>[] sig) throws Exception { Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig); if (constructor.isVarArgs()) { /* * Get the last argument because this contains the actual varargs * arguments. */ int length = constructor.getParameterTypes().length; args = (Object[]) args[length-1]; } try { final MocksControl.MockType mockType = ((EasyMockMethodInvocationControl<?>) MockRepository.getInstanceMethodInvocationControl(substitute)) .getMockType(); Object result = substitute.performSubstitutionLogic(args); if (result == null) { if (mockType == MocksControl.MockType.NICE) { result = EasyMock.createNiceMock(subsitutionType); } else { throw new IllegalStateException("Must replay class " + type.getName() + " to get configured expectation."); } } return result; } catch (AssertionError e) { NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } // Won't happen return null; }
private void clearStateFromOtherClassLoaders() { for(ClassLoader cl : classloadersToClear()) { try { final Class<?> lastControlClassByCL = Class.forName(LastControl.class.getName(), false, cl); final Class<?> mocksControlClassByCL = Class.forName(MocksControl.class.getName(), false, cl); final Method reportLastControl = lastControlClassByCL.getMethod("reportLastControl", mocksControlClassByCL); reportLastControl.invoke(lastControlClassByCL, new Object[]{null}); } catch (Exception e) { // Should never happen throw new RuntimeException("Failed to clean up state", e); } } }
@Before public void setup() { final TestEntityFactory entityFactory = new TestEntityFactory(); product = entityFactory.createProduct(); final BuildConfiguration buildConfig = new BuildConfiguration(); product.setBuildConfiguration(buildConfig); final QATestConfig qaConfig = new QATestConfig(); product.setQaTestConfig(qaConfig); build = entityFactory.createBuild(); mocksControl = new MocksControl(MockType.DEFAULT); mockServletRequest = mocksControl.createMock(HttpServletRequest.class); EasyMock.expect(mockServletRequest.getContextPath()).andReturn("api").anyTimes(); EasyMock.expect(mockServletRequest.getServletPath()).andReturn("/v1").anyTimes(); }
@Before public void setup() { mocksControl = new MocksControl(MockType.DEFAULT); this.mockS3Client = mocksControl.createMock(S3Client.class); buildDAO.setS3Client(mockS3Client); product = productDAO.find(1L, TestUtils.TEST_USER); final Date creationTime = new GregorianCalendar(2014, 1, 4, 10, 30, 1).getTime(); build = new Build(creationTime, product); }
public void testServiceBeanInjection() throws Exception { ServiceBean bean = new ServiceBean(); final MyService bean1 = new MyService() { public Object getId() { return this; } }; final Serializable bean2 = new Serializable() { public String toString() { return "bean2"; } }; BundleContext context = new MockBundleContext() { public Object getService(org.osgi.framework.ServiceReference reference) { String clazz = ((String[]) reference.getProperty(Constants.OBJECTCLASS))[0]; if (clazz == null) return null; else if (clazz.equals(MyService.class.getName())) { return bean1; } else if (clazz.equals(Serializable.class.getName())) { return bean2; } return null; } }; ServiceReferenceInjectionBeanPostProcessor p = new ServiceReferenceInjectionBeanPostProcessor(); p.setBundleContext(context); p.setBeanClassLoader(getClass().getClassLoader()); BeanFactory factory = EasyMock.createMock(BeanFactory.class); MocksControl factoryControl = MocksControl.getControl(factory); EasyMock.expect(factory.containsBean("&myBean")).andReturn(true); EasyMock.replay(factory); p.setBeanFactory(factory); p.postProcessAfterInitialization(bean, "myBean"); assertSame(bean1.getId(), bean.getServiceBean().getId()); assertSame(bean2.toString(), bean.getSerializableBean().toString()); factoryControl.verify(); }
/** * Replays all mock controls. */ public void replay() { for (MocksControl mocksControl : mocksControls) { mocksControl.replay(); } }
/** * Resets all mock controls. */ public void reset() { for (MocksControl mocksControl : mocksControls) { mocksControl.reset(); } }
public IMocksControl createMockControl(Class<?> type) { return new MocksControl(mockType); }
@Before public void setUp() throws Exception { mocksControl = new MocksControl(MockType.DEFAULT); mockIdRestClient = mocksControl.createMock(IdServiceRestClient.class); cachedSctidFactory = new CachedSctidFactory(1, "123", "456", mockIdRestClient,1,20); }
/** * This method makes sure {@link org.easymock.internal.MocksControl#verify} method is called for every mock mock object * that was injected to a field annotated with {@link Mock}, or directly created by calling * {@link #createRegularMock(Class, InvocationOrder, Calls)} or * {@link #createMock(Class, InvocationOrder, Calls, Order, Dates, Defaults)}. * <p/> * If there are mocks that weren't already switched to the replay state using {@link MocksControl#replay()}} or by * calling {@link org.unitils.easymock.EasyMockUnitils#replay()}, this method is called first. */ public void verify() { for (MocksControl mocksControl : mocksControls) { if (!(mocksControl.getState() instanceof ReplayState)) { mocksControl.replay(); } mocksControl.verify(); } }