How to use method of org.easymock.tests2.MockedExceptionTest class

Best Easymock code snippet using org.easymock.tests2.MockedExceptionTest.

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2001-2021 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock.tests2;
17
18import static org.easymock.EasyMock.*;
19import static org.junit.Assert.*;
20
21import org.junit.Test;
22
23/**
24 * @author Henri Tremblay
25 */
26public class MockedExceptionTest {
27
28    @Test
29    public void testMockedException() {
30        RuntimeException expected = createNiceMock(RuntimeException.class);
31        CharSequence c = createMock(CharSequence.class);
32        expect(c.length()).andStubThrow(expected);
33        replay(c, expected);
34
35        try {
36            c.length(); // fillInStackTrace will be called internally here
37        } catch (RuntimeException actual) {
38            assertSame(expected, actual);
39        }
40
41        verify(c, expected);
42    }
43
44    @Test
45    public void testExplicitFillInStackTrace() {
46
47        RuntimeException expected = createNiceMock(RuntimeException.class);
48        RuntimeException myException = new RuntimeException();
49        expect(expected.fillInStackTrace()).andReturn(myException);
50
51        CharSequence c = createMock(CharSequence.class);
52        expect(c.length()).andStubThrow(expected);
53
54        replay(c, expected);
55
56        try {
57            c.length(); // fillInStackTrace wont' be called internally
58        } catch (RuntimeException actual) {
59            assertSame(myException, actual.fillInStackTrace()); // so the fillInStackTrace recording is still valid
60            assertSame(expected, actual);
61        }
62
63        verify(c, expected);
64    }
65
66    private static int check = 2;
67
68    private static class MyException extends RuntimeException {
69        private static final long serialVersionUID = 1L;
70
71        @Override
72        public synchronized Throwable fillInStackTrace() {
73            check = 4;
74            return super.fillInStackTrace();
75        }
76
77    }
78
79    @Test
80    public void testNotMockedFillInStackTrace() {
81
82        RuntimeException expected = createMockBuilder(MyException.class)
83                .createNiceMock();
84
85        CharSequence c = createMock(CharSequence.class);
86        expect(c.length()).andStubThrow(expected);
87
88        replay(c, expected);
89
90        try {
91            c.length(); // fillInStackTrace won't be called internally
92        } catch (RuntimeException actual) {
93            assertSame(expected, actual);
94            assertSame("fillInStackTrace should have been called normally since it isn't mocked", expected,
95                    actual.fillInStackTrace());
96            assertEquals("The original method was called", 4, check);
97        }
98
99        verify(c, expected);
100    }
101
102    @Test
103    public void testRealException() {
104
105        RuntimeException expected = new RuntimeException();
106
107        CharSequence c = createMock(CharSequence.class);
108        expect(c.length()).andStubThrow(expected);
109
110        replay(c);
111
112        try {
113            c.length(); // fillInStackTrace will be called internally here
114        } catch (RuntimeException actual) {
115            assertSame(expected, actual);
116            assertEquals("fillInStackTrace should have been called normally",
117                    "org.easymock.internal.MockInvocationHandler", actual.getStackTrace()[0].getClassName());
118        }
119
120        verify(c);
121    }
122}
123
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 code on LambdaTest Cloud Grid

Execute automation tests with 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)