Skip to content

Commit 7974f9c

Browse files
authored
Refactor to Eliminate Repetitive Mock Object Creation in some tests
1 parent 26fc907 commit 7974f9c

File tree

3 files changed

+54
-71
lines changed

3 files changed

+54
-71
lines changed

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/CachingClientConnectionFactoryTests.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,6 +88,7 @@
8888
/**
8989
* @author Gary Russell
9090
* @author Artem Bilan
91+
* @author Gengwu Zhao
9192
*
9293
* @since 2.2
9394
*
@@ -505,17 +506,13 @@ private void testCloseOnTimeoutGuts(AbstractClientConnectionFactory cf) throws E
505506
@Test
506507
public void testCachedFailover() throws Exception {
507508
// Failover
508-
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
509-
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
509+
TcpConnectionSupport mockConn1 = makeMockConnection();
510+
TcpConnectionSupport mockConn2 = makeMockConnection();
511+
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection(mockConn1);
512+
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection(mockConn2);
510513
List<AbstractClientConnectionFactory> factories = new ArrayList<>();
511514
factories.add(factory1);
512515
factories.add(factory2);
513-
TcpConnectionSupport mockConn1 = makeMockConnection();
514-
TcpConnectionSupport mockConn2 = makeMockConnection();
515-
when(factory1.getConnection()).thenReturn(mockConn1);
516-
when(factory2.getConnection()).thenReturn(mockConn2);
517-
when(factory1.isActive()).thenReturn(true);
518-
when(factory2.isActive()).thenReturn(true);
519516
doThrow(new UncheckedIOException(new IOException("fail"))).when(mockConn1).send(Mockito.any(Message.class));
520517
doAnswer(invocation -> null).when(mockConn2).send(Mockito.any(Message.class));
521518
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
@@ -822,4 +819,11 @@ private TcpConnectionSupport makeMockConnection() {
822819
return connection;
823820
}
824821

822+
private static AbstractClientConnectionFactory createFactoryWithMockConnection(TcpConnectionSupport mockConn) throws Exception {
823+
AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
824+
when(factory.getConnection()).thenReturn(mockConn);
825+
when(factory.isActive()).thenReturn(true);
826+
return factory;
827+
}
828+
825829
}

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,6 +72,7 @@
7272
/**
7373
* @author Gary Russell
7474
* @author Artem Bilan
75+
* @author Gengwu Zhao
7576
*
7677
* @since 2.2
7778
*
@@ -93,17 +94,13 @@ public void publishEvent(Object event) {
9394

9495
@Test
9596
public void testFailoverGood() throws Exception {
96-
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
97-
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
97+
TcpConnectionSupport conn1 = makeMockConnection();
98+
TcpConnectionSupport conn2 = makeMockConnection();
99+
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection(conn1);
100+
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection(conn2);
98101
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
99102
factories.add(factory1);
100103
factories.add(factory2);
101-
TcpConnectionSupport conn1 = makeMockConnection();
102-
TcpConnectionSupport conn2 = makeMockConnection();
103-
when(factory1.getConnection()).thenReturn(conn1);
104-
when(factory2.getConnection()).thenReturn(conn2);
105-
when(factory1.isActive()).thenReturn(true);
106-
when(factory2.isActive()).thenReturn(true);
107104
doThrow(new UncheckedIOException(new IOException("fail")))
108105
.when(conn1).send(Mockito.any(Message.class));
109106
doAnswer(invocation -> null).when(conn2).send(Mockito.any(Message.class));
@@ -181,17 +178,13 @@ private void testRefreshShared(boolean closeOnRefresh, long interval) throws Exc
181178

182179
@Test
183180
public void testFailoverAllDead() throws Exception {
184-
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
185-
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
181+
TcpConnectionSupport conn1 = makeMockConnection();
182+
TcpConnectionSupport conn2 = makeMockConnection();
183+
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection(conn1);
184+
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection(conn2);
186185
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
187186
factories.add(factory1);
188187
factories.add(factory2);
189-
TcpConnectionSupport conn1 = makeMockConnection();
190-
TcpConnectionSupport conn2 = makeMockConnection();
191-
when(factory1.getConnection()).thenReturn(conn1);
192-
when(factory2.getConnection()).thenReturn(conn2);
193-
when(factory1.isActive()).thenReturn(true);
194-
when(factory2.isActive()).thenReturn(true);
195188
doThrow(new UncheckedIOException(new IOException("fail")))
196189
.when(conn1).send(Mockito.any(Message.class));
197190
doThrow(new UncheckedIOException(new IOException("fail")))
@@ -243,17 +236,13 @@ void failoverAllDeadAfterSuccess() throws Exception {
243236

244237
@Test
245238
public void testFailoverAllDeadButOriginalOkAgain() throws Exception {
246-
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
247-
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
239+
TcpConnectionSupport conn1 = makeMockConnection();
240+
TcpConnectionSupport conn2 = makeMockConnection();
241+
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection(conn1);
242+
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection(conn2);
248243
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
249244
factories.add(factory1);
250245
factories.add(factory2);
251-
TcpConnectionSupport conn1 = makeMockConnection();
252-
TcpConnectionSupport conn2 = makeMockConnection();
253-
when(factory1.getConnection()).thenReturn(conn1);
254-
when(factory2.getConnection()).thenReturn(conn2);
255-
when(factory1.isActive()).thenReturn(true);
256-
when(factory2.isActive()).thenReturn(true);
257246
final AtomicBoolean failedOnce = new AtomicBoolean();
258247
doAnswer(invocation -> {
259248
if (!failedOnce.get()) {
@@ -315,17 +304,13 @@ public void testFailoverConnectToFirstAfterTriedAll() throws Exception {
315304

316305
@Test
317306
public void testOkAgainAfterCompleteFailure() throws Exception {
318-
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
319-
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
307+
TcpConnectionSupport conn1 = makeMockConnection();
308+
TcpConnectionSupport conn2 = makeMockConnection();
309+
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection(conn1);
310+
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection(conn2);
320311
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
321312
factories.add(factory1);
322313
factories.add(factory2);
323-
TcpConnectionSupport conn1 = makeMockConnection();
324-
TcpConnectionSupport conn2 = makeMockConnection();
325-
when(factory1.getConnection()).thenReturn(conn1);
326-
when(factory2.getConnection()).thenReturn(conn2);
327-
when(factory1.isActive()).thenReturn(true);
328-
when(factory2.isActive()).thenReturn(true);
329314
final AtomicInteger failCount = new AtomicInteger();
330315
doAnswer(invocation -> {
331316
if (failCount.incrementAndGet() < 3) {
@@ -710,5 +695,12 @@ private static class Holder {
710695

711696
}
712697

698+
private static AbstractClientConnectionFactory createFactoryWithMockConnection(TcpConnectionSupport mockConn) throws Exception {
699+
AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
700+
when(factory.getConnection()).thenReturn(mockConn);
701+
when(factory.isActive()).thenReturn(true);
702+
return factory;
703+
}
704+
713705
}
714706

spring-integration-jms/src/test/java/org/springframework/integration/jms/OutboundGatewayFunctionTests.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
/**
4545
* @author Gary Russell
4646
* @author Artem Bilan
47+
* @author Gengwu Zhao
4748
*
4849
* @since 2.2
4950
*
@@ -72,12 +73,9 @@ public class OutboundGatewayFunctionTests extends ActiveMQMultiContextTests {
7273

7374
@Test
7475
public void testContainerWithDest() throws Exception {
75-
BeanFactory beanFactory = mock(BeanFactory.class);
76-
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
7776
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
7877
scheduler.initialize();
79-
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
80-
.thenReturn(scheduler);
78+
BeanFactory beanFactory = createFactoryWithMockScheduler(scheduler);
8179
final JmsOutboundGateway gateway = new JmsOutboundGateway();
8280
gateway.setBeanFactory(beanFactory);
8381
gateway.setConnectionFactory(connectionFactory);
@@ -118,12 +116,9 @@ public void testContainerWithDest() throws Exception {
118116

119117
@Test
120118
public void testContainerWithDestNoCorrelation() throws Exception {
121-
BeanFactory beanFactory = mock(BeanFactory.class);
122-
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
123119
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
124120
scheduler.initialize();
125-
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
126-
.thenReturn(scheduler);
121+
BeanFactory beanFactory = createFactoryWithMockScheduler(scheduler);
127122
final JmsOutboundGateway gateway = new JmsOutboundGateway();
128123
gateway.setBeanFactory(beanFactory);
129124
gateway.setConnectionFactory(connectionFactory);
@@ -166,12 +161,9 @@ public void testContainerWithDestNoCorrelation() throws Exception {
166161

167162
@Test
168163
public void testContainerWithDestName() throws Exception {
169-
BeanFactory beanFactory = mock(BeanFactory.class);
170-
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
171164
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
172165
scheduler.initialize();
173-
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
174-
.thenReturn(scheduler);
166+
BeanFactory beanFactory = createFactoryWithMockScheduler(scheduler);
175167
final JmsOutboundGateway gateway = new JmsOutboundGateway();
176168
gateway.setBeanFactory(beanFactory);
177169
gateway.setConnectionFactory(connectionFactory);
@@ -212,12 +204,9 @@ public void testContainerWithDestName() throws Exception {
212204

213205
@Test
214206
public void testContainerWithDestNameNoCorrelation() throws Exception {
215-
BeanFactory beanFactory = mock(BeanFactory.class);
216-
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
217207
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
218208
scheduler.initialize();
219-
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
220-
.thenReturn(scheduler);
209+
BeanFactory beanFactory = createFactoryWithMockScheduler(scheduler);
221210
final JmsOutboundGateway gateway = new JmsOutboundGateway();
222211
gateway.setBeanFactory(beanFactory);
223212
gateway.setConnectionFactory(connectionFactory);
@@ -260,12 +249,9 @@ public void testContainerWithDestNameNoCorrelation() throws Exception {
260249

261250
@Test
262251
public void testContainerWithTemporary() throws Exception {
263-
BeanFactory beanFactory = mock(BeanFactory.class);
264-
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
265252
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
266253
scheduler.initialize();
267-
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
268-
.thenReturn(scheduler);
254+
BeanFactory beanFactory = createFactoryWithMockScheduler(scheduler);
269255
final JmsOutboundGateway gateway = new JmsOutboundGateway();
270256
gateway.setBeanFactory(beanFactory);
271257
gateway.setConnectionFactory(connectionFactory);
@@ -306,12 +292,9 @@ public void testContainerWithTemporary() throws Exception {
306292

307293
@Test
308294
public void testContainerWithTemporaryNoCorrelation() throws Exception {
309-
BeanFactory beanFactory = mock(BeanFactory.class);
310-
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
311295
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
312296
scheduler.initialize();
313-
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
314-
.thenReturn(scheduler);
297+
BeanFactory beanFactory = createFactoryWithMockScheduler(scheduler);
315298
final JmsOutboundGateway gateway = new JmsOutboundGateway();
316299
gateway.setBeanFactory(beanFactory);
317300
gateway.setConnectionFactory(connectionFactory);
@@ -353,12 +336,9 @@ public void testContainerWithTemporaryNoCorrelation() throws Exception {
353336

354337
@Test
355338
public void testLazyContainerWithDest() throws Exception {
356-
BeanFactory beanFactory = mock(BeanFactory.class);
357-
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
358339
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
359340
scheduler.initialize();
360-
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
361-
.thenReturn(scheduler);
341+
BeanFactory beanFactory = createFactoryWithMockScheduler(scheduler);
362342
final JmsOutboundGateway gateway = new JmsOutboundGateway();
363343
gateway.setBeanFactory(beanFactory);
364344
gateway.setConnectionFactory(connectionFactory);
@@ -407,4 +387,11 @@ private void receiveAndSend(JmsTemplate template) {
407387
}
408388
}
409389

390+
private static BeanFactory createFactoryWithMockScheduler(ThreadPoolTaskScheduler scheduler) {
391+
BeanFactory beanFactory = mock(BeanFactory.class);
392+
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
393+
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
394+
.thenReturn(scheduler);
395+
return beanFactory;
396+
}
410397
}

0 commit comments

Comments
 (0)