How to use AssertionErrorTranslator class of org.jmock.lib package

Best Jmock-library code snippet using org.jmock.lib.AssertionErrorTranslator

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package net.jxta.test.util;
2
3import org.jmock.Mockery;
4import org.jmock.lib.AssertionErrorTranslator;
5import org.junit.Test;
6import org.junit.rules.MethodRule;
7import org.junit.runners.model.FrameworkMethod;
8import org.junit.runners.model.Statement;
9
10/**
11 * JUnit 4.7 based mockery for JMock - this code is taken from issue
12 * JMOCK-237 in the JMock issue tracker. This code is not yet
13 * available in a released version of JMock, but is essential for
14 * us to mix JMock with rule based tests.
15 */
16public class JUnitRuleMockery extends Mockery implements MethodRule {
17
18	public JUnitRuleMockery() {
19	   setExpectationErrorTranslator(AssertionErrorTranslator.INSTANCE);
20	}
21	
22	public Statement apply(final Statement base, final FrameworkMethod method,
23			Object target) {
24		return new Statement() {
25			@Override
26			public void evaluate() throws Throwable {
27				try {
28					base.evaluate();
29				} catch(Throwable exp) {
30					if(!isExceptionExpectedByTestMethod(exp, method)) {
31						throw exp;
32					}
33				}
34				assertIsSatisfied();
35			}
36		};
37	}
38
39	protected boolean isExceptionExpectedByTestMethod(Throwable exp, FrameworkMethod method) {
40		Test test = method.getAnnotation(Test.class);
41		return test.expected().equals(exp.getClass());
42	}
43
44}
45
Full Screen
copy
1package org.jmock.lib;
2
3import org.hamcrest.StringDescription;
4import org.jmock.api.ExpectationError;
5import org.jmock.api.ExpectationErrorTranslator;
6
7/**
8 * Translates {@link org.jmock.api.ExpectationError}s into
9 * {@link java.lang.AssertionError}s that several
10 * test frameworks, including JUnit 4 and TestNG, use to report
11 * errors.
12 * 
13 * @author npryce
14 *
15 */
16public class AssertionErrorTranslator implements ExpectationErrorTranslator {
17    public static final AssertionErrorTranslator INSTANCE = new AssertionErrorTranslator();
18    
19    public Error translate(ExpectationError e) {
20        return new AssertionError(StringDescription.toString(e));
21    }
22    
23    private AssertionErrorTranslator() {}
24}
25
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

...Most popular Stackoverflow questions on AssertionErrorTranslator

    No relevant questions found for this class 😞

Most used methods in AssertionErrorTranslator

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)