How to use method of org.jmock.lib.action.ThrowAction class

Best Jmock-library code snippet using org.jmock.lib.action.ThrowAction.

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.lib.action;
4
5import org.hamcrest.Description;
6import org.jmock.api.Action;
7import org.jmock.api.Invocation;
8
9/**
10 * Throws an exception.
11 * 
12 * @author nat
13 *
14 */
15public class ThrowAction implements Action {
16    private Throwable throwable;
17    
18    public ThrowAction(Throwable throwable) {
19        this.throwable = throwable;
20    }
21
22    public Object invoke(Invocation invocation) throws Throwable {
23        if (isThrowingCheckedException()) {
24            checkTypeCompatiblity(invocation.getInvokedMethod().getExceptionTypes());
25        }
26
27        throwable.fillInStackTrace();
28        throw throwable;
29    }
30
31    public void describeTo(Description description) {
32        description.appendText("throws ");
33        description.appendValue(throwable);
34    }
35
36    private void checkTypeCompatiblity(Class<?>[] allowedExceptionTypes) {
37        for (int i = 0; i < allowedExceptionTypes.length; i++) {
38            if (allowedExceptionTypes[i].isInstance(throwable))
39                return;
40        }
41        
42        reportIncompatibleCheckedException(allowedExceptionTypes);
43    }
44
45    private void reportIncompatibleCheckedException(Class<?>[] allowedTypes) {
46        StringBuffer message = new StringBuffer();
47
48        message.append("tried to throw a ");
49        message.append(throwable.getClass().getName());
50        message.append(" from a method that throws ");
51
52        if (allowedTypes.length == 0) {
53            message.append("no exceptions");
54        } else {
55            for (int i = 0; i < allowedTypes.length; i++) {
56                if (i > 0)
57                    message.append(",");
58                message.append(allowedTypes[i].getName());
59            }
60        }
61        
62        throw new IllegalStateException(message.toString());
63    }
64    
65    private boolean isThrowingCheckedException() {
66        return !(throwable instanceof RuntimeException || throwable instanceof Error);
67    }
68}
69
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)