From 24422b51630f464db9d7b6cc95b861fdb151ac23 Mon Sep 17 00:00:00 2001 From: Sebastiano Valle Date: Fri, 9 Jul 2021 17:15:57 +0200 Subject: [PATCH] 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 --- .../batch/core/JobParametersBuilderTests.java | 12 +- .../JobScopeConfigurationTests.java | 36 +++--- .../StepScopeConfigurationTests.java | 38 +++--- .../xml/JsrSplitParsingTests.java | 5 - .../FaultTolerantStepFactoryBeanTests.java | 24 ++-- .../step/item/SimpleStepFactoryBeanTests.java | 26 ++-- .../item/data/GemfireItemWriterTests.java | 4 +- .../batch/item/data/MongoItemWriterTests.java | 14 +-- .../batch/item/data/Neo4jItemWriterTests.java | 8 +- .../item/data/RepositoryItemWriterTests.java | 4 +- .../json/JsonItemReaderFunctionalTests.java | 33 +++--- .../batch/item/json/JsonItemReaderTests.java | 26 ++-- .../builder/KafkaItemReaderBuilderTests.java | 112 ++++++++++-------- .../builder/KafkaItemWriterBuilderTests.java | 26 ++-- ...ractSynchronizedItemStreamWriterTests.java | 4 - .../SynchronizedItemStreamWriterTests.java | 11 +- ...chronizedItemStreamWriterBuilderTests.java | 14 ++- .../RemoteChunkingManagerStepBuilderTest.java | 90 ++++++-------- .../RemoteChunkingWorkerBuilderTest.java | 67 ++++------- ...otePartitioningMasterStepBuilderTests.java | 70 +++++------ ...otePartitioningWorkerStepBuilderTests.java | 60 ++++------ .../BatchTestContextCustomizerTest.java | 14 +-- 22 files changed, 329 insertions(+), 369 deletions(-) diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java index ec194ffb6f..f26d603669 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java @@ -23,9 +23,7 @@ import java.util.Properties; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.job.SimpleJob; @@ -34,6 +32,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -59,9 +58,6 @@ public class JobParametersBuilderTests { private Date date = new Date(System.currentTimeMillis()); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Before public void initialize() { this.job = new SimpleJob("simpleJob"); @@ -204,10 +200,10 @@ public void testGetNextJobParametersFirstRun(){ @Test public void testGetNextJobParametersNoIncrementer(){ - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("No job parameters incrementer found for job=simpleJob"); initializeForNextJobParameters(); - this.parametersBuilder.getNextJobParameters(this.job); + final Exception expectedException = assertThrows(IllegalArgumentException.class, + () -> this.parametersBuilder.getNextJobParameters(this.job)); + assertEquals("No job parameters incrementer found for job=simpleJob", expectedException.getMessage()); } @Test diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTests.java index dc3ec0498e..7b44e971ae 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTests.java @@ -17,14 +17,14 @@ package org.springframework.batch.core.configuration.annotation; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.concurrent.Callable; import org.junit.After; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobInstance; @@ -57,9 +57,6 @@ public class JobScopeConfigurationTests { private JobExecution jobExecution; - @Rule - public ExpectedException expected = ExpectedException.none(); - @Test public void testXmlJobScopeWithProxyTargetClass() throws Exception { context = new ClassPathXmlApplicationContext( @@ -116,20 +113,22 @@ public void testJobScopeWithProxyTargetClassInjected() throws Exception { public void testIntentionallyBlowUpOnMissingContextWithProxyTargetClass() throws Exception { init(JobScopeConfigurationRequiringProxyTargetClass.class); JobSynchronizationManager.release(); - expected.expect(BeanCreationException.class); - expected.expectMessage("job scope"); - SimpleHolder value = context.getBean(SimpleHolder.class); - assertEquals("JOB", value.call()); + final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> { + SimpleHolder value = context.getBean(SimpleHolder.class); + assertEquals("JOB", value.call()); + }); + assertTrue(expectedException.getMessage().contains("job scope")); } @Test public void testIntentionallyBlowupWithForcedInterface() throws Exception { init(JobScopeConfigurationForcingInterfaceProxy.class); JobSynchronizationManager.release(); - expected.expect(BeanCreationException.class); - expected.expectMessage("job scope"); - SimpleHolder value = context.getBean(SimpleHolder.class); - assertEquals("JOB", value.call()); + final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> { + SimpleHolder value = context.getBean(SimpleHolder.class); + assertEquals("JOB", value.call()); + }); + assertTrue(expectedException.getMessage().contains("job scope")); } @Test @@ -144,11 +143,12 @@ public void testJobScopeWithDefaults() throws Exception { public void testIntentionallyBlowUpOnMissingContextWithInterface() throws Exception { init(JobScopeConfigurationWithDefaults.class); JobSynchronizationManager.release(); - expected.expect(BeanCreationException.class); - expected.expectMessage("job scope"); - @SuppressWarnings("unchecked") - Callable value = context.getBean(Callable.class); - assertEquals("JOB", value.call()); + final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> { + @SuppressWarnings("unchecked") + Callable value = context.getBean(Callable.class); + assertEquals("JOB", value.call()); + }); + assertTrue(expectedException.getMessage().contains("job scope")); } public void init(Class... config) throws Exception { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java index 2af2dc03b9..3d5f72b279 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java @@ -17,10 +17,9 @@ package org.springframework.batch.core.configuration.annotation; import org.junit.After; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.scope.context.ChunkContext; @@ -42,6 +41,7 @@ import java.util.concurrent.Callable; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Dave Syer @@ -55,9 +55,6 @@ public class StepScopeConfigurationTests { private StepExecution stepExecution; - @Rule - public ExpectedException expected = ExpectedException.none(); - @Test public void testXmlStepScopeWithProxyTargetClass() throws Exception { context = new ClassPathXmlApplicationContext( @@ -114,20 +111,23 @@ public void testStepScopeWithProxyTargetClassInjected() throws Exception { public void testIntentionallyBlowUpOnMissingContextWithProxyTargetClass() throws Exception { init(StepScopeConfigurationRequiringProxyTargetClass.class); StepSynchronizationManager.release(); - expected.expect(BeanCreationException.class); - expected.expectMessage("step scope"); - SimpleHolder value = context.getBean(SimpleHolder.class); - assertEquals("STEP", value.call()); + + final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> { + SimpleHolder value = context.getBean(SimpleHolder.class); + assertEquals("STEP", value.call()); + }); + assertTrue(expectedException.getMessage().contains("step scope")); } @Test public void testIntentionallyBlowupWithForcedInterface() throws Exception { init(StepScopeConfigurationForcingInterfaceProxy.class); StepSynchronizationManager.release(); - expected.expect(BeanCreationException.class); - expected.expectMessage("step scope"); - SimpleHolder value = context.getBean(SimpleHolder.class); - assertEquals("STEP", value.call()); + final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> { + SimpleHolder value = context.getBean(SimpleHolder.class); + assertEquals("STEP", value.call()); + }); + assertTrue(expectedException.getMessage().contains("step scope")); } @Test @@ -142,11 +142,13 @@ public void testStepScopeWithDefaults() throws Exception { public void testIntentionallyBlowUpOnMissingContextWithInterface() throws Exception { init(StepScopeConfigurationWithDefaults.class); StepSynchronizationManager.release(); - expected.expect(BeanCreationException.class); - expected.expectMessage("step scope"); - @SuppressWarnings("unchecked") - Callable value = context.getBean(Callable.class); - assertEquals("STEP", value.call()); + + final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> { + @SuppressWarnings("unchecked") + Callable value = context.getBean(Callable.class); + assertEquals("STEP", value.call()); + }); + assertTrue(expectedException.getMessage().contains("step scope")); } public void init(Class... config) throws Exception { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java index fb14e3076d..4833f337be 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java @@ -16,9 +16,7 @@ package org.springframework.batch.core.jsr.configuration.xml; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.batch.core.jsr.AbstractJsrTestCase; import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.config.RuntimeBeanReference; @@ -32,9 +30,6 @@ public class JsrSplitParsingTests extends AbstractJsrTestCase { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testOneFlowInSplit() { try { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java index b64fd274f8..2af14d49d0 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java @@ -26,11 +26,10 @@ import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.aop.framework.ProxyFactory; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.ChunkListener; @@ -80,9 +79,6 @@ */ public class FaultTolerantStepFactoryBeanTests { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - protected final Log logger = LogFactory.getLog(getClass()); private FaultTolerantStepFactoryBean factory; @@ -142,25 +138,29 @@ public void setUp() throws Exception { } @Test - public void testMandatoryReader() throws Exception { + public void testMandatoryReader() { + // given factory = new FaultTolerantStepFactoryBean<>(); factory.setItemWriter(writer); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("ItemReader must be provided"); + // when + final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject); - factory.getObject(); + // then + assertEquals("ItemReader must be provided", expectedException.getMessage()); } @Test public void testMandatoryWriter() throws Exception { + // given factory = new FaultTolerantStepFactoryBean<>(); factory.setItemReader(reader); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("ItemWriter must be provided"); + // when + final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject); - factory.getObject(); + // then + assertEquals("ItemWriter must be provided", expectedException.getMessage()); } /** diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java index 3e3c3250bc..48acbf275d 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java @@ -26,10 +26,9 @@ import java.util.Collections; import java.util.List; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.ChunkListener; import org.springframework.batch.core.ItemProcessListener; @@ -66,9 +65,6 @@ */ public class SimpleStepFactoryBeanTests { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private List listened = new ArrayList<>(); private SimpleJobRepository repository = new SimpleJobRepository(new MapJobInstanceDao(), new MapJobExecutionDao(), @@ -99,25 +95,29 @@ public void testMandatoryProperties() throws Exception { } @Test - public void testMandatoryReader() throws Exception { + public void testMandatoryReader() { + // given SimpleStepFactoryBean factory = new SimpleStepFactoryBean<>(); factory.setItemWriter(writer); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("ItemReader must be provided"); + // when + final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject); - factory.getObject(); + // then + assertEquals("ItemReader must be provided", expectedException.getMessage()); } @Test - public void testMandatoryWriter() throws Exception { + public void testMandatoryWriter() { + // given SimpleStepFactoryBean factory = new SimpleStepFactoryBean<>(); factory.setItemReader(reader); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("ItemWriter must be provided"); + // when + final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject); - factory.getObject(); + // then + assertEquals("ItemWriter must be provided", expectedException.getMessage()); } @Test diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/GemfireItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/GemfireItemWriterTests.java index 589c9c1490..80e13d51b5 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/GemfireItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/GemfireItemWriterTests.java @@ -17,7 +17,7 @@ import static org.junit.Assert.fail; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.verifyNoInteractions; import java.util.ArrayList; import java.util.List; @@ -129,7 +129,7 @@ public String convert(Foo item) { @Test public void testWriteNoTransactionNoItems() throws Exception { writer.write(null); - verifyZeroInteractions(template); + verifyNoInteractions(template); } static class Foo { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java index 853e0ee505..47711d43f6 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java @@ -27,10 +27,10 @@ import org.mockito.Mock; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.times; import static org.mockito.Mockito.never; import org.mockito.junit.MockitoJUnit; @@ -130,8 +130,8 @@ public void testWriteNoTransactionWithCollection() throws Exception { public void testWriteNoTransactionNoItems() throws Exception { writer.write(null); - verifyZeroInteractions(template); - verifyZeroInteractions(bulkOperations); + verifyNoInteractions(template); + verifyNoInteractions(bulkOperations); } @Test @@ -193,8 +193,8 @@ public void testWriteTransactionFails() throws Exception { fail("Unexpected exception was thrown"); } - verifyZeroInteractions(template); - verifyZeroInteractions(bulkOperations); + verifyNoInteractions(template); + verifyNoInteractions(bulkOperations); } /** @@ -222,8 +222,8 @@ public void testWriteTransactionReadOnly() throws Exception { fail("Unexpected exception was thrown"); } - verifyZeroInteractions(template); - verifyZeroInteractions(bulkOperations); + verifyNoInteractions(template); + verifyNoInteractions(bulkOperations); } @Test diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/Neo4jItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/Neo4jItemWriterTests.java index 4c5e02a833..142e52e3b9 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/Neo4jItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/Neo4jItemWriterTests.java @@ -29,7 +29,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; public class Neo4jItemWriterTests { @@ -79,7 +79,7 @@ public void testWriteNullSession() throws Exception { writer.write(null); - verifyZeroInteractions(this.session); + verifyNoInteractions(this.session); } @Test @@ -92,7 +92,7 @@ public void testWriteNullWithSession() throws Exception { when(this.sessionFactory.openSession()).thenReturn(this.session); writer.write(null); - verifyZeroInteractions(this.session); + verifyNoInteractions(this.session); } @Test @@ -105,7 +105,7 @@ public void testWriteNoItemsWithSession() throws Exception { when(this.sessionFactory.openSession()).thenReturn(this.session); writer.write(new ArrayList<>()); - verifyZeroInteractions(this.session); + verifyNoInteractions(this.session); } @Test diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemWriterTests.java index 28fb038aec..09d925dc05 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemWriterTests.java @@ -19,7 +19,7 @@ import static org.junit.Assert.fail; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.verifyNoInteractions; import java.io.Serializable; import java.util.ArrayList; @@ -80,7 +80,7 @@ public void testWriteNoItems() throws Exception { writer.write(new ArrayList<>()); - verifyZeroInteractions(repository); + verifyNoInteractions(repository); } @Test diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderFunctionalTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderFunctionalTests.java index 82d7ad9175..142572d28e 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderFunctionalTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderFunctionalTests.java @@ -18,11 +18,8 @@ import java.math.BigDecimal; -import org.hamcrest.Matchers; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemStreamException; @@ -32,16 +29,15 @@ import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ClassPathResource; -import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Mahmoud Ben Hassine */ public abstract class JsonItemReaderFunctionalTests { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - protected abstract JsonObjectReader getJsonObjectReader(); protected abstract Class getJsonParsingException(); @@ -104,22 +100,24 @@ public void testEmptyResource() throws Exception { @Test public void testInvalidResourceFormat() { - this.expectedException.expect(ItemStreamException.class); - this.expectedException.expectMessage("Failed to initialize the reader"); - this.expectedException.expectCause(instanceOf(IllegalStateException.class)); + // given JsonItemReader itemReader = new JsonItemReaderBuilder() .jsonObjectReader(getJsonObjectReader()) .resource(new ByteArrayResource("{}, {}".getBytes())) .name("tradeJsonItemReader") .build(); - itemReader.open(new ExecutionContext()); + // when + final Exception expectedException = assertThrows(ItemStreamException.class, () -> itemReader.open(new ExecutionContext())); + + // then + assertEquals("Failed to initialize the reader", expectedException.getMessage()); + assertTrue(expectedException.getCause() instanceof IllegalStateException); } @Test - public void testInvalidResourceContent() throws Exception { - this.expectedException.expect(ParseException.class); - this.expectedException.expectCause(Matchers.instanceOf(getJsonParsingException())); + public void testInvalidResourceContent() { + // given JsonItemReader itemReader = new JsonItemReaderBuilder() .jsonObjectReader(getJsonObjectReader()) .resource(new ByteArrayResource("[{]".getBytes())) @@ -127,6 +125,11 @@ public void testInvalidResourceContent() throws Exception { .build(); itemReader.open(new ExecutionContext()); - itemReader.read(); + // when + final Exception expectedException = assertThrows(ParseException.class, itemReader::read); + + + // then + assertTrue(getJsonParsingException().isInstance(expectedException.getCause())); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderTests.java index 3cd1d4ca8b..98ae63ca00 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderTests.java @@ -18,10 +18,8 @@ import java.io.InputStream; -import org.hamcrest.Matchers; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; @@ -34,6 +32,7 @@ import org.springframework.core.io.Resource; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** @@ -42,9 +41,6 @@ @RunWith(MockitoJUnitRunner.class) public class JsonItemReaderTests { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Mock private JsonObjectReader jsonObjectReader; @@ -70,31 +66,29 @@ public void testValidation() { @Test public void testNonExistentResource() { // given - this.expectedException.expect(ItemStreamException.class); - this.expectedException.expectMessage("Failed to initialize the reader"); - this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class)); this.itemReader = new JsonItemReader<>(new NonExistentResource(), this.jsonObjectReader); // when - this.itemReader.open(new ExecutionContext()); + final Exception expectedException = Assert.assertThrows(ItemStreamException.class, + () -> this.itemReader.open(new ExecutionContext())); // then - // expected exception + assertEquals("Failed to initialize the reader", expectedException.getMessage()); + assertTrue(expectedException.getCause() instanceof IllegalStateException); } @Test public void testNonReadableResource() { // given - this.expectedException.expect(ItemStreamException.class); - this.expectedException.expectMessage("Failed to initialize the reader"); - this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class)); this.itemReader = new JsonItemReader<>(new NonReadableResource(), this.jsonObjectReader); // when - this.itemReader.open(new ExecutionContext()); + final Exception expectedException = Assert.assertThrows(ItemStreamException.class, + () -> this.itemReader.open(new ExecutionContext())); // then - // expected exception + assertEquals("Failed to initialize the reader", expectedException.getMessage()); + assertTrue(expectedException.getCause() instanceof IllegalStateException); } @Test diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/kafka/builder/KafkaItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/kafka/builder/KafkaItemReaderBuilderTests.java index a178783a48..71310d3fe1 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/kafka/builder/KafkaItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/kafka/builder/KafkaItemReaderBuilderTests.java @@ -26,14 +26,14 @@ import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.serialization.StringDeserializer; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.batch.item.kafka.KafkaItemReader; import org.springframework.test.util.ReflectionTestUtils; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -45,9 +45,6 @@ */ public class KafkaItemReaderBuilderTests { - @Rule - public ExpectedException thrown = ExpectedException.none(); - private Properties consumerProperties; @Before @@ -63,13 +60,16 @@ public void setUp() throws Exception { @Test public void testNullConsumerProperties() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Consumer properties must not be null"); - - new KafkaItemReaderBuilder<>() + // given + final KafkaItemReaderBuilder builder = new KafkaItemReaderBuilder<>() .name("kafkaItemReader") - .consumerProperties(null) - .build(); + .consumerProperties(null); + + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertThat(expectedException).hasMessage("Consumer properties must not be null"); } @Test @@ -133,78 +133,96 @@ public void testConsumerPropertiesValidation() { @Test public void testNullTopicName() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Topic name must not be null or empty"); - - new KafkaItemReaderBuilder<>() + // given + final KafkaItemReaderBuilder builder = new KafkaItemReaderBuilder<>() .name("kafkaItemReader") .consumerProperties(this.consumerProperties) - .topic(null) - .build(); + .topic(null); + + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertThat(expectedException).hasMessage("Topic name must not be null or empty"); } @Test public void testEmptyTopicName() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Topic name must not be null or empty"); - - new KafkaItemReaderBuilder<>() + // given + final KafkaItemReaderBuilder builder = new KafkaItemReaderBuilder<>() .name("kafkaItemReader") .consumerProperties(this.consumerProperties) - .topic("") - .build(); + .topic(""); + + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertThat(expectedException).hasMessage("Topic name must not be null or empty"); } @Test public void testNullPollTimeout() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("pollTimeout must not be null"); - - new KafkaItemReaderBuilder<>() + // given + final KafkaItemReaderBuilder builder = new KafkaItemReaderBuilder<>() .name("kafkaItemReader") .consumerProperties(this.consumerProperties) .topic("test") - .pollTimeout(null) - .build(); + .pollTimeout(null); + + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertThat(expectedException).hasMessage("pollTimeout must not be null"); } @Test public void testNegativePollTimeout() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("pollTimeout must not be negative"); - - new KafkaItemReaderBuilder<>() + // given + final KafkaItemReaderBuilder builder = new KafkaItemReaderBuilder<>() .name("kafkaItemReader") .consumerProperties(this.consumerProperties) .topic("test") - .pollTimeout(Duration.ofSeconds(-1)) - .build(); + .pollTimeout(Duration.ofSeconds(-1)); + + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertThat(expectedException).hasMessage("pollTimeout must not be negative"); } @Test public void testZeroPollTimeout() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("pollTimeout must not be zero"); - - new KafkaItemReaderBuilder<>() + // given + final KafkaItemReaderBuilder builder = new KafkaItemReaderBuilder<>() .name("kafkaItemReader") .consumerProperties(this.consumerProperties) .topic("test") - .pollTimeout(Duration.ZERO) - .build(); + .pollTimeout(Duration.ZERO); + + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertThat(expectedException).hasMessage("pollTimeout must not be zero"); } @Test public void testEmptyPartitions() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("At least one partition must be provided"); - - new KafkaItemReaderBuilder<>() + // given + final KafkaItemReaderBuilder builder = new KafkaItemReaderBuilder<>() .name("kafkaItemReader") .consumerProperties(this.consumerProperties) .topic("test") - .pollTimeout(Duration.ofSeconds(10)) - .build(); + .pollTimeout(Duration.ofSeconds(10)); + + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertThat(expectedException).hasMessage("At least one partition must be provided"); } @Test diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/kafka/builder/KafkaItemWriterBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/kafka/builder/KafkaItemWriterBuilderTests.java index 79fb0a0542..a0047faac2 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/kafka/builder/KafkaItemWriterBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/kafka/builder/KafkaItemWriterBuilderTests.java @@ -16,10 +16,10 @@ package org.springframework.batch.item.kafka.builder; +import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; @@ -28,6 +28,7 @@ import org.springframework.kafka.core.KafkaTemplate; import org.springframework.test.util.ReflectionTestUtils; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -40,9 +41,6 @@ public class KafkaItemWriterBuilderTests { @Rule public MockitoRule rule = MockitoJUnit.rule().silent(); - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Mock private KafkaTemplate kafkaTemplate; @@ -55,18 +53,26 @@ public void setUp() { @Test public void testNullKafkaTemplate() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("kafkaTemplate is required."); + // given + final KafkaItemWriterBuilder builder = new KafkaItemWriterBuilder().itemKeyMapper(this.itemKeyMapper); + + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); - new KafkaItemWriterBuilder().itemKeyMapper(this.itemKeyMapper).build(); + // then + assertThat(expectedException).hasMessage("kafkaTemplate is required."); } @Test public void testNullItemKeyMapper() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("itemKeyMapper is required."); + // given + final KafkaItemWriterBuilder builder = new KafkaItemWriterBuilder().kafkaTemplate(this.kafkaTemplate); - new KafkaItemWriterBuilder().kafkaTemplate(this.kafkaTemplate).build(); + // when + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertThat(expectedException).hasMessage("itemKeyMapper is required."); } @Test diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractSynchronizedItemStreamWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractSynchronizedItemStreamWriterTests.java index 89437bf477..1d70d79475 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractSynchronizedItemStreamWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractSynchronizedItemStreamWriterTests.java @@ -24,7 +24,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; @@ -43,9 +42,6 @@ public abstract class AbstractSynchronizedItemStreamWriterTests { @Rule public MockitoRule rule = MockitoJUnit.rule().silent(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Mock protected ItemStreamWriter delegate; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/SynchronizedItemStreamWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/SynchronizedItemStreamWriterTests.java index dca30c1c0e..9871e3e7ef 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/SynchronizedItemStreamWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/SynchronizedItemStreamWriterTests.java @@ -18,6 +18,9 @@ import org.junit.Test; import org.springframework.beans.factory.InitializingBean; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * * @author Dimitrios Liapis @@ -34,9 +37,9 @@ protected SynchronizedItemStreamWriter createNewSynchronizedItemStreamWr } @Test - public void testDelegateIsNotNullWhenPropertiesSet() throws Exception { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("A delegate item writer is required"); - ((InitializingBean) new SynchronizedItemStreamWriter<>()).afterPropertiesSet(); + public void testDelegateIsNotNullWhenPropertiesSet() { + final Exception expectedException = assertThrows(IllegalArgumentException.class, + () -> ((InitializingBean) new SynchronizedItemStreamWriter<>()).afterPropertiesSet()); + assertEquals("A delegate item writer is required", expectedException.getMessage()); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilderTests.java index 1be4699ddd..bffc4e1aae 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilderTests.java @@ -19,6 +19,9 @@ import org.springframework.batch.item.support.AbstractSynchronizedItemStreamWriterTests; import org.springframework.batch.item.support.SynchronizedItemStreamWriter; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * * @author Dimitrios Liapis @@ -36,8 +39,13 @@ protected SynchronizedItemStreamWriter createNewSynchronizedItemStreamWr @Test public void testBuilderDelegateIsNotNull() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("A delegate item writer is required"); - new SynchronizedItemStreamWriterBuilder<>().build(); + // given + final SynchronizedItemStreamWriterBuilder builder = new SynchronizedItemStreamWriterBuilder<>(); + + // when + final Exception expectedException = assertThrows(IllegalArgumentException.class, builder::build); + + // then + assertEquals("A delegate item writer is required", expectedException.getMessage()); } } diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTest.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTest.java index 0304cbe262..78b72684da 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTest.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTest.java @@ -20,9 +20,7 @@ import java.util.List; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.batch.core.ChunkListener; @@ -62,6 +60,7 @@ import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.interceptor.DefaultTransactionAttribute; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.any; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; @@ -75,9 +74,6 @@ @ContextConfiguration(classes = {RemoteChunkingManagerStepBuilderTest.BatchConfiguration.class}) public class RemoteChunkingManagerStepBuilderTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Autowired private JobRepository jobRepository; @Autowired @@ -90,76 +86,66 @@ public class RemoteChunkingManagerStepBuilderTest { @Test public void inputChannelMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("inputChannel must not be null"); + final RemoteChunkingManagerStepBuilder builder = new RemoteChunkingManagerStepBuilder("step") + .inputChannel(null); // when - TaskletStep step = new RemoteChunkingManagerStepBuilder("step") - .inputChannel(null) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("inputChannel must not be null"); } @Test public void outputChannelMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("outputChannel must not be null"); + final RemoteChunkingManagerStepBuilder builder = new RemoteChunkingManagerStepBuilder("step") + .outputChannel(null); // when - TaskletStep step = new RemoteChunkingManagerStepBuilder("step") - .outputChannel(null) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("outputChannel must not be null"); } @Test public void messagingTemplateMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("messagingTemplate must not be null"); + final RemoteChunkingManagerStepBuilder builder = new RemoteChunkingManagerStepBuilder("step") + .messagingTemplate(null); // when - TaskletStep step = new RemoteChunkingManagerStepBuilder("step") - .messagingTemplate(null) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("messagingTemplate must not be null"); } @Test public void maxWaitTimeoutsMustBeGreaterThanZero() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("maxWaitTimeouts must be greater than zero"); + final RemoteChunkingManagerStepBuilder builder = new RemoteChunkingManagerStepBuilder("step") + .maxWaitTimeouts(-1); // when - TaskletStep step = new RemoteChunkingManagerStepBuilder("step") - .maxWaitTimeouts(-1) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("maxWaitTimeouts must be greater than zero"); } @Test public void throttleLimitMustNotBeGreaterThanZero() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("throttleLimit must be greater than zero"); + final RemoteChunkingManagerStepBuilder builder = new RemoteChunkingManagerStepBuilder("step") + .throttleLimit(-1L); // when - TaskletStep step = new RemoteChunkingManagerStepBuilder("step") - .throttleLimit(-1L) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("throttleLimit must be greater than zero"); } @Test @@ -167,14 +153,11 @@ public void testMandatoryInputChannel() { // given RemoteChunkingManagerStepBuilder builder = new RemoteChunkingManagerStepBuilder<>("step"); - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("An InputChannel must be provided"); - // when - TaskletStep step = builder.build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("An InputChannel must be provided"); } @Test @@ -185,37 +168,32 @@ public void eitherOutputChannelOrMessagingTemplateMustBeProvided() { .outputChannel(new DirectChannel()) .messagingTemplate(new MessagingTemplate()); - this.expectedException.expect(IllegalStateException.class); - this.expectedException.expectMessage("You must specify either an outputChannel or a messagingTemplate but not both."); - // when - TaskletStep step = builder.build(); + final Exception expectedException = Assert.assertThrows(IllegalStateException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("You must specify either an outputChannel or a messagingTemplate but not both."); } @Test public void testUnsupportedOperationExceptionWhenSpecifyingAnItemWriter() { // given - this.expectedException.expect(UnsupportedOperationException.class); - this.expectedException.expectMessage("When configuring a manager " + - "step for remote chunking, the item writer will be automatically " + - "set to an instance of ChunkMessageChannelItemWriter. " + - "The item writer must not be provided in this case."); - - // when - TaskletStep step = new RemoteChunkingManagerStepBuilder("step") + final RemoteChunkingManagerStepBuilder builder = new RemoteChunkingManagerStepBuilder("step") .reader(this.itemReader) .writer(items -> { }) .repository(this.jobRepository) .transactionManager(this.transactionManager) .inputChannel(this.inputChannel) - .outputChannel(this.outputChannel) - .build(); + .outputChannel(this.outputChannel); + + // when + final Exception expectedException = Assert.assertThrows(UnsupportedOperationException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("When configuring a manager " + + "step for remote chunking, the item writer will be automatically " + + "set to an instance of ChunkMessageChannelItemWriter. " + + "The item writer must not be provided in this case."); } @Test diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingWorkerBuilderTest.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingWorkerBuilderTest.java index e21fee18c7..a2815e883b 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingWorkerBuilderTest.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingWorkerBuilderTest.java @@ -16,9 +16,7 @@ package org.springframework.batch.integration.chunk; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.ItemWriter; @@ -26,75 +24,66 @@ import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.dsl.IntegrationFlow; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author Mahmoud Ben Hassine */ public class RemoteChunkingWorkerBuilderTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private ItemProcessor itemProcessor = new PassThroughItemProcessor<>(); private ItemWriter itemWriter = items -> { }; @Test public void itemProcessorMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("itemProcessor must not be null"); + final RemoteChunkingWorkerBuilder builder = new RemoteChunkingWorkerBuilder() + .itemProcessor(null); // when - IntegrationFlow integrationFlow = new RemoteChunkingWorkerBuilder() - .itemProcessor(null) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("itemProcessor must not be null"); } @Test public void itemWriterMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("itemWriter must not be null"); + final RemoteChunkingWorkerBuilder builder = new RemoteChunkingWorkerBuilder() + .itemWriter(null); // when - IntegrationFlow integrationFlow = new RemoteChunkingWorkerBuilder() - .itemWriter(null) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("itemWriter must not be null"); } @Test public void inputChannelMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("inputChannel must not be null"); + final RemoteChunkingWorkerBuilder builder = new RemoteChunkingWorkerBuilder() + .inputChannel(null); // when - IntegrationFlow integrationFlow = new RemoteChunkingWorkerBuilder() - .inputChannel(null) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("inputChannel must not be null"); } @Test public void outputChannelMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("outputChannel must not be null"); + final RemoteChunkingWorkerBuilder builder = new RemoteChunkingWorkerBuilder() + .outputChannel(null); // when - IntegrationFlow integrationFlow = new RemoteChunkingWorkerBuilder() - .outputChannel(null) - .build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("outputChannel must not be null"); } @Test @@ -102,14 +91,11 @@ public void testMandatoryItemWriter() { // given RemoteChunkingWorkerBuilder builder = new RemoteChunkingWorkerBuilder<>(); - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("An ItemWriter must be provided"); - // when - builder.build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("An ItemWriter must be provided"); } @Test @@ -118,14 +104,11 @@ public void testMandatoryInputChannel() { RemoteChunkingWorkerBuilder builder = new RemoteChunkingWorkerBuilder() .itemWriter(items -> { }); - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("An InputChannel must be provided"); - // when - builder.build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("An InputChannel must be provided"); } @Test @@ -135,14 +118,12 @@ public void testMandatoryOutputChannel() { .itemWriter(items -> { }) .inputChannel(new DirectChannel()); - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("An OutputChannel must be provided"); // when - builder.build(); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("An OutputChannel must be provided"); } @Test diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/RemotePartitioningMasterStepBuilderTests.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/RemotePartitioningMasterStepBuilderTests.java index b2b5d0d682..8e4587d759 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/RemotePartitioningMasterStepBuilderTests.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/RemotePartitioningMasterStepBuilderTests.java @@ -17,9 +17,7 @@ package org.springframework.batch.integration.partition; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -37,6 +35,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.util.ReflectionTestUtils.getField; /** @@ -46,75 +45,72 @@ @ContextConfiguration(classes = {RemotePartitioningMasterStepBuilderTests.BatchConfiguration.class}) public class RemotePartitioningMasterStepBuilderTests { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Autowired private JobRepository jobRepository; @Test public void inputChannelMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("inputChannel must not be null"); + final RemotePartitioningMasterStepBuilder builder = new RemotePartitioningMasterStepBuilder("step"); // when - new RemotePartitioningMasterStepBuilder("step").inputChannel(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, + () -> builder.inputChannel(null)); // then - // expected exception + assertThat(expectedException).hasMessage("inputChannel must not be null"); } @Test public void outputChannelMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("outputChannel must not be null"); + final RemotePartitioningMasterStepBuilder builder = new RemotePartitioningMasterStepBuilder("step"); // when - new RemotePartitioningMasterStepBuilder("step").outputChannel(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, + () -> builder.outputChannel(null)); // then - // expected exception + assertThat(expectedException).hasMessage("outputChannel must not be null"); } @Test public void messagingTemplateMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("messagingTemplate must not be null"); + final RemotePartitioningMasterStepBuilder builder = new RemotePartitioningMasterStepBuilder("step"); // when - new RemotePartitioningMasterStepBuilder("step").messagingTemplate(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, + () -> builder.messagingTemplate(null)); // then - // expected exception + assertThat(expectedException).hasMessage("messagingTemplate must not be null"); } @Test public void jobExplorerMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("jobExplorer must not be null"); + final RemotePartitioningMasterStepBuilder builder = new RemotePartitioningMasterStepBuilder("step"); // when - new RemotePartitioningMasterStepBuilder("step").jobExplorer(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, + () -> builder.jobExplorer(null)); // then - // expected exception + assertThat(expectedException).hasMessage("jobExplorer must not be null"); } @Test public void pollIntervalMustBeGreaterThanZero() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("The poll interval must be greater than zero"); + final RemotePartitioningMasterStepBuilder builder = new RemotePartitioningMasterStepBuilder("step"); // when - new RemotePartitioningMasterStepBuilder("step").pollInterval(-1); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, + () -> builder.pollInterval(-1)); // then - // expected exception + assertThat(expectedException).hasMessage("The poll interval must be greater than zero"); } @Test @@ -124,32 +120,30 @@ public void eitherOutputChannelOrMessagingTemplateMustBeProvided() { .outputChannel(new DirectChannel()) .messagingTemplate(new MessagingTemplate()); - this.expectedException.expect(IllegalStateException.class); - this.expectedException.expectMessage("You must specify either an outputChannel or a messagingTemplate but not both."); - // when - Step step = builder.build(); + final Exception expectedException = Assert.assertThrows(IllegalStateException.class, + builder::build); // then - // expected exception + assertThat(expectedException).hasMessage("You must specify either an outputChannel or a messagingTemplate but not both."); } @Test public void testUnsupportedOperationExceptionWhenSpecifyingPartitionHandler() { // given PartitionHandler partitionHandler = Mockito.mock(PartitionHandler.class); - this.expectedException.expect(UnsupportedOperationException.class); - this.expectedException.expectMessage("When configuring a master step " + - "for remote partitioning using the RemotePartitioningMasterStepBuilder, " + - "the partition handler will be automatically set to an instance " + - "of MessageChannelPartitionHandler. The partition handler must " + - "not be provided in this case."); + final RemotePartitioningMasterStepBuilder builder = new RemotePartitioningMasterStepBuilder("step"); // when - new RemotePartitioningMasterStepBuilder("step").partitionHandler(partitionHandler); + final Exception expectedException = Assert.assertThrows(UnsupportedOperationException.class, + () -> builder.partitionHandler(partitionHandler)); // then - // expected exception + assertThat(expectedException).hasMessage("When configuring a master step " + + "for remote partitioning using the RemotePartitioningMasterStepBuilder, " + + "the partition handler will be automatically set to an instance " + + "of MessageChannelPartitionHandler. The partition handler must " + + "not be provided in this case."); } @Test diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/RemotePartitioningWorkerStepBuilderTests.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/RemotePartitioningWorkerStepBuilderTests.java index 316f99dbab..ed26752100 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/RemotePartitioningWorkerStepBuilderTests.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/RemotePartitioningWorkerStepBuilderTests.java @@ -16,117 +16,107 @@ package org.springframework.batch.integration.partition; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.integration.channel.DirectChannel; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author Mahmoud Ben Hassine */ public class RemotePartitioningWorkerStepBuilderTests { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Mock private Tasklet tasklet; @Test public void inputChannelMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("inputChannel must not be null"); + final RemotePartitioningWorkerStepBuilder builder = new RemotePartitioningWorkerStepBuilder("step"); // when - new RemotePartitioningWorkerStepBuilder("step").inputChannel(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, () -> builder.inputChannel(null)); // then - // expected exception + assertThat(expectedException).hasMessage("inputChannel must not be null"); } @Test public void outputChannelMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("outputChannel must not be null"); + final RemotePartitioningWorkerStepBuilder builder = new RemotePartitioningWorkerStepBuilder("step"); // when - new RemotePartitioningWorkerStepBuilder("step").outputChannel(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, () -> builder.outputChannel(null)); // then - // expected exception + assertThat(expectedException).hasMessage("outputChannel must not be null"); } @Test public void jobExplorerMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("jobExplorer must not be null"); + final RemotePartitioningWorkerStepBuilder builder = new RemotePartitioningWorkerStepBuilder("step"); // when - new RemotePartitioningWorkerStepBuilder("step").jobExplorer(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, () -> builder.jobExplorer(null)); // then - // expected exception + assertThat(expectedException).hasMessage("jobExplorer must not be null"); } @Test public void stepLocatorMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("stepLocator must not be null"); + final RemotePartitioningWorkerStepBuilder builder = new RemotePartitioningWorkerStepBuilder("step"); // when - new RemotePartitioningWorkerStepBuilder("step").stepLocator(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, () -> builder.stepLocator(null)); // then - // expected exception + assertThat(expectedException).hasMessage("stepLocator must not be null"); } @Test public void beanFactoryMustNotBeNull() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("beanFactory must not be null"); + final RemotePartitioningWorkerStepBuilder builder = new RemotePartitioningWorkerStepBuilder("step"); // when - new RemotePartitioningWorkerStepBuilder("step").beanFactory(null); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, () -> builder.beanFactory(null)); // then - // expected exception + assertThat(expectedException).hasMessage("beanFactory must not be null"); } @Test public void testMandatoryInputChannel() { // given - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("An InputChannel must be provided"); + final RemotePartitioningWorkerStepBuilder builder = new RemotePartitioningWorkerStepBuilder("step"); // when - new RemotePartitioningWorkerStepBuilder("step").tasklet(this.tasklet); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, () -> builder.tasklet(this.tasklet)); // then - // expected exception + assertThat(expectedException).hasMessage("An InputChannel must be provided"); } @Test public void testMandatoryJobExplorer() { // given DirectChannel inputChannel = new DirectChannel(); - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("A JobExplorer must be provided"); + final RemotePartitioningWorkerStepBuilder builder = new RemotePartitioningWorkerStepBuilder("step") + .inputChannel(inputChannel); // when - new RemotePartitioningWorkerStepBuilder("step") - .inputChannel(inputChannel) - .tasklet(this.tasklet); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, () -> builder.tasklet(this.tasklet)); // then - // expected exception + assertThat(expectedException).hasMessage("A JobExplorer must be provided"); } } diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTest.java b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTest.java index 29ec878976..c47d65a3b2 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTest.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTest.java @@ -16,23 +16,20 @@ package org.springframework.batch.test.context; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.Mockito; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.springframework.test.context.MergedContextConfiguration; +import static org.junit.jupiter.api.Assertions.assertEquals; + /** * @author Mahmoud Ben Hassine */ public class BatchTestContextCustomizerTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private BatchTestContextCustomizer contextCustomizer = new BatchTestContextCustomizer(); @Test @@ -54,13 +51,12 @@ public void testCustomizeContext_whenBeanFactoryIsNotAnInstanceOfBeanDefinitionR // given ConfigurableApplicationContext context = Mockito.mock(ConfigurableApplicationContext.class); MergedContextConfiguration mergedConfig = Mockito.mock(MergedContextConfiguration.class); - this.expectedException.expect(IllegalArgumentException.class); - this.expectedException.expectMessage("The bean factory must be an instance of BeanDefinitionRegistry"); // when - this.contextCustomizer.customizeContext(context, mergedConfig); + final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, + () -> this.contextCustomizer.customizeContext(context, mergedConfig)); // then - // expected exception + assertEquals("The bean factory must be an instance of BeanDefinitionRegistry", expectedException.getMessage()); } }