How to use method of org.jmock.test.unit.lib.action.ThrowActionTests class

Best Jmock-library code snippet using org.jmock.test.unit.lib.action.ThrowActionTests.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1/*  Copyright (c) 2000-2004 jMock.org
2 */
3package org.jmock.test.unit.lib.action;
4
5import junit.framework.AssertionFailedError;
6import junit.framework.TestCase;
7
8import org.hamcrest.StringDescription;
9import org.jmock.api.Invocation;
10import org.jmock.lib.action.ThrowAction;
11import org.jmock.test.unit.support.AssertThat;
12import org.jmock.test.unit.support.DummyThrowable;
13import org.jmock.test.unit.support.MethodFactory;
14
15
16public class ThrowActionTests extends TestCase {
17    static final Throwable THROWABLE = new DummyThrowable();
18    static final Class<?>[] EXCEPTION_TYPES = {DummyThrowable.class};
19
20    MethodFactory methodFactory;
21    Invocation invocation;
22    ThrowAction throwAction;
23
24    @Override
25    public void setUp() {
26        methodFactory = new MethodFactory();
27        invocation = new Invocation("INVOKED-OBJECT",
28                                    methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, EXCEPTION_TYPES));
29        throwAction = new ThrowAction(THROWABLE);
30    }
31
32    public void testThrowsThrowableObjectPassedToConstructorWhenInvoked() {
33        try {
34            throwAction.invoke(invocation);
35        }
36        catch (Throwable t) {
37            assertSame("Should be the same throwable", THROWABLE, t);
38        }
39    }
40
41    public void testIncludesDetailsOfThrowableInDescription() {
42    	String description = StringDescription.toString(throwAction);
43
44        assertTrue("contains class of thrown object in description",
45                   description.indexOf(THROWABLE.toString()) >= 0);
46        assertTrue("contains 'throws' in description",
47                   description.indexOf("throws") >= 0);
48    }
49
50    public static class ExpectedExceptionType1 extends Exception {
51        private static final long serialVersionUID = 1L;
52    }
53
54    public static class ExpectedExceptionType2 extends Exception {
55        private static final long serialVersionUID = 1L;
56    }
57
58    public void testDoesNotAllowThrowingIncompatibleCheckedException() throws Throwable {
59        Class<?>[] expectedExceptionTypes = {ExpectedExceptionType1.class, ExpectedExceptionType2.class};
60        Invocation incompatibleInvocation = 
61            new Invocation("INVOKED-OBJECT", methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, expectedExceptionTypes));
62
63        try {
64            throwAction.invoke(incompatibleInvocation);
65        }
66        catch (IllegalStateException ex) {
67            String message = ex.getMessage();
68
69            for (int i = 0; i < expectedExceptionTypes.length; i++) {
70                AssertThat.stringIncludes("should include name of expected exception types",
71                    expectedExceptionTypes[i].getName(), message);
72            }
73            AssertThat.stringIncludes("should include name of thrown exception type",
74                THROWABLE.getClass().getName(), message);
75            return;
76        }
77        fail("should have failed");
78    }
79
80    public void testGivesInformativeErrorMessageIfAttemptToThrowCheckedExceptionFromMethodWithNoExceptions() throws Throwable {
81        Invocation incompatibleInvocation = 
82            new Invocation("INVOKED-OBJECT", methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, MethodFactory.NO_EXCEPTIONS));
83        
84        try {
85            throwAction.invoke(incompatibleInvocation);
86        }
87        catch (IllegalStateException ex) {
88            String message = ex.getMessage();
89
90            AssertThat.stringIncludes("should include name of thrown exception type",
91                THROWABLE.getClass().getName(), message);
92            AssertThat.stringIncludes("should describe that the method doesn't allow any exceptions",
93                "no exceptions", message);
94            return;
95        }
96        fail("should have failed");
97    }
98
99    public void testDoesNotCheckTypeCompatiblityOfUncheckedExceptions() throws Throwable {
100        throwAction = new ThrowAction(new RuntimeException());
101
102        try {
103            throwAction.invoke(invocation);
104        }
105        catch (RuntimeException ex) {
106            return;
107        }
108        fail("should have thrown a RuntimeException");
109    }
110
111    public void testDoesNotCheckTypeCompatiblityOfErrors() throws Throwable {
112        throwAction = new ThrowAction(new Error());
113
114        try {
115            throwAction.invoke(invocation);
116        }
117        catch (AssertionFailedError err) {
118            throw err;
119        }
120        catch (Error ex) {
121            return;
122        }
123        fail("should have thrown an Error");
124    }
125
126    public void testSetsStackTraceWhenExceptionIsThrown() {
127        try {
128            throwAction.invoke(invocation);
129        }
130        catch (Throwable t) {
131            StackTraceElement[] stackTrace = t.getStackTrace();
132
133            assertEquals("thrown from ThrowAction object",
134                         throwAction.getClass().getName(), stackTrace[0].getClassName());
135        }
136    }
137}
138
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
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)