use of org.easymock.IMocksControl in project guice by google.
the class ContextPathTest method testSimple.
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();
}
use of org.easymock.IMocksControl in project cobar by alibaba.
the class JdbcTest method test.
public void test() {
IMocksControl control = EasyMock.createControl();
DBUtility mockDBUtility = control.createMock(DBUtility.class);
Connection mockConnection = control.createMock(Connection.class);
Statement mockStatement = control.createMock(Statement.class);
ResultSet mockResultSet = control.createMock(ResultSet.class);
try {
mockDBUtility.getConnection();
EasyMock.expectLastCall().andStubReturn(mockConnection);
mockConnection.createStatement();
EasyMock.expectLastCall().andStubReturn(mockStatement);
mockStatement.executeQuery(SQLEquals.sqlEquals("SELECT * FROM sales_order_table"));
EasyMock.expectLastCall().andStubReturn(mockResultSet);
mockResultSet.next();
EasyMock.expectLastCall().andReturn(true).times(3);
EasyMock.expectLastCall().andReturn(false).times(1);
mockResultSet.getString(1);
EasyMock.expectLastCall().andReturn("DEMO_ORDER_001").times(1);
EasyMock.expectLastCall().andReturn("DEMO_ORDER_002").times(1);
EasyMock.expectLastCall().andReturn("DEMO_ORDER_003").times(1);
mockResultSet.getString(2);
EasyMock.expectLastCall().andReturn("Asia Pacific").times(1);
EasyMock.expectLastCall().andReturn("Europe").times(1);
EasyMock.expectLastCall().andReturn("America").times(1);
mockResultSet.getDouble(3);
EasyMock.expectLastCall().andReturn(350.0).times(1);
EasyMock.expectLastCall().andReturn(1350.0).times(1);
EasyMock.expectLastCall().andReturn(5350.0).times(1);
control.replay();
Connection conn = mockDBUtility.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from sales_order_table");
int i = 0;
String[] priceLevels = { "Level_A", "Level_C", "Level_E" };
while (rs.next()) {
SalesOrder order = new SalesOrderImpl();
order.loadDataFromDB(rs);
assertEquals(order.getPriceLevel(), priceLevels[i]);
i++;
}
control.verify();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.easymock.IMocksControl in project titan by thinkaurelius.
the class IDPoolTest method testAllocationTimeoutAndRecovery.
@Test
public void testAllocationTimeoutAndRecovery() throws BackendException {
IMocksControl ctrl = EasyMock.createStrictControl();
final int partition = 42;
final int idNamespace = 777;
final Duration timeout = Duration.ofSeconds(1L);
final IDAuthority mockAuthority = ctrl.createMock(IDAuthority.class);
// Sleep for two seconds, then throw a backendexception
// this whole delegate could be deleted if we abstracted StandardIDPool's internal executor and stopwatches
expect(mockAuthority.getIDBlock(partition, idNamespace, timeout)).andDelegateTo(new IDAuthority() {
@Override
public IDBlock getIDBlock(int partition, int idNamespace, Duration timeout) throws BackendException {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
fail();
}
throw new TemporaryBackendException("slow backend");
}
@Override
public List<KeyRange> getLocalIDPartition() throws BackendException {
throw new IllegalArgumentException();
}
@Override
public void setIDBlockSizer(IDBlockSizer sizer) {
throw new IllegalArgumentException();
}
@Override
public void close() throws BackendException {
throw new IllegalArgumentException();
}
@Override
public String getUniqueID() {
throw new IllegalArgumentException();
}
@Override
public boolean supportsInterruption() {
return true;
}
});
expect(mockAuthority.getIDBlock(partition, idNamespace, timeout)).andReturn(new IDBlock() {
@Override
public long numIds() {
return 2;
}
@Override
public long getId(long index) {
return 200;
}
});
expect(mockAuthority.supportsInterruption()).andStubReturn(true);
ctrl.replay();
StandardIDPool pool = new StandardIDPool(mockAuthority, partition, idNamespace, Integer.MAX_VALUE, timeout, 0.1);
try {
pool.nextID();
fail();
} catch (TitanException e) {
}
long nextID = pool.nextID();
assertEquals(200, nextID);
ctrl.verify();
}
use of org.easymock.IMocksControl in project powermock by powermock.
the class PowerMock method doMock.
@SuppressWarnings("unchecked")
private static <T> T doMock(Class<T> type, boolean isStatic, MockStrategy mockStrategy, ConstructorArgs constructorArgs, Method... methods) {
if (type == null) {
throw new IllegalArgumentException("The class to mock cannot be null");
}
/*
* Clear the EasyMock state after the test method is executed.
*/
MockRepository.addAfterMethodRunner(new Runnable() {
@Override
public void run() {
LastControl.reportLastControl(null);
}
});
IMocksControl control = mockStrategy.createMockControl(type);
MockRepository.addAfterMethodRunner(new EasyMockStateCleaner());
T mock;
if (type.isInterface()) {
mock = control.createMock(type);
} else if (type.getName().startsWith("java.") && Modifier.isFinal(type.getModifiers())) {
Class<?> replicaType = createReplicaType(type, isStatic, constructorArgs);
final Object replica = doCreateMock(replicaType, constructorArgs, control, methods);
control = mockStrategy.createMockControl(replicaType);
MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);
final Set<Method> methodsToMock = toSet(methods);
if (isStatic) {
MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));
MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);
return null;
} else {
final T newInstance;
if (constructorArgs == null) {
newInstance = Whitebox.newInstance(type);
DefaultFieldValueGenerator.fillWithDefaultValues(newInstance);
} else {
try {
newInstance = (T) constructorArgs.getConstructor().newInstance(constructorArgs.getInitArgs());
} catch (Exception e) {
throw new RuntimeException("Internal error", e);
}
}
MockRepository.putInstanceMethodInvocationControl(newInstance, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));
if (!(newInstance instanceof InvocationSubstitute<?>)) {
MockRepository.addObjectsToAutomaticallyReplayAndVerify(newInstance);
}
return newInstance;
}
} else {
mock = doCreateMock(type, constructorArgs, control, methods);
}
MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);
final Set<Method> methodsToMock = toSet(methods);
if (isStatic) {
MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<T>(h, methodsToMock, mock));
MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);
} else {
MockRepository.putInstanceMethodInvocationControl(mock, new EasyMockMethodInvocationControl<T>(h, methodsToMock));
if (!(mock instanceof InvocationSubstitute<?>)) {
MockRepository.addObjectsToAutomaticallyReplayAndVerify(mock);
}
}
ClassLoader classLoader = mock.getClass().getClassLoader();
if (classLoader instanceof MockClassLoader) {
((MockClassLoader) classLoader).cache(mock.getClass());
}
return mock;
}
use of org.easymock.IMocksControl in project powermock by powermock.
the class PowerMock method doCreateMock.
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(null, type, null, methods);
}
} else {
if (methods == null) {
mock = mocksControl.createMock(null, type, constructorArgs);
} else {
mock = mocksControl.createMock(null, type, constructorArgs, methods);
}
}
return mock;
}
Aggregations