How to use expectCause method of org.junit.rules.ExpectedException class

Best junit code snippet using org.junit.rules.ExpectedException.expectCause

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright 2018 LINE Corporation
3 *
4 * LINE Corporation licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17package com.linecorp.bot.client;
18
19import static org.assertj.core.api.Assertions.assertThat;
20import static org.hamcrest.CoreMatchers.isA;
21
22import java.util.concurrent.ExecutionException;
23
24import org.hamcrest.CustomTypeSafeMatcher;
25import org.junit.Rule;
26import org.junit.Test;
27import org.junit.rules.ExpectedException;
28
29import com.linecorp.bot.client.exception.BadRequestException;
30import com.linecorp.bot.client.exception.ForbiddenException;
31import com.linecorp.bot.client.exception.LineMessagingException;
32import com.linecorp.bot.client.exception.LineServerException;
33import com.linecorp.bot.client.exception.NotFoundException;
34import com.linecorp.bot.client.exception.TooManyRequestsException;
35import com.linecorp.bot.client.exception.UnauthorizedException;
36import com.linecorp.bot.model.error.ErrorResponse;
37
38public class LineMessagingClientImplWiremockTest extends AbstractWiremockTest {
39    static final ErrorResponse ERROR_RESPONSE =
40            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        lineMessagingClient.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        lineMessagingClient.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        lineMessagingClient.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        lineMessagingClient.getMessageContent("TOKEN").get();
99    }
100
101    @Test(timeout = ASYNC_TEST_TIMEOUT)
102    public void status429TooManyRequestsTest() throws Exception {
103        // Mocking
104        mocking(429, ERROR_RESPONSE);
105
106        // Expect
107        expectedException.expect(ExecutionException.class);
108        expectedException.expectCause(isA(TooManyRequestsException.class));
109        expectedException.expectCause(errorResponseIs(ERROR_RESPONSE));
110
111        // Do
112        lineMessagingClient.getMessageContent("TOKEN").get();
113    }
114
115    @Test(timeout = ASYNC_TEST_TIMEOUT)
116    public void status500InternalServerErrorTest() throws Exception {
117        // 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));
124
125        // Do
126        lineMessagingClient.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        };
139    }
140}
141
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger expectCause code on LambdaTest Cloud Grid

Execute automation tests with expectCause on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)