Skip to content

Commit 24422b5

Browse files
snatefmbenhassine
authored andcommitted
Remove some deprecated APIs from tests
The following deprecated APIs are not in use anymore in the project's tests: - `org.junit.rules.ExpectedException#none` - `org.mockito.MockitoAnnotations#initMocks` - `org.mockito.Mockito#verifyZeroInteractions` Issue #3838
1 parent 7242f8f commit 24422b5

22 files changed

+329
-369
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import java.util.Properties;
2424

2525
import org.junit.Before;
26-
import org.junit.Rule;
2726
import org.junit.Test;
28-
import org.junit.rules.ExpectedException;
2927

3028
import org.springframework.batch.core.explore.JobExplorer;
3129
import org.springframework.batch.core.job.SimpleJob;
@@ -34,6 +32,7 @@
3432
import static org.junit.Assert.assertEquals;
3533
import static org.junit.Assert.assertFalse;
3634
import static org.junit.Assert.assertNull;
35+
import static org.junit.Assert.assertThrows;
3736
import static org.mockito.ArgumentMatchers.any;
3837
import static org.mockito.Mockito.mock;
3938
import static org.mockito.Mockito.when;
@@ -59,9 +58,6 @@ public class JobParametersBuilderTests {
5958

6059
private Date date = new Date(System.currentTimeMillis());
6160

62-
@Rule
63-
public ExpectedException expectedException = ExpectedException.none();
64-
6561
@Before
6662
public void initialize() {
6763
this.job = new SimpleJob("simpleJob");
@@ -204,10 +200,10 @@ public void testGetNextJobParametersFirstRun(){
204200

205201
@Test
206202
public void testGetNextJobParametersNoIncrementer(){
207-
expectedException.expect(IllegalArgumentException.class);
208-
expectedException.expectMessage("No job parameters incrementer found for job=simpleJob");
209203
initializeForNextJobParameters();
210-
this.parametersBuilder.getNextJobParameters(this.job);
204+
final Exception expectedException = assertThrows(IllegalArgumentException.class,
205+
() -> this.parametersBuilder.getNextJobParameters(this.job));
206+
assertEquals("No job parameters incrementer found for job=simpleJob", expectedException.getMessage());
211207
}
212208

213209
@Test

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTests.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
package org.springframework.batch.core.configuration.annotation;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
2021

2122
import java.util.concurrent.Callable;
2223

2324
import org.junit.After;
25+
import org.junit.Assert;
2426
import org.junit.Before;
25-
import org.junit.Rule;
2627
import org.junit.Test;
27-
import org.junit.rules.ExpectedException;
2828

2929
import org.springframework.batch.core.JobExecution;
3030
import org.springframework.batch.core.JobInstance;
@@ -57,9 +57,6 @@ public class JobScopeConfigurationTests {
5757

5858
private JobExecution jobExecution;
5959

60-
@Rule
61-
public ExpectedException expected = ExpectedException.none();
62-
6360
@Test
6461
public void testXmlJobScopeWithProxyTargetClass() throws Exception {
6562
context = new ClassPathXmlApplicationContext(
@@ -116,20 +113,22 @@ public void testJobScopeWithProxyTargetClassInjected() throws Exception {
116113
public void testIntentionallyBlowUpOnMissingContextWithProxyTargetClass() throws Exception {
117114
init(JobScopeConfigurationRequiringProxyTargetClass.class);
118115
JobSynchronizationManager.release();
119-
expected.expect(BeanCreationException.class);
120-
expected.expectMessage("job scope");
121-
SimpleHolder value = context.getBean(SimpleHolder.class);
122-
assertEquals("JOB", value.call());
116+
final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> {
117+
SimpleHolder value = context.getBean(SimpleHolder.class);
118+
assertEquals("JOB", value.call());
119+
});
120+
assertTrue(expectedException.getMessage().contains("job scope"));
123121
}
124122

125123
@Test
126124
public void testIntentionallyBlowupWithForcedInterface() throws Exception {
127125
init(JobScopeConfigurationForcingInterfaceProxy.class);
128126
JobSynchronizationManager.release();
129-
expected.expect(BeanCreationException.class);
130-
expected.expectMessage("job scope");
131-
SimpleHolder value = context.getBean(SimpleHolder.class);
132-
assertEquals("JOB", value.call());
127+
final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> {
128+
SimpleHolder value = context.getBean(SimpleHolder.class);
129+
assertEquals("JOB", value.call());
130+
});
131+
assertTrue(expectedException.getMessage().contains("job scope"));
133132
}
134133

135134
@Test
@@ -144,11 +143,12 @@ public void testJobScopeWithDefaults() throws Exception {
144143
public void testIntentionallyBlowUpOnMissingContextWithInterface() throws Exception {
145144
init(JobScopeConfigurationWithDefaults.class);
146145
JobSynchronizationManager.release();
147-
expected.expect(BeanCreationException.class);
148-
expected.expectMessage("job scope");
149-
@SuppressWarnings("unchecked")
150-
Callable<String> value = context.getBean(Callable.class);
151-
assertEquals("JOB", value.call());
146+
final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> {
147+
@SuppressWarnings("unchecked")
148+
Callable<String> value = context.getBean(Callable.class);
149+
assertEquals("JOB", value.call());
150+
});
151+
assertTrue(expectedException.getMessage().contains("job scope"));
152152
}
153153

154154
public void init(Class<?>... config) throws Exception {

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
package org.springframework.batch.core.configuration.annotation;
1818

1919
import org.junit.After;
20+
import org.junit.Assert;
2021
import org.junit.Before;
21-
import org.junit.Rule;
2222
import org.junit.Test;
23-
import org.junit.rules.ExpectedException;
2423
import org.springframework.batch.core.StepContribution;
2524
import org.springframework.batch.core.StepExecution;
2625
import org.springframework.batch.core.scope.context.ChunkContext;
@@ -42,6 +41,7 @@
4241
import java.util.concurrent.Callable;
4342

4443
import static org.junit.Assert.assertEquals;
44+
import static org.junit.Assert.assertTrue;
4545

4646
/**
4747
* @author Dave Syer
@@ -55,9 +55,6 @@ public class StepScopeConfigurationTests {
5555

5656
private StepExecution stepExecution;
5757

58-
@Rule
59-
public ExpectedException expected = ExpectedException.none();
60-
6158
@Test
6259
public void testXmlStepScopeWithProxyTargetClass() throws Exception {
6360
context = new ClassPathXmlApplicationContext(
@@ -114,20 +111,23 @@ public void testStepScopeWithProxyTargetClassInjected() throws Exception {
114111
public void testIntentionallyBlowUpOnMissingContextWithProxyTargetClass() throws Exception {
115112
init(StepScopeConfigurationRequiringProxyTargetClass.class);
116113
StepSynchronizationManager.release();
117-
expected.expect(BeanCreationException.class);
118-
expected.expectMessage("step scope");
119-
SimpleHolder value = context.getBean(SimpleHolder.class);
120-
assertEquals("STEP", value.call());
114+
115+
final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> {
116+
SimpleHolder value = context.getBean(SimpleHolder.class);
117+
assertEquals("STEP", value.call());
118+
});
119+
assertTrue(expectedException.getMessage().contains("step scope"));
121120
}
122121

123122
@Test
124123
public void testIntentionallyBlowupWithForcedInterface() throws Exception {
125124
init(StepScopeConfigurationForcingInterfaceProxy.class);
126125
StepSynchronizationManager.release();
127-
expected.expect(BeanCreationException.class);
128-
expected.expectMessage("step scope");
129-
SimpleHolder value = context.getBean(SimpleHolder.class);
130-
assertEquals("STEP", value.call());
126+
final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> {
127+
SimpleHolder value = context.getBean(SimpleHolder.class);
128+
assertEquals("STEP", value.call());
129+
});
130+
assertTrue(expectedException.getMessage().contains("step scope"));
131131
}
132132

133133
@Test
@@ -142,11 +142,13 @@ public void testStepScopeWithDefaults() throws Exception {
142142
public void testIntentionallyBlowUpOnMissingContextWithInterface() throws Exception {
143143
init(StepScopeConfigurationWithDefaults.class);
144144
StepSynchronizationManager.release();
145-
expected.expect(BeanCreationException.class);
146-
expected.expectMessage("step scope");
147-
@SuppressWarnings("unchecked")
148-
Callable<String> value = context.getBean(Callable.class);
149-
assertEquals("STEP", value.call());
145+
146+
final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> {
147+
@SuppressWarnings("unchecked")
148+
Callable<String> value = context.getBean(Callable.class);
149+
assertEquals("STEP", value.call());
150+
});
151+
assertTrue(expectedException.getMessage().contains("step scope"));
150152
}
151153

152154
public void init(Class<?>... config) throws Exception {

spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package org.springframework.batch.core.jsr.configuration.xml;
1717

1818
import org.junit.Assert;
19-
import org.junit.Rule;
2019
import org.junit.Test;
21-
import org.junit.rules.ExpectedException;
2220
import org.springframework.batch.core.jsr.AbstractJsrTestCase;
2321
import org.springframework.beans.PropertyValue;
2422
import org.springframework.beans.factory.config.RuntimeBeanReference;
@@ -32,9 +30,6 @@
3230

3331
public class JsrSplitParsingTests extends AbstractJsrTestCase {
3432

35-
@Rule
36-
public ExpectedException expectedException = ExpectedException.none();
37-
3833
@Test
3934
public void testOneFlowInSplit() {
4035
try {

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
import org.aopalliance.intercept.MethodInvocation;
2727
import org.apache.commons.logging.Log;
2828
import org.apache.commons.logging.LogFactory;
29+
import org.junit.Assert;
2930
import org.junit.Before;
30-
import org.junit.Rule;
3131
import org.junit.Test;
3232

33-
import org.junit.rules.ExpectedException;
3433
import org.springframework.aop.framework.ProxyFactory;
3534
import org.springframework.batch.core.BatchStatus;
3635
import org.springframework.batch.core.ChunkListener;
@@ -80,9 +79,6 @@
8079
*/
8180
public class FaultTolerantStepFactoryBeanTests {
8281

83-
@Rule
84-
public ExpectedException expectedException = ExpectedException.none();
85-
8682
protected final Log logger = LogFactory.getLog(getClass());
8783

8884
private FaultTolerantStepFactoryBean<String, String> factory;
@@ -142,25 +138,29 @@ public void setUp() throws Exception {
142138
}
143139

144140
@Test
145-
public void testMandatoryReader() throws Exception {
141+
public void testMandatoryReader() {
142+
// given
146143
factory = new FaultTolerantStepFactoryBean<>();
147144
factory.setItemWriter(writer);
148145

149-
expectedException.expect(IllegalStateException.class);
150-
expectedException.expectMessage("ItemReader must be provided");
146+
// when
147+
final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject);
151148

152-
factory.getObject();
149+
// then
150+
assertEquals("ItemReader must be provided", expectedException.getMessage());
153151
}
154152

155153
@Test
156154
public void testMandatoryWriter() throws Exception {
155+
// given
157156
factory = new FaultTolerantStepFactoryBean<>();
158157
factory.setItemReader(reader);
159158

160-
expectedException.expect(IllegalStateException.class);
161-
expectedException.expectMessage("ItemWriter must be provided");
159+
// when
160+
final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject);
162161

163-
factory.getObject();
162+
// then
163+
assertEquals("ItemWriter must be provided", expectedException.getMessage());
164164
}
165165

166166
/**

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
import java.util.Collections;
2727
import java.util.List;
2828

29+
import org.junit.Assert;
2930
import org.junit.Before;
30-
import org.junit.Rule;
3131
import org.junit.Test;
32-
import org.junit.rules.ExpectedException;
3332
import org.springframework.batch.core.BatchStatus;
3433
import org.springframework.batch.core.ChunkListener;
3534
import org.springframework.batch.core.ItemProcessListener;
@@ -66,9 +65,6 @@
6665
*/
6766
public class SimpleStepFactoryBeanTests {
6867

69-
@Rule
70-
public ExpectedException expectedException = ExpectedException.none();
71-
7268
private List<Exception> listened = new ArrayList<>();
7369

7470
private SimpleJobRepository repository = new SimpleJobRepository(new MapJobInstanceDao(), new MapJobExecutionDao(),
@@ -99,25 +95,29 @@ public void testMandatoryProperties() throws Exception {
9995
}
10096

10197
@Test
102-
public void testMandatoryReader() throws Exception {
98+
public void testMandatoryReader() {
99+
// given
103100
SimpleStepFactoryBean<String, String> factory = new SimpleStepFactoryBean<>();
104101
factory.setItemWriter(writer);
105102

106-
expectedException.expect(IllegalStateException.class);
107-
expectedException.expectMessage("ItemReader must be provided");
103+
// when
104+
final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject);
108105

109-
factory.getObject();
106+
// then
107+
assertEquals("ItemReader must be provided", expectedException.getMessage());
110108
}
111109

112110
@Test
113-
public void testMandatoryWriter() throws Exception {
111+
public void testMandatoryWriter() {
112+
// given
114113
SimpleStepFactoryBean<String, String> factory = new SimpleStepFactoryBean<>();
115114
factory.setItemReader(reader);
116115

117-
expectedException.expect(IllegalStateException.class);
118-
expectedException.expectMessage("ItemWriter must be provided");
116+
// when
117+
final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject);
119118

120-
factory.getObject();
119+
// then
120+
assertEquals("ItemWriter must be provided", expectedException.getMessage());
121121
}
122122

123123
@Test

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/GemfireItemWriterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import static org.junit.Assert.fail;
1919
import static org.mockito.Mockito.verify;
20-
import static org.mockito.Mockito.verifyZeroInteractions;
20+
import static org.mockito.Mockito.verifyNoInteractions;
2121

2222
import java.util.ArrayList;
2323
import java.util.List;
@@ -129,7 +129,7 @@ public String convert(Foo item) {
129129
@Test
130130
public void testWriteNoTransactionNoItems() throws Exception {
131131
writer.write(null);
132-
verifyZeroInteractions(template);
132+
verifyNoInteractions(template);
133133
}
134134

135135
static class Foo {

0 commit comments

Comments
 (0)