Skip to content

Commit c2f3552

Browse files
committed
NA: UTs can just use assertThrows instead of try-catch
1 parent e5a6e86 commit c2f3552

File tree

6 files changed

+291
-632
lines changed

6 files changed

+291
-632
lines changed

yoti-sdk-api/src/test/java/com/yoti/api/client/docs/DocScanClientBuilderTest.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.hamcrest.Matchers.containsString;
55
import static org.hamcrest.Matchers.is;
66
import static org.hamcrest.Matchers.notNullValue;
7+
import static org.junit.Assert.assertThrows;
78
import static org.junit.Assert.fail;
89

910
import com.yoti.api.client.KeyPairSource;
@@ -26,40 +27,25 @@ public void setUp() {
2627

2728
@Test
2829
public void build_shouldThrowExceptionWhenSdkIdIsNull() {
29-
try {
30-
DocScanClient.builder()
31-
.build();
32-
} catch (IllegalArgumentException ex) {
33-
assertThat(ex.getMessage(), containsString("SDK ID"));
34-
return;
35-
}
36-
fail("Expected an exception");
30+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> DocScanClient.builder().build());
31+
32+
assertThat(ex.getMessage(), containsString("SDK ID"));
3733
}
3834

3935
@Test
4036
public void build_shouldThrowExceptionWhenSdkIdIsEmpty() {
41-
try {
42-
DocScanClient.builder()
43-
.withClientSdkId("")
44-
.build();
45-
} catch (IllegalArgumentException ex) {
46-
assertThat(ex.getMessage(), containsString("SDK ID"));
47-
return;
48-
}
49-
fail("Expected an exception");
37+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> DocScanClient.builder().withClientSdkId("").build());
38+
39+
assertThat(ex.getMessage(), containsString("SDK ID"));
5040
}
5141

5242
@Test
5343
public void build_shouldThrowExceptionWhenKeyPairSourceIsNull() {
54-
try {
55-
DocScanClient.builder()
56-
.withClientSdkId(SOME_APPLICATION_ID)
57-
.build();
58-
} catch (IllegalArgumentException ex) {
59-
assertThat(ex.getMessage(), containsString("Application key Pair"));
60-
return;
61-
}
62-
fail("Expected an exception");
44+
DocScanClient.Builder builder = DocScanClient.builder().withClientSdkId(SOME_APPLICATION_ID);
45+
46+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> builder.build());
47+
48+
assertThat(ex.getMessage(), containsString("Application key Pair"));
6349
}
6450

6551
@Test

yoti-sdk-api/src/test/java/com/yoti/api/client/docs/DocScanClientTest.java

Lines changed: 41 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static org.hamcrest.Matchers.instanceOf;
66
import static org.hamcrest.Matchers.is;
77
import static org.junit.Assert.assertThrows;
8-
import static org.junit.Assert.fail;
98
import static org.mockito.ArgumentMatchers.any;
109
import static org.mockito.ArgumentMatchers.eq;
1110
import static org.mockito.Mockito.doThrow;
@@ -49,194 +48,142 @@ public void setUp() {
4948
public void constructor_shouldFailWhenStreamExceptionLoadingKeys() {
5049
KeyPairSource badKeyPairSource = new StaticKeyPairSource(true);
5150

52-
try {
53-
new DocScanClient(APP_ID, badKeyPairSource, docScanServiceMock);
54-
} catch (InitialisationException e) {
55-
assertThat(e.getCause(), is(instanceOf(IOException.class)));
56-
assertThat(e.getCause().getMessage(), containsString("Test stream exception"));
57-
return;
58-
}
59-
fail("Expected an Exception");
51+
InitialisationException ex = assertThrows(InitialisationException.class, () -> new DocScanClient(APP_ID, badKeyPairSource, docScanServiceMock));
52+
53+
assertThat(ex.getCause(), is(instanceOf(IOException.class)));
54+
assertThat(ex.getCause().getMessage(), containsString("Test stream exception"));
6055
}
6156

6257
@Test
6358
public void createDocScanSession_shouldFailWithExceptionFromYotiDocsService() throws Exception {
6459
DocScanException original = new DocScanException("Test exception");
65-
6660
when(docScanServiceMock.createSession(eq(APP_ID), any(KeyPair.class), eq(sessionSpecMock))).thenThrow(original);
61+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
62+
63+
DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.createSession(sessionSpecMock));
6764

68-
try {
69-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
70-
testObj.createSession(sessionSpecMock);
71-
} catch (DocScanException thrown) {
72-
assertThat(thrown, is(original));
73-
return;
74-
}
75-
fail("Expected an exception");
65+
assertThat(thrown, is(original));
7666
}
7767

