BAEL-3298 Using @MockBean
This commit is contained in:
		
							parent
							
								
									db549a4f8f
								
							
						
					
					
						commit
						65be6994fa
					
				| @ -36,7 +36,6 @@ import java.text.ParseException; | |||||||
| public class SpringBatchRetryConfig { | public class SpringBatchRetryConfig { | ||||||
|      |      | ||||||
|     private static final String[] tokens = { "username", "userid", "transactiondate", "amount" }; |     private static final String[] tokens = { "username", "userid", "transactiondate", "amount" }; | ||||||
| 
 |  | ||||||
|     private static final int TWO_SECONDS = 2000; |     private static final int TWO_SECONDS = 2000; | ||||||
| 
 | 
 | ||||||
|     @Autowired |     @Autowired | ||||||
| @ -66,7 +65,9 @@ public class SpringBatchRetryConfig { | |||||||
| 
 | 
 | ||||||
|     @Bean |     @Bean | ||||||
|     public CloseableHttpClient closeableHttpClient() { |     public CloseableHttpClient closeableHttpClient() { | ||||||
|         final RequestConfig config = RequestConfig.custom().setConnectTimeout(TWO_SECONDS).build(); |         final RequestConfig config = RequestConfig.custom() | ||||||
|  |           .setConnectTimeout(TWO_SECONDS) | ||||||
|  |           .build(); | ||||||
|         return HttpClientBuilder.create().setDefaultRequestConfig(config).build(); |         return HttpClientBuilder.create().setDefaultRequestConfig(config).build(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -92,8 +93,8 @@ public class SpringBatchRetryConfig { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Bean |     @Bean | ||||||
|     public Step retryStep(@Qualifier("retryItemProcessor") ItemProcessor<Transaction, Transaction> processor |     public Step retryStep(@Qualifier("retryItemProcessor") ItemProcessor<Transaction, Transaction> processor, | ||||||
|       , ItemWriter<Transaction> writer) throws ParseException { |       ItemWriter<Transaction> writer) throws ParseException { | ||||||
|         return stepBuilderFactory.get("retryStep") |         return stepBuilderFactory.get("retryStep") | ||||||
|           .<Transaction, Transaction>chunk(10) |           .<Transaction, Transaction>chunk(10) | ||||||
|           .reader(itemReader(inputCsv)) |           .reader(itemReader(inputCsv)) | ||||||
|  | |||||||
| @ -4,13 +4,8 @@ import org.apache.http.client.methods.CloseableHttpResponse; | |||||||
| import org.apache.http.conn.ConnectTimeoutException; | import org.apache.http.conn.ConnectTimeoutException; | ||||||
| import org.apache.http.entity.StringEntity; | import org.apache.http.entity.StringEntity; | ||||||
| import org.apache.http.impl.client.CloseableHttpClient; | import org.apache.http.impl.client.CloseableHttpClient; | ||||||
| import org.baeldung.batch.service.RetryItemProcessor; |  | ||||||
| import org.junit.Before; |  | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||||
| import org.mockito.InjectMocks; |  | ||||||
| import org.mockito.Mock; |  | ||||||
| import org.mockito.MockitoAnnotations; |  | ||||||
| import org.springframework.batch.core.ExitStatus; | import org.springframework.batch.core.ExitStatus; | ||||||
| import org.springframework.batch.core.JobExecution; | import org.springframework.batch.core.JobExecution; | ||||||
| import org.springframework.batch.core.JobInstance; | import org.springframework.batch.core.JobInstance; | ||||||
| @ -21,14 +16,10 @@ import org.springframework.batch.test.JobLauncherTestUtils; | |||||||
| import org.springframework.batch.test.context.SpringBatchTest; | import org.springframework.batch.test.context.SpringBatchTest; | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||||||
|  | import org.springframework.boot.test.mock.mockito.MockBean; | ||||||
| import org.springframework.core.io.FileSystemResource; | import org.springframework.core.io.FileSystemResource; | ||||||
| import org.springframework.test.annotation.DirtiesContext; |  | ||||||
| import org.springframework.test.annotation.DirtiesContext.ClassMode; |  | ||||||
| import org.springframework.test.context.ContextConfiguration; | import org.springframework.test.context.ContextConfiguration; | ||||||
| import org.springframework.test.context.TestExecutionListeners; |  | ||||||
| import org.springframework.test.context.junit4.SpringRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; |  | ||||||
| import org.springframework.test.context.support.DirtiesContextTestExecutionListener; |  | ||||||
| 
 | 
 | ||||||
| import static org.hamcrest.CoreMatchers.containsString; | import static org.hamcrest.CoreMatchers.containsString; | ||||||
| import static org.hamcrest.Matchers.is; | import static org.hamcrest.Matchers.is; | ||||||
| @ -40,34 +31,24 @@ import static org.mockito.Mockito.when; | |||||||
| @SpringBatchTest | @SpringBatchTest | ||||||
| @EnableAutoConfiguration | @EnableAutoConfiguration | ||||||
| @ContextConfiguration(classes = { SpringBatchRetryConfig.class }) | @ContextConfiguration(classes = { SpringBatchRetryConfig.class }) | ||||||
| @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) |  | ||||||
| @DirtiesContext(classMode = ClassMode.AFTER_CLASS) |  | ||||||
| public class SpringBatchRetryIntegrationTest { | public class SpringBatchRetryIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     private static final String TEST_OUTPUT = "xml/retryOutput.xml"; |     private static final String TEST_OUTPUT = "xml/retryOutput.xml"; | ||||||
| 
 |  | ||||||
|     private static final String EXPECTED_OUTPUT = "src/test/resources/output/batchRetry/retryOutput.xml"; |     private static final String EXPECTED_OUTPUT = "src/test/resources/output/batchRetry/retryOutput.xml"; | ||||||
| 
 | 
 | ||||||
|     @Autowired |     @Autowired | ||||||
|     private JobLauncherTestUtils jobLauncherTestUtils; |     private JobLauncherTestUtils jobLauncherTestUtils; | ||||||
| 
 | 
 | ||||||
|     @Mock |     @MockBean | ||||||
|     private CloseableHttpClient closeableHttpClient; |     private CloseableHttpClient closeableHttpClient; | ||||||
| 
 | 
 | ||||||
|     @Mock |     @MockBean | ||||||
|     private CloseableHttpResponse httpResponse; |     private CloseableHttpResponse httpResponse; | ||||||
| 
 | 
 | ||||||
|     @InjectMocks |  | ||||||
|     private RetryItemProcessor retryItemProcessor; |  | ||||||
| 
 |  | ||||||
|     @Before |  | ||||||
|     public void setup() { |  | ||||||
|         MockitoAnnotations.initMocks(this); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Test |     @Test | ||||||
|     public void whenEndpointAlwaysFailing_thenJobFails() throws Exception { |     public void whenEndpointAlwaysFailing_thenJobFails() throws Exception { | ||||||
|         when(closeableHttpClient.execute(any())).thenThrow(new ConnectTimeoutException("Endpoint is down")); |         when(closeableHttpClient.execute(any())) | ||||||
|  |           .thenThrow(new ConnectTimeoutException("Endpoint is down")); | ||||||
| 
 | 
 | ||||||
|         JobExecution jobExecution = jobLauncherTestUtils.launchJob(defaultJobParameters()); |         JobExecution jobExecution = jobLauncherTestUtils.launchJob(defaultJobParameters()); | ||||||
|         JobInstance actualJobInstance = jobExecution.getJobInstance(); |         JobInstance actualJobInstance = jobExecution.getJobInstance(); | ||||||
| @ -84,7 +65,8 @@ public class SpringBatchRetryIntegrationTest { | |||||||
|         FileSystemResource actualResult = new FileSystemResource(TEST_OUTPUT); |         FileSystemResource actualResult = new FileSystemResource(TEST_OUTPUT); | ||||||
| 
 | 
 | ||||||
|         //fails for first two calls and passes third time onwards |         //fails for first two calls and passes third time onwards | ||||||
|         when(httpResponse.getEntity()).thenReturn(new StringEntity("{ \"age\":10, \"postCode\":\"430222\" }")); |         when(httpResponse.getEntity()) | ||||||
|  |           .thenReturn(new StringEntity("{ \"age\":10, \"postCode\":\"430222\" }")); | ||||||
|         when(closeableHttpClient.execute(any())) |         when(closeableHttpClient.execute(any())) | ||||||
|           .thenThrow(new ConnectTimeoutException("Timeout count 1")) |           .thenThrow(new ConnectTimeoutException("Timeout count 1")) | ||||||
|           .thenThrow(new ConnectTimeoutException("Timeout count 2")) |           .thenThrow(new ConnectTimeoutException("Timeout count 2")) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user