Skip to content

Commit 8fa65f6

Browse files
authored
use assertThatThrownBy instead of ExpectedException. (#553)
This change makes easier migrating to Junit5.
1 parent 826d087 commit 8fa65f6

File tree

3 files changed

+42
-105
lines changed

3 files changed

+42
-105
lines changed

config/checkstyle/checkstyle.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
<message key="import.illegal"
8888
value="Use AssertJ (org.assertj.core.api.Assumptions.assumeThat) instead." />
8989
</module>
90+
<module name="IllegalImport">
91+
<property name="id" value="junitExpectedException" />
92+
<property name="illegalClasses" value="org.junit.rules.ExpectedException" />
93+
<message key="import.illegal"
94+
value="Use AssertJ (org.assertj.core.api.Assertions.assertThatThrownBy) instead." />
95+
</module>
9096

9197
<module name="AvoidNestedBlocks">
9298
<property name="allowInSwitchCase" value="true" />

line-bot-api-client/src/test/java/com/linecorp/bot/client/LineMessagingClientImplWiremockTest.java

Lines changed: 31 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -16,125 +16,61 @@
1616

1717
package com.linecorp.bot.client;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.hamcrest.CoreMatchers.isA;
19+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2120

21+
import java.util.Arrays;
22+
import java.util.Collection;
2223
import java.util.concurrent.ExecutionException;
2324

24-
import org.hamcrest.CustomTypeSafeMatcher;
25-
import org.junit.Rule;
2625
import org.junit.Test;
27-
import org.junit.rules.ExpectedException;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.Parameterized;
28+
import org.junit.runners.Parameterized.Parameters;
2829

2930
import com.linecorp.bot.client.exception.BadRequestException;
3031
import com.linecorp.bot.client.exception.ForbiddenException;
31-
import com.linecorp.bot.client.exception.LineMessagingException;
3232
import com.linecorp.bot.client.exception.LineServerException;
3333
import com.linecorp.bot.client.exception.NotFoundException;
3434
import com.linecorp.bot.client.exception.TooManyRequestsException;
3535
import com.linecorp.bot.client.exception.UnauthorizedException;
3636
import com.linecorp.bot.model.error.ErrorResponse;
3737

38+
@RunWith(Parameterized.class)
3839
public class LineMessagingClientImplWiremockTest extends AbstractWiremockTest {
3940
static final ErrorResponse ERROR_RESPONSE =
4041
new ErrorResponse(null, "Error Message", null);
41-
42-
@Rule
43-
public final ExpectedException expectedException = ExpectedException.none();
44-
45-
@Test(timeout = ASYNC_TEST_TIMEOUT)
46-
public void status400BadRequestTest() throws Exception {
47-
// Mocking
48-
mocking(400, ERROR_RESPONSE);
49-
50-
// Expect
51-
expectedException.expect(ExecutionException.class);
52-
expectedException.expectCause(isA(BadRequestException.class));
53-
expectedException.expectCause(errorResponseIs(ERROR_RESPONSE));
54-
55-
// Do
56-
lineBlobClient.getMessageContent("TOKEN").get();
57-
}
58-
59-
@Test(timeout = ASYNC_TEST_TIMEOUT)
60-
public void status401UnauthorizedTest() throws Exception {
61-
// Mocking
62-
mocking(401, ERROR_RESPONSE);
63-
64-
// Expect
65-
expectedException.expect(ExecutionException.class);
66-
expectedException.expectCause(isA(UnauthorizedException.class));
67-
expectedException.expectCause(errorResponseIs(ERROR_RESPONSE));
68-
69-
// Do
70-
lineBlobClient.getMessageContent("TOKEN").get();
71-
}
72-
73-
@Test(timeout = ASYNC_TEST_TIMEOUT)
74-
public void status403ForbiddenTest() throws Exception {
75-
// Mocking
76-
mocking(403, ERROR_RESPONSE);
77-
78-
// Expect
79-
expectedException.expect(ExecutionException.class);
80-
expectedException.expectCause(isA(ForbiddenException.class));
81-
expectedException.expectCause(errorResponseIs(ERROR_RESPONSE));
82-
83-
// Do
84-
lineBlobClient.getMessageContent("TOKEN").get();
85-
}
86-
87-
@Test(timeout = ASYNC_TEST_TIMEOUT)
88-
public void status404NotFoundTest() throws Exception {
89-
// Mocking
90-
mocking(404, ERROR_RESPONSE);
91-
92-
// Expect
93-
expectedException.expect(ExecutionException.class);
94-
expectedException.expectCause(isA(NotFoundException.class));
95-
expectedException.expectCause(errorResponseIs(ERROR_RESPONSE));
96-
97-
// Do
98-
lineBlobClient.getMessageContent("TOKEN").get();
42+
private static final String ERROR_MESSAGE
43+
= "Error Message : ErrorResponse(requestId=null, message=Error Message, details=[])";
44+
45+
@Parameters(name = "{index}: status={0}, expected={1}")
46+
public static Collection<Object[]> data() {
47+
return Arrays.asList(new Object[][] {
48+
{ 400, BadRequestException.class },
49+
{ 401, UnauthorizedException.class },
50+
{ 403, ForbiddenException.class },
51+
{ 404, NotFoundException.class },
52+
{ 429, TooManyRequestsException.class },
53+
{ 500, LineServerException.class }
54+
});
9955
}
10056

101-
@Test(timeout = ASYNC_TEST_TIMEOUT)
102-
public void status429TooManyRequestsTest() throws Exception {
103-
// Mocking
104-
mocking(429, ERROR_RESPONSE);
57+
private final int statusCode;
58+
private final Class<? extends Throwable> expectedClass;
10559

106-
// Expect
107-
expectedException.expect(ExecutionException.class);
108-
expectedException.expectCause(isA(TooManyRequestsException.class));
109-
expectedException.expectCause(errorResponseIs(ERROR_RESPONSE));
110-
111-
// Do
112-
lineBlobClient.getMessageContent("TOKEN").get();
60+
public LineMessagingClientImplWiremockTest(int statusCode, Class<? extends Throwable> expectedClass) {
61+
this.statusCode = statusCode;
62+
this.expectedClass = expectedClass;
11363
}
11464

11565
@Test(timeout = ASYNC_TEST_TIMEOUT)
116-
public void status500InternalServerErrorTest() throws Exception {
66+
public void statusCodeHandlerTest() throws Exception {
11767
// Mocking
118-
mocking(500, ERROR_RESPONSE);
119-
120-
// Expect
121-
expectedException.expect(ExecutionException.class);
122-
expectedException.expectCause(isA(LineServerException.class));
123-
expectedException.expectCause(errorResponseIs(ERROR_RESPONSE));
68+
mocking(statusCode, ERROR_RESPONSE);
12469

12570
// Do
126-
lineBlobClient.getMessageContent("TOKEN").get();
127-
}
128-
129-
private CustomTypeSafeMatcher<LineMessagingException> errorResponseIs(final ErrorResponse errorResponse) {
130-
return new CustomTypeSafeMatcher<LineMessagingException>("Error Response") {
131-
@Override
132-
protected boolean matchesSafely(LineMessagingException item) {
133-
assertThat(item.getErrorResponse())
134-
.isEqualTo(errorResponse);
135-
136-
return true;
137-
}
138-
};
71+
assertThatThrownBy(() -> lineBlobClient.getMessageContent("TOKEN").get())
72+
.isInstanceOf(ExecutionException.class)
73+
.hasRootCauseInstanceOf(expectedClass)
74+
.hasRootCauseMessage(ERROR_MESSAGE);
13975
}
14076
}

line-bot-spring-boot/src/test/java/com/linecorp/bot/spring/boot/support/ReplyByReturnValueConsumerTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.util.Collections.singletonList;
2020
import static java.util.concurrent.CompletableFuture.completedFuture;
2121
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2223
import static org.mockito.ArgumentMatchers.any;
2324
import static org.mockito.Mockito.only;
2425
import static org.mockito.Mockito.reset;
@@ -32,7 +33,6 @@
3233
import org.junit.Rule;
3334
import org.junit.Test;
3435
import org.junit.contrib.java.lang.system.SystemOutRule;
35-
import org.junit.rules.ExpectedException;
3636
import org.mockito.InjectMocks;
3737
import org.mockito.Mock;
3838
import org.mockito.junit.MockitoJUnit;
@@ -52,9 +52,6 @@ public class ReplyByReturnValueConsumerTest {
5252
@Rule
5353
public final MockitoRule mockitoRule = MockitoJUnit.rule();
5454

55-
@Rule
56-
public final ExpectedException expectedException = ExpectedException.none();
57-
5855
@Rule
5956
public final SystemOutRule systemOut = new SystemOutRule().enableLog();
6057

@@ -157,17 +154,15 @@ public void errorInLineMessagingClientLoggingTest() {
157154
// Internal method test.
158155
@Test
159156
public void checkListContentsNullTest() throws Exception {
160-
expectedException.expect(NullPointerException.class);
161-
162157
// Do
163-
ReplyByReturnValueConsumer.checkListContents(singletonList(null));
158+
assertThatThrownBy(() -> ReplyByReturnValueConsumer.checkListContents(singletonList(null)))
159+
.isInstanceOf(NullPointerException.class);
164160
}
165161

166162
@Test
167163
public void checkListContentsIllegalTypeTest() throws Exception {
168-
expectedException.expect(IllegalArgumentException.class);
169-
170164
// Do
171-
ReplyByReturnValueConsumer.checkListContents(singletonList(new Object()));
165+
assertThatThrownBy(() -> ReplyByReturnValueConsumer.checkListContents(singletonList(new Object())))
166+
.isInstanceOf(IllegalArgumentException.class);
172167
}
173168
}

0 commit comments

Comments
 (0)