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
This commit is contained in:
Sebastiano Valle
2021-07-09 17:15:57 +02:00
committed by Mahmoud Ben Hassine
parent 7242f8fe77
commit 24422b5163
22 changed files with 329 additions and 369 deletions

View File

@@ -17,7 +17,7 @@ package org.springframework.batch.item.data;
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 class GemfireItemWriterTests {
@Test
public void testWriteNoTransactionNoItems() throws Exception {
writer.write(null);
verifyZeroInteractions(template);
verifyNoInteractions(template);
}
static class Foo {

View File

@@ -27,10 +27,10 @@ import org.junit.Test;
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 class MongoItemWriterTests {
public void testWriteNoTransactionNoItems() throws Exception {
writer.write(null);
verifyZeroInteractions(template);
verifyZeroInteractions(bulkOperations);
verifyNoInteractions(template);
verifyNoInteractions(bulkOperations);
}
@Test
@@ -193,8 +193,8 @@ public class MongoItemWriterTests {
fail("Unexpected exception was thrown");
}
verifyZeroInteractions(template);
verifyZeroInteractions(bulkOperations);
verifyNoInteractions(template);
verifyNoInteractions(bulkOperations);
}
/**
@@ -222,8 +222,8 @@ public class MongoItemWriterTests {
fail("Unexpected exception was thrown");
}
verifyZeroInteractions(template);
verifyZeroInteractions(bulkOperations);
verifyNoInteractions(template);
verifyNoInteractions(bulkOperations);
}
@Test

View File

@@ -29,7 +29,7 @@ import org.neo4j.ogm.session.SessionFactory;
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 class Neo4jItemWriterTests {
writer.write(null);
verifyZeroInteractions(this.session);
verifyNoInteractions(this.session);
}
@Test
@@ -92,7 +92,7 @@ public class Neo4jItemWriterTests {
when(this.sessionFactory.openSession()).thenReturn(this.session);
writer.write(null);
verifyZeroInteractions(this.session);
verifyNoInteractions(this.session);
}
@Test
@@ -105,7 +105,7 @@ public class Neo4jItemWriterTests {
when(this.sessionFactory.openSession()).thenReturn(this.session);
writer.write(new ArrayList<>());
verifyZeroInteractions(this.session);
verifyNoInteractions(this.session);
}
@Test

View File

@@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals;
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 class RepositoryItemWriterTests {
writer.write(new ArrayList<>());
verifyZeroInteractions(repository);
verifyNoInteractions(repository);
}
@Test

View File

@@ -18,11 +18,8 @@ package org.springframework.batch.item.json;
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.batch.item.json.domain.Trade;
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<Trade> getJsonObjectReader();
protected abstract Class<? extends Exception> getJsonParsingException();
@@ -104,22 +100,24 @@ public abstract class JsonItemReaderFunctionalTests {
@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<Trade> itemReader = new JsonItemReaderBuilder<Trade>()
.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<Trade> itemReader = new JsonItemReaderBuilder<Trade>()
.jsonObjectReader(getJsonObjectReader())
.resource(new ByteArrayResource("[{]".getBytes()))
@@ -127,6 +125,11 @@ public abstract class JsonItemReaderFunctionalTests {
.build();
itemReader.open(new ExecutionContext());
itemReader.read();
// when
final Exception expectedException = assertThrows(ParseException.class, itemReader::read);
// then
assertTrue(getJsonParsingException().isInstance(expectedException.getCause()));
}
}

View File

@@ -18,10 +18,8 @@ package org.springframework.batch.item.json;
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.ByteArrayResource;
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 @@ import static org.junit.Assert.fail;
@RunWith(MockitoJUnitRunner.class)
public class JsonItemReaderTests {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Mock
private JsonObjectReader<String> jsonObjectReader;
@@ -70,31 +66,29 @@ public class JsonItemReaderTests {
@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

View File

@@ -26,14 +26,14 @@ import java.util.Properties;
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 @@ import static org.junit.Assert.fail;
*/
public class KafkaItemReaderBuilderTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private Properties consumerProperties;
@Before
@@ -63,13 +60,16 @@ public class KafkaItemReaderBuilderTests {
@Test
public void testNullConsumerProperties() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Consumer properties must not be null");
new KafkaItemReaderBuilder<>()
// given
final KafkaItemReaderBuilder<Object, Object> 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 class KafkaItemReaderBuilderTests {
@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<Object, Object> 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<Object, Object> 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<Object, Object> 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<Object, Object> 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<Object, Object> 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<Object, Object> 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

View File

@@ -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.core.convert.converter.Converter;
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<String, String> kafkaTemplate;
@@ -55,18 +53,26 @@ public class KafkaItemWriterBuilderTests {
@Test
public void testNullKafkaTemplate() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("kafkaTemplate is required.");
// given
final KafkaItemWriterBuilder<String, String> builder = new KafkaItemWriterBuilder<String, String>().itemKeyMapper(this.itemKeyMapper);
new KafkaItemWriterBuilder<String, String>().itemKeyMapper(this.itemKeyMapper).build();
// when
final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::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<String, String> builder = new KafkaItemWriterBuilder<String, String>().kafkaTemplate(this.kafkaTemplate);
new KafkaItemWriterBuilder<String, String>().kafkaTemplate(this.kafkaTemplate).build();
// when
final Exception expectedException = Assert.assertThrows(IllegalArgumentException.class, builder::build);
// then
assertThat(expectedException).hasMessage("itemKeyMapper is required.");
}
@Test

View File

@@ -24,7 +24,6 @@ import java.util.List;
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<Object> delegate;

View File

@@ -18,6 +18,9 @@ package org.springframework.batch.item.support;
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 @@ public class SynchronizedItemStreamWriterTests extends AbstractSynchronizedItemS
}
@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());
}
}

View File

@@ -19,6 +19,9 @@ import org.junit.Test;
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 @@ public class SynchronizedItemStreamWriterBuilderTests extends AbstractSynchroniz
@Test
public void testBuilderDelegateIsNotNull() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("A delegate item writer is required");
new SynchronizedItemStreamWriterBuilder<>().build();
// given
final SynchronizedItemStreamWriterBuilder<Object> builder = new SynchronizedItemStreamWriterBuilder<>();
// when
final Exception expectedException = assertThrows(IllegalArgumentException.class, builder::build);
// then
assertEquals("A delegate item writer is required", expectedException.getMessage());
}
}