@Test public void testSunnyDayPathLogsPerformanceMetricsCorrectly() throws Throwable { MethodInvocation mi = mock(MethodInvocation.class); given(mi.getMethod()).willReturn(String.class.getMethod("toString", new Class[0])); Log log = mock(Log.class); PerformanceMonitorInterceptor interceptor = new PerformanceMonitorInterceptor(true); interceptor.invokeUnderTrace(mi, log); verify(log).trace(anyString()); }
protected final ContentHandler mockContentHandler() throws Exception { ContentHandler contentHandler = mock(ContentHandler.class); willAnswer(new CopyCharsAnswer()).given(contentHandler).characters(any(char[].class), anyInt(), anyInt()); willAnswer(new CopyCharsAnswer()).given(contentHandler).ignorableWhitespace(any(char[].class), anyInt(), anyInt()); willAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]); return null; } }).given(contentHandler).startElement(anyString(), anyString(), anyString(), any(Attributes.class)); return contentHandler; }
@Test public void testExceptionPathStillLogsCorrectly() throws Throwable { MethodInvocation mi = mock(MethodInvocation.class); given(mi.getMethod()).willReturn(String.class.getMethod("toString")); given(mi.getThis()).willReturn(this); IllegalArgumentException exception = new IllegalArgumentException(); given(mi.proceed()).willThrow(exception); Log log = mock(Log.class); final SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true); try { interceptor.invokeUnderTrace(mi, log); fail("Must have propagated the IllegalArgumentException."); } catch (IllegalArgumentException expected) { } verify(log).trace(anyString()); verify(log).trace(anyString(), eq(exception)); }
@Test public void testSunnyDayPathLogsCorrectly() throws Throwable { MethodInvocation mi = mock(MethodInvocation.class); given(mi.getMethod()).willReturn(String.class.getMethod("toString")); given(mi.getThis()).willReturn(this); Log log = mock(Log.class); SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true); interceptor.invokeUnderTrace(mi, log); verify(log, times(2)).trace(anyString()); }
@Test public void testExceptionPathStillLogsPerformanceMetricsCorrectly() throws Throwable { MethodInvocation mi = mock(MethodInvocation.class); given(mi.getMethod()).willReturn(String.class.getMethod("toString", new Class[0])); given(mi.proceed()).willThrow(new IllegalArgumentException()); Log log = mock(Log.class); PerformanceMonitorInterceptor interceptor = new PerformanceMonitorInterceptor(true); try { interceptor.invokeUnderTrace(mi, log); fail("Must have propagated the IllegalArgumentException."); } catch (IllegalArgumentException expected) { } verify(log).trace(anyString()); }
@Test public void testExceptionPathStillLogsCorrectly() throws Throwable { MethodInvocation methodInvocation = mock(MethodInvocation.class); IllegalArgumentException exception = new IllegalArgumentException(); given(methodInvocation.proceed()).willThrow(exception); Log log = mock(Log.class); given(log.isTraceEnabled()).willReturn(true); DebugInterceptor interceptor = new StubDebugInterceptor(log); try { interceptor.invoke(methodInvocation); fail("Must have propagated the IllegalArgumentException."); } catch (IllegalArgumentException expected) { } checkCallCountTotal(interceptor); verify(log).trace(anyString()); verify(log).trace(anyString(), eq(exception)); }
@Test public void testSunnyDayPathLogsCorrectly() throws Throwable { MethodInvocation methodInvocation = mock(MethodInvocation.class); given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString")); given(methodInvocation.getThis()).willReturn(this); Log log = mock(Log.class); given(log.isTraceEnabled()).willReturn(true); CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log); interceptor.invoke(methodInvocation); verify(log, times(2)).trace(anyString()); }
@Before public void setUp() throws SQLException { given(connection.createStatement()).willReturn(statement); given(connection.prepareStatement(anyString())).willReturn(preparedStatement); given(statement.executeQuery(anyString())).willReturn(resultSet); given(preparedStatement.executeQuery()).willReturn(resultSet); given(resultSet.next()).willReturn(true, true, false); given(resultSet.getString(1)).willReturn("tb1", "tb2"); given(resultSet.getInt(2)).willReturn(1, 2); template.setDataSource(new SingleConnectionDataSource(connection, false)); template.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); template.afterPropertiesSet(); }
@Before public void setUp() throws Exception { this.connection = mock(Connection.class); this.dataSource = mock(DataSource.class); this.statement = mock(Statement.class); this.preparedStatement = mock(PreparedStatement.class); this.resultSet = mock(ResultSet.class); this.resultSetMetaData = mock(ResultSetMetaData.class); this.template = new JdbcTemplate(this.dataSource); given(this.dataSource.getConnection()).willReturn(this.connection); given(this.resultSet.getMetaData()).willReturn(this.resultSetMetaData); given(this.resultSetMetaData.getColumnCount()).willReturn(1); given(this.resultSetMetaData.getColumnLabel(1)).willReturn("age"); given(this.connection.createStatement()).willReturn(this.statement); given(this.connection.prepareStatement(anyString())).willReturn(this.preparedStatement); given(this.preparedStatement.executeQuery()).willReturn(this.resultSet); given(this.statement.executeQuery(anyString())).willReturn(this.resultSet); }
@Test public void testBatchUpdateWithNoBatchSupport() throws Exception { final String[] sql = {"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1", "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 2"}; given(this.statement.execute(sql[0])).willReturn(false); given(this.statement.getUpdateCount()).willReturn(1, 1); given(this.statement.execute(sql[1])).willReturn(false); mockDatabaseMetaData(false); given(this.connection.createStatement()).willReturn(this.statement); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); int[] actualRowsAffected = template.batchUpdate(sql); assertTrue("executed 2 updates", actualRowsAffected.length == 2); verify(this.statement, never()).addBatch(anyString()); verify(this.statement).close(); verify(this.connection, atLeastOnce()).close(); }
@Before public void setUp() throws Exception { this.connection = mock(Connection.class); this.dataSource = mock(DataSource.class); this.preparedStatement = mock(PreparedStatement.class); this.resultSet = mock(ResultSet.class); given(this.dataSource.getConnection()).willReturn(this.connection); given(this.connection.prepareStatement(anyString())).willReturn(this.preparedStatement); given(preparedStatement.executeQuery()).willReturn(resultSet); }
@Test public void replyPayloadToDestination() throws JMSException { Session session = mock(Session.class); MessageProducer messageProducer = mock(MessageProducer.class); TextMessage responseMessage = mock(TextMessage.class); given(session.createTextMessage("Response")).willReturn(responseMessage); given(session.createProducer(sharedReplyDestination)).willReturn(messageProducer); MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadToDestination", Message.class); listener.onMessage(mock(javax.jms.Message.class), session); verify(session, times(0)).createQueue(anyString()); verify(session).createTextMessage("Response"); verify(messageProducer).send(responseMessage); verify(messageProducer).close(); }
@Before public void setup() throws Exception { this.connection = mock(Connection.class); this.dataSource = mock(DataSource.class); this.preparedStatement = mock(PreparedStatement.class); this.statement = mock(Statement.class); this.resultSet = mock(ResultSet.class); this.template = new JdbcTemplate(this.dataSource); this.callableStatement = mock(CallableStatement.class); given(this.dataSource.getConnection()).willReturn(this.connection); given(this.connection.prepareStatement(anyString())).willReturn(this.preparedStatement); given(this.preparedStatement.executeQuery()).willReturn(this.resultSet); given(this.preparedStatement.executeQuery(anyString())).willReturn(this.resultSet); given(this.preparedStatement.getConnection()).willReturn(this.connection); given(this.statement.getConnection()).willReturn(this.connection); given(this.statement.executeQuery(anyString())).willReturn(this.resultSet); given(this.connection.prepareCall(anyString())).willReturn(this.callableStatement); given(this.callableStatement.getResultSet()).willReturn(this.resultSet); }
@Test public void testSunnyDayPathLogsCorrectly() throws Throwable { MethodInvocation methodInvocation = mock(MethodInvocation.class); Log log = mock(Log.class); given(log.isTraceEnabled()).willReturn(true); DebugInterceptor interceptor = new StubDebugInterceptor(log); interceptor.invoke(methodInvocation); checkCallCountTotal(interceptor); verify(log, times(2)).trace(anyString()); }
@Before public void setup() throws Exception { connection = mock(Connection.class); dataSource = mock(DataSource.class); preparedStatement = mock(PreparedStatement.class); resultSet = mock(ResultSet.class); resultSetMetaData = mock(ResultSetMetaData.class); template = new NamedParameterJdbcTemplate(dataSource); given(dataSource.getConnection()).willReturn(connection); given(resultSetMetaData.getColumnCount()).willReturn(1); given(resultSetMetaData.getColumnLabel(1)).willReturn("age"); given(connection.prepareStatement(anyString())).willReturn(preparedStatement); given(preparedStatement.executeQuery()).willReturn(resultSet); }
@Test public void replyPayloadNoDestination() throws JMSException { Queue replyDestination = mock(Queue.class); Session session = mock(Session.class); MessageProducer messageProducer = mock(MessageProducer.class); TextMessage responseMessage = mock(TextMessage.class); given(session.createTextMessage("Response")).willReturn(responseMessage); given(session.createProducer(replyDestination)).willReturn(messageProducer); MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadNoDestination", Message.class); listener.setDefaultResponseDestination(replyDestination); listener.onMessage(mock(javax.jms.Message.class), session); verify(session, times(0)).createQueue(anyString()); verify(session).createTextMessage("Response"); verify(messageProducer).send(responseMessage); verify(messageProducer).close(); }
public TextMessage testReplyWithJackson(String methodName, String replyContent) throws JMSException { Queue replyDestination = mock(Queue.class); Session session = mock(Session.class); MessageProducer messageProducer = mock(MessageProducer.class); TextMessage responseMessage = mock(TextMessage.class); given(session.createTextMessage(replyContent)).willReturn(responseMessage); given(session.createProducer(replyDestination)).willReturn(messageProducer); MessagingMessageListenerAdapter listener = getPayloadInstance("Response", methodName, Message.class); MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter(); messageConverter.setTargetType(MessageType.TEXT); listener.setMessageConverter(messageConverter); listener.setDefaultResponseDestination(replyDestination); listener.onMessage(mock(javax.jms.Message.class), session); verify(session, times(0)).createQueue(anyString()); verify(session).createTextMessage(replyContent); verify(messageProducer).send(responseMessage); verify(messageProducer).close(); return responseMessage; }
@Before public void setup() throws Exception { connection = mock(Connection.class); dataSource = mock(DataSource.class); preparedStatement = mock(PreparedStatement.class); resultSet = mock(ResultSet.class); namedParameterTemplate = new NamedParameterJdbcTemplate(dataSource); databaseMetaData = mock(DatabaseMetaData.class); given(dataSource.getConnection()).willReturn(connection); given(connection.prepareStatement(anyString())).willReturn(preparedStatement); given(preparedStatement.getConnection()).willReturn(connection); given(preparedStatement.executeQuery()).willReturn(resultSet); given(databaseMetaData.getDatabaseProductName()).willReturn("MySQL"); given(databaseMetaData.supportsBatchUpdates()).willReturn(true); }
@Test public void missingDataSourceAndTxMgr() throws Exception { ApplicationContext ctx = mock(ApplicationContext.class); given(ctx.getResource(anyString())).willReturn(mock(Resource.class)); given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class)); Class<?> clazz = MissingDataSourceAndTxMgr.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); given(testContext.getApplicationContext()).willReturn(ctx); assertExceptionContains("supply at least a DataSource or PlatformTransactionManager"); }
@Test public void isolatedTxModeDeclaredWithoutTxMgr() throws Exception { ApplicationContext ctx = mock(ApplicationContext.class); given(ctx.getResource(anyString())).willReturn(mock(Resource.class)); given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class)); Class<?> clazz = IsolatedWithoutTxMgr.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); given(testContext.getApplicationContext()).willReturn(ctx); assertExceptionContains("cannot execute SQL scripts using Transaction Mode [ISOLATED] without a PlatformTransactionManager"); }