|
22 | 22 | import org.junit.Rule;
|
23 | 23 | import org.junit.Test;
|
24 | 24 | import org.junit.rules.ExpectedException;
|
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.junit.MockitoJUnitRunner; |
25 | 28 |
|
26 | 29 | import org.springframework.batch.item.ExecutionContext;
|
27 | 30 | import org.springframework.batch.item.ItemStreamException;
|
28 | 31 | import org.springframework.core.io.AbstractResource;
|
| 32 | +import org.springframework.core.io.ByteArrayResource; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.fail; |
29 | 36 |
|
30 | 37 | /**
|
31 | 38 | * @author Mahmoud Ben Hassine
|
32 | 39 | */
|
| 40 | +@RunWith(MockitoJUnitRunner.class) |
33 | 41 | public class JsonItemReaderTests {
|
34 | 42 |
|
35 | 43 | @Rule
|
36 | 44 | public ExpectedException expectedException = ExpectedException.none();
|
37 | 45 |
|
38 |
| - private JsonItemReader<String> itemReader = new JsonItemReader<>(); |
39 |
| - |
40 |
| - @Test |
41 |
| - public void testMandatoryJsonObjectReader() throws Exception { |
42 |
| - this.expectedException.expect(IllegalArgumentException.class); |
43 |
| - this.expectedException.expectMessage("The json object reader must not be null."); |
| 46 | + @Mock |
| 47 | + private JsonObjectReader<String> jsonObjectReader; |
44 | 48 |
|
45 |
| - this.itemReader.afterPropertiesSet(); |
46 |
| - } |
| 49 | + private JsonItemReader<String> itemReader; |
47 | 50 |
|
48 | 51 | @Test
|
49 |
| - public void testMandatoryResource() throws Exception { |
50 |
| - this.expectedException.expect(IllegalArgumentException.class); |
51 |
| - this.expectedException.expectMessage("The resource must not be null."); |
52 |
| - this.itemReader.setJsonObjectReader(() -> null); |
| 52 | + public void testValidation() { |
| 53 | + try { |
| 54 | + new JsonItemReader<>(null, this.jsonObjectReader); |
| 55 | + fail("A resource is required."); |
| 56 | + } catch (IllegalArgumentException iae) { |
| 57 | + assertEquals("The resource must not be null.", iae.getMessage()); |
| 58 | + } |
53 | 59 |
|
54 |
| - this.itemReader.afterPropertiesSet(); |
| 60 | + try { |
| 61 | + new JsonItemReader<>(new ByteArrayResource("[{}]".getBytes()), null); |
| 62 | + fail("A json object reader is required."); |
| 63 | + } catch (IllegalArgumentException iae) { |
| 64 | + assertEquals("The json object reader must not be null.", iae.getMessage()); |
| 65 | + } |
55 | 66 | }
|
56 | 67 |
|
57 | 68 | @Test
|
58 |
| - public void testNonExistentResource() throws Exception { |
| 69 | + public void testNonExistentResource() { |
| 70 | + // given |
59 | 71 | this.expectedException.expect(ItemStreamException.class);
|
60 | 72 | this.expectedException.expectMessage("Failed to initialize the reader");
|
61 | 73 | this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class));
|
62 |
| - this.itemReader.setJsonObjectReader(() -> null); |
63 |
| - this.itemReader.setResource(new NonExistentResource()); |
64 |
| - this.itemReader.afterPropertiesSet(); |
| 74 | + this.itemReader = new JsonItemReader<>(new NonExistentResource(), this.jsonObjectReader); |
65 | 75 |
|
| 76 | + // when |
66 | 77 | this.itemReader.open(new ExecutionContext());
|
| 78 | + |
| 79 | + // then |
| 80 | + // expected exception |
67 | 81 | }
|
68 | 82 |
|
69 | 83 | @Test
|
70 |
| - public void testNonReadableResource() throws Exception { |
| 84 | + public void testNonReadableResource() { |
| 85 | + // given |
71 | 86 | this.expectedException.expect(ItemStreamException.class);
|
72 | 87 | this.expectedException.expectMessage("Failed to initialize the reader");
|
73 | 88 | this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class));
|
74 |
| - this.itemReader.setJsonObjectReader(() -> null); |
75 |
| - this.itemReader.setResource(new NonReadableResource()); |
76 |
| - this.itemReader.afterPropertiesSet(); |
| 89 | + this.itemReader = new JsonItemReader<>(new NonReadableResource(), this.jsonObjectReader); |
77 | 90 |
|
| 91 | + // when |
78 | 92 | this.itemReader.open(new ExecutionContext());
|
| 93 | + |
| 94 | + // then |
| 95 | + // expected exception |
79 | 96 | }
|
80 | 97 |
|
81 | 98 | private static class NonExistentResource extends AbstractResource {
|
|
0 commit comments