How to use method of org.jmock.junit5.extensions.ExpectationExtension class

Best Jmock-library code snippet using org.jmock.junit5.extensions.ExpectationExtension.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.junit5.extensions;
2
3import java.lang.annotation.Annotation;
4import java.util.Timer;
5import java.util.TimerTask;
6
7import org.junit.jupiter.api.Assertions;
8import org.junit.jupiter.api.extension.AfterEachCallback;
9import org.junit.jupiter.api.extension.BeforeEachCallback;
10import org.junit.jupiter.api.extension.Extension;
11import org.junit.jupiter.api.extension.ExtensionContext;
12import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
13
14import com.google.auto.service.AutoService;
15
16@AutoService(Extension.class)
17public class ExpectationExtension implements TestExecutionExceptionHandler, BeforeEachCallback, AfterEachCallback {
18
19    private Throwable thrown = null;
20    private Timer timer = null;
21
22    @Override
23    public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
24        if (isAnnotated(context, ExpectationThrows.class)) {
25            if (!readExpectedFromAnnotations(context).isAssignableFrom(throwable.getClass())) {
26                throw throwable;
27            }
28            thrown = throwable;
29        }
30    }
31
32    @Override
33    public void beforeEach(ExtensionContext context) throws Exception {
34        if (isAnnotated(context, ExpectationTimeout.class)) {
35            timer = new Timer(true);
36            timer.schedule(new TimerTask() {
37                @Override
38                public void run() {
39                    Assertions.fail("Timed out");
40                }
41            }, readTimoutFromAnnotations(context));
42        }
43    }
44
45    @Override
46    public void afterEach(ExtensionContext context) throws Exception {
47        if (isAnnotated(context, ExpectationTimeout.class)) {
48            timer.cancel();
49            timer = null;
50        }
51
52        if (isAnnotated(context, ExpectationThrows.class)) {
53            Class<? extends Throwable> expected = readExpectedFromAnnotations(context);
54            if (thrown == null || !expected.isAssignableFrom(thrown.getClass())) {
55                Assertions.fail("Was expecting the throwable:" + expected.getName());
56            }
57        }
58    }
59
60    private boolean isAnnotated(ExtensionContext context, Class<? extends Annotation> annotation) {
61        return context.getRequiredTestMethod().isAnnotationPresent(annotation);
62    }
63
64    private Class<? extends Throwable> readExpectedFromAnnotations(ExtensionContext context) {
65        ExpectationThrows annotation = context.getRequiredTestMethod().getAnnotation(ExpectationThrows.class);
66        return annotation.expected();
67    }
68
69    private long readTimoutFromAnnotations(ExtensionContext context) {
70        ExpectationTimeout annotation = context.getRequiredTestMethod().getAnnotation(ExpectationTimeout.class);
71        return annotation.timeout();
72    }
73}
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)