7868
@Test
7969
public void getDocScanSession_shouldFailWithExceptionFromYotiDocsService() throws Exception {
8070
DocScanException original = new DocScanException("Test exception");
81-
8271
when(docScanServiceMock.retrieveSession(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID))).thenThrow(original);
72+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
73+
74+
DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.getSession(SOME_SESSION_ID));
8375

84-
try {
85-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
86-
testObj.getSession(SOME_SESSION_ID);
87-
} catch (DocScanException thrown) {
88-
assertThat(thrown, is(original));
89-
return;
90-
}
91-
fail("Expected an exception");
76+
assertThat(thrown, is(original));
9277
}
9378

9479
@Test
9580
public void getDocScanMedia_shouldFailWithExceptionFromYotiDocsService() throws Exception {
9681
DocScanException original = new DocScanException("Test exception");
97-
9882
when(docScanServiceMock.getMediaContent(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID), eq(SOME_MEDIA_ID))).thenThrow(original);
83+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
84+
85+
DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.getMediaContent(SOME_SESSION_ID, SOME_MEDIA_ID));
9986

100-
try {
101-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
102-
testObj.getMediaContent(SOME_SESSION_ID, SOME_MEDIA_ID);
103-
} catch (DocScanException thrown) {
104-
assertThat(thrown, is(original));
105-
return;
106-
}
107-
fail("Expected an exception");
87+
assertThat(thrown, is(original));
10888
}
10989

11090
@Test
11191
public void deleteDocScanMedia_shouldFailWithExceptionFromYotiDocsService() throws Exception {
11292
DocScanException original = new DocScanException("Test exception");
113-
11493
doThrow(original).when(docScanServiceMock).deleteMediaContent(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID), eq(SOME_MEDIA_ID));
94+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
11595

116-
try {
117-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
118-
testObj.deleteMediaContent(SOME_SESSION_ID, SOME_MEDIA_ID);
119-
} catch (DocScanException thrown) {
120-
assertThat(thrown, is(original));
121-
return;
122-
}
123-
fail("Expected an exception");
96+
DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.deleteMediaContent(SOME_SESSION_ID, SOME_MEDIA_ID));
97+
98+
assertThat(thrown, is(original));
12499
}
125100

126101
@Test
127102
public void deleteDocScanSession_shouldFailWithExceptionFromYotiDocsService() throws Exception {
128103
DocScanException original = new DocScanException("Test exception");
129-
130104
doThrow(original).when(docScanServiceMock).deleteSession(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
105+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
131106

132-
try {
133-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
134-
testObj.deleteSession(SOME_SESSION_ID);
135-
} catch (DocScanException thrown) {
136-
assertThat(thrown, is(original));
137-
return;
138-
}
139-
fail("Expected an exception");
107+
DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.deleteSession(SOME_SESSION_ID));
108+
109+
assertThat(thrown, is(original));
140110
}
141111

142112
@Test
143113
public void putIbvInstructions_shouldFailWithExceptionFromYotiDocsService() throws Exception {
144114
DocScanException original = new DocScanException("Test exception");
145-
146115
doThrow(original).when(docScanServiceMock).putIbvInstructions(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID), eq(instructionsMock));
116+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
147117

148-
DocScanException exception = assertThrows(DocScanException.class, () -> {
149-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
150-
testObj.putIbvInstructions(SOME_SESSION_ID, instructionsMock);
151-
});
118+
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.putIbvInstructions(SOME_SESSION_ID, instructionsMock));
152119

153120
assertThat(exception, is(original));
154121
}
155122

156123
@Test
157124
public void getIbvInstructions_shouldFailWithExceptionFromYotiDocsService() throws Exception {
158125
DocScanException original = new DocScanException("Test exception");
159-
160126
doThrow(original).when(docScanServiceMock).getIbvInstructions(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
127+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
161128

162-
DocScanException exception = assertThrows(DocScanException.class, () -> {
163-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
164-
testObj.getIbvInstructions(SOME_SESSION_ID);
165-
});
129+
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.getIbvInstructions(SOME_SESSION_ID));
166130

167131
assertThat(exception, is(original));
168132
}
169133

170134
@Test
171135
public void getIbvInstructionsPdf_shouldFailWithExceptionFromYotiDocsService() throws Exception {
172136
DocScanException original = new DocScanException("Test exception");
173-
174137
doThrow(original).when(docScanServiceMock).getIbvInstructionsPdf(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
138+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
175139

176-
DocScanException exception = assertThrows(DocScanException.class, () -> {
177-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
178-
testObj.getIbvInstructionsPdf(SOME_SESSION_ID);
179-
});
140+
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.getIbvInstructionsPdf(SOME_SESSION_ID));
180141

181142
assertThat(exception, is(original));
182143
}
183144

184145
@Test
185146
public void fetchInstructionsContactProfile_shouldFailWithExceptionFromYotiDocsService() throws Exception {
186147
DocScanException original = new DocScanException("Test exception");
187-
188148
doThrow(original).when(docScanServiceMock).fetchInstructionsContactProfile(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
149+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
189150

190-
DocScanException exception = assertThrows(DocScanException.class, () -> {
191-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
192-
testObj.fetchInstructionsContactProfile(SOME_SESSION_ID);
193-
});
151+
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.fetchInstructionsContactProfile(SOME_SESSION_ID));
194152

195153
assertThat(exception, is(original));
196154
}
197155

198156
@Test
199157
public void triggerIbvEmailNotification_shouldFailWithExceptionFromYotiDocsService() throws Exception {
200158
DocScanException original = new DocScanException("Test exception");
201-
202159
doThrow(original).when(docScanServiceMock).triggerIbvEmailNotification(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
160+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
203161

204-
DocScanException exception = assertThrows(DocScanException.class, () -> {
205-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
206-
testObj.triggerIbvEmailNotification(SOME_SESSION_ID);
207-
});
162+
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.triggerIbvEmailNotification(SOME_SESSION_ID));
208163

209164
assertThat(exception, is(original));
210165
}
211-
166+
212167
@Test
213168
public void getSessionConfiguration_shouldFailWithExceptionFromYotiDocsService() throws Exception {
214169
DocScanException original = new DocScanException("Test exception");
215-
216170
doThrow(original).when(docScanServiceMock).fetchSessionConfiguration(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
171+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
217172

218-
DocScanException exception = assertThrows(DocScanException.class, () -> {
219-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
220-
testObj.getSessionConfiguration(SOME_SESSION_ID);
221-
});
173+
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.getSessionConfiguration(SOME_SESSION_ID));
222174

223175
assertThat(exception, is(original));
224176
}
225177

226178
@Test
227179
public void getSupportedDocuments_shouldFailWithExceptionFromYotiDocsService() throws Exception {
228180
DocScanException original = new DocScanException("Test exception");
229-
230181
doThrow(original).when(docScanServiceMock).getSupportedDocuments(any(KeyPair.class), any(Boolean.class));
182+
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
183+
184+
DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.getSupportedDocuments());
231185

232-
try {
233-
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
234-
testObj.getSupportedDocuments();
235-
} catch (DocScanException thrown) {
236-
assertThat(thrown, is(original));
237-
return;
238-
}
239-
fail("Expected an exception");
186+
assertThat(thrown, is(original));
240187
}
241188

242189
}

0 commit comments

Comments
 (0)