Skip to content

Commit

Permalink
Remove some deprecated APIs from tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snate authored and fmbenhassine committed Aug 10, 2021
1 parent 7242f8f commit 24422b5
Show file tree
Hide file tree
Showing 22 changed files with 329 additions and 369 deletions.
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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<String> value = context.getBean(Callable.class);
assertEquals("JOB", value.call());
final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> {
@SuppressWarnings("unchecked")
Callable<String> value = context.getBean(Callable.class);
assertEquals("JOB", value.call());
});
assertTrue(expectedException.getMessage().contains("job scope"));
}

public void init(Class<?>... config) throws Exception {
Expand Down
Expand Up @@ -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;
Expand All @@ -42,6 +41,7 @@
import java.util.concurrent.Callable;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author Dave Syer
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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<String> value = context.getBean(Callable.class);
assertEquals("STEP", value.call());

final Exception expectedException = Assert.assertThrows(BeanCreationException.class, () -> {
@SuppressWarnings("unchecked")
Callable<String> value = context.getBean(Callable.class);
assertEquals("STEP", value.call());
});
assertTrue(expectedException.getMessage().contains("step scope"));
}

public void init(Class<?>... config) throws Exception {
Expand Down
Expand Up @@ -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;
Expand All @@ -32,9 +30,6 @@

public class JsrSplitParsingTests extends AbstractJsrTestCase {

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void testOneFlowInSplit() {
try {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -80,9 +79,6 @@
*/
public class FaultTolerantStepFactoryBeanTests {

@Rule
public ExpectedException expectedException = ExpectedException.none();

protected final Log logger = LogFactory.getLog(getClass());

private FaultTolerantStepFactoryBean<String, String> factory;
Expand Down Expand Up @@ -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());
}

/**
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -66,9 +65,6 @@
*/
public class SimpleStepFactoryBeanTests {

@Rule
public ExpectedException expectedException = ExpectedException.none();

private List<Exception> listened = new ArrayList<>();

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

@Test
public void testMandatoryReader() throws Exception {
public void testMandatoryReader() {
// given
SimpleStepFactoryBean<String, String> 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<String, String> 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
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 24422b5

Please sign in to comment.