Skip to content

Commit f227c49

Browse files
committed
- replaces try catches in batch unit tests by assert throws for readbility
1 parent 73219ae commit f227c49

3 files changed

Lines changed: 22 additions & 47 deletions

File tree

‎src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
import static org.junit.Assert.assertFalse;
44
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertThrows;
56
import static org.junit.Assert.assertTrue;
6-
import static org.junit.Assert.fail;
77
import static org.mockito.ArgumentMatchers.any;
88
import static org.mockito.Mockito.mock;
99
import static org.mockito.Mockito.when;
1010

11-
import java.io.IOException;
12-
import java.util.ArrayList;
13-
import java.util.Arrays;
1411
import java.util.HashSet;
15-
import java.util.List;
1612
import java.util.concurrent.ThreadLocalRandom;
1713

1814
import com.google.gson.JsonParser;
@@ -28,7 +24,6 @@
2824
import org.mockito.stubbing.Answer;
2925

3026
import okhttp3.Call;
31-
import okhttp3.Callback;
3227
import okhttp3.MediaType;
3328
import okhttp3.OkHttpClient;
3429
import okhttp3.Protocol;
@@ -115,34 +110,26 @@ public String answer(InvocationOnMock invocation) throws Throwable {
115110
}
116111
});
117112

118-
try {
113+
assertThrows("the number of steps cannot exceed 20", IllegalArgumentException.class, () -> {
119114
new MSBatchRequestContent(null, null, null, null, null, null, null, null, null, null, null, null, null,
120115
null, null, null, null, null, null, null, null, null);
121-
fail("the number of steps cannot exceed 20");
122-
} catch (IllegalArgumentException ex) {
123-
}
116+
});
124117

125118
new MSBatchRequestContent(mockStep, null); // addind a null step doesn't throw
126-
try {
119+
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
127120
new MSBatchRequestContent().addBatchRequestStep(null);
128-
fail("should throw argument exception");
129-
} catch (IllegalArgumentException ex) {
130-
}
131-
try {
121+
});
122+
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
132123
new MSBatchRequestContent().addBatchRequestStep((Request) null);
133-
fail("should throw argument exception");
134-
} catch (IllegalArgumentException ex) {
135-
}
124+
});
136125

137-
try {
126+
assertThrows("the number of steps cannot exceed 20", IllegalArgumentException.class, () -> {
138127
final MSBatchRequestContent batchContent = new MSBatchRequestContent();
139128
for (int i = 0; i < MSBatchRequestContent.MAX_NUMBER_OF_REQUESTS; i++) {
140129
assertNotNull(batchContent.addBatchRequestStep(mock(Request.class)));
141130
}
142131
batchContent.addBatchRequestStep(mock(Request.class));
143-
fail("the number of steps cannot exceed 20");
144-
} catch (IllegalArgumentException ex) {
145-
}
132+
});
146133
{
147134
final MSBatchRequestContent batchContent = new MSBatchRequestContent();
148135
reservedIds.clear();

‎src/test/java/com/microsoft/graph/content/MSBatchRequestStepTest.java‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.microsoft.graph.content;
22

3+
import static org.junit.Assert.assertThrows;
34
import static org.junit.Assert.assertTrue;
4-
import static org.junit.Assert.fail;
55
import static org.mockito.Mockito.mock;
66

77
import org.junit.Test;
@@ -22,21 +22,15 @@ public void testMSBatchRequestStepCreation() {
2222

2323
@Test
2424
public void defensiveProgrammingTests() {
25-
try {
25+
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
2626
new MSBatchRequestStep(null, null);
27-
fail("should throw argument exception");
28-
} catch (IllegalArgumentException ex) {
29-
}
30-
try {
27+
});
28+
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
3129
new MSBatchRequestStep("id", null);
32-
fail("should throw argument exception");
33-
} catch (IllegalArgumentException ex) {
34-
}
35-
try {
30+
});
31+
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
3632
new MSBatchRequestStep("", null);
37-
fail("should throw argument exception");
38-
} catch (IllegalArgumentException ex) {
39-
}
33+
});
4034
new MSBatchRequestStep("id", mock(Request.class));
4135
}
4236

‎src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.microsoft.graph.content;
22

3-
import static org.junit.Assert.assertNotNull;
3+
import static org.junit.Assert.assertThrows;
44
import static org.junit.Assert.assertTrue;
55

66
import java.io.IOException;
@@ -20,13 +20,9 @@ public class MSBatchResponseContentTest {
2020

2121
@Test
2222
public void testNullMSBatchResponseContent() {
23-
Response response = null;
24-
try {
25-
new MSBatchResponseContent(response);
26-
}
27-
catch(IllegalArgumentException e) {
28-
assertNotNull(e);
29-
}
23+
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
24+
new MSBatchResponseContent((Response)null);
25+
});
3026
}
3127

3228
@Test
@@ -49,11 +45,9 @@ public void testInvalidMSBatchResponseContentWithEmptyResponse() {
4945

5046
@Test
5147
public void testInvalidMSBatchResponseContentWithNullResponseString() {
52-
try{
48+
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
5349
new MSBatchResponseContent(null);
54-
}catch(Exception e) {
55-
assertTrue(e instanceof IllegalArgumentException);
56-
}
50+
});
5751
}
5852

5953
@Test

0 commit comments

Comments
 (0)