How to use AbstractMatcherTest class of org.jmock.test.unit.lib package

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

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.test.unit.lib;
2
3import static org.hamcrest.StringDescription.asString;
4import static org.jmock.lib.CurrentStateMatcher.isCurrently;
5import static org.jmock.lib.CurrentStateMatcher.isNotCurrently;
6
7import org.hamcrest.AbstractMatcherTest;
8import org.hamcrest.Matcher;
9import org.jmock.States;
10import org.jmock.internal.StateMachine;
11
12
13public class CurrentStateMatcherTests extends AbstractMatcherTest {
14    States stateMachine = new StateMachine("stateMachine");
15    Matcher<States> isCurrentlyS = isCurrently("S");
16    Matcher<States> isNotCurrentlyS = isNotCurrently("S");
17    
18    public void testMatchesStateMachineCurrentlyInNamedState() {
19        stateMachine.become("S");
20        
21        assertTrue("should match", isCurrently("S").matches(stateMachine));
22        assertTrue("should not match", !isNotCurrently("S").matches(stateMachine));
23    }
24    
25    public void testDoesNotMatchStateMachineCurrentlyInOtherState() {
26        stateMachine.become("T");
27        
28        assertTrue("should not match", !isCurrently("S").matches(stateMachine));
29        assertTrue("should match", isNotCurrently("S").matches(stateMachine));
30    }
31
32    public void testDoesNotMatchStateMachineInAnonymousInitialState() {
33        assertTrue("should not match", !isCurrently("S").matches(stateMachine));
34        assertTrue("should match", isNotCurrently("S").matches(stateMachine));
35    }
36
37    public void testDoesNotMatchNull() {
38        assertTrue("should not match", !isCurrentlyS.matches(null));
39    }
40
41    public void testDoesNotMatchOtherTypesOfObject() {
42        assertTrue("should not match", !isCurrentlyS.matches("something else"));
43    }
44    
45    public void testHasReadableDescription() {
46        assertEquals("a state machine that is S", asString(isCurrently("S")));
47        assertEquals("a state machine that is not S", asString(isNotCurrently("S")));
48    }
49    
50    public void testHasReadableDescriptionWhenPassedToAssertThat() {
51        stateMachine.become("X");
52        
53        assertMismatchDescription("was not S", isCurrently("S"), stateMachine);
54    }
55
56    @Override
57    protected Matcher<?> createMatcher() {
58        return isCurrentlyS;
59    }
60}
61
Full Screen
copy
1/*  Copyright (c) 2000-2006 hamcrest.org
2 */
3package org.jmock.test.unit.lib;
4
5import junit.framework.TestCase;
6
7import org.hamcrest.Description;
8import org.hamcrest.Matcher;
9import org.hamcrest.StringDescription;
10import org.junit.Assert;
11
12
13/**
14 * Need this source from hamcrest to break the circular dependency with JMock.
15 * 
16 * org.hamcrest.JMock1Matchers depends on org.jmock.core.Constraint
17 * org.jmock.test.unit.lib.CurrentStateMatcherTests used to depend on org.hamcrest.AbstractMatcherTest
18 * @author byeo
19 *
20 */
21public abstract class AbstractMatcherTest extends TestCase {
22
23    /**
24     * Create an instance of the Matcher so some generic safety-net tests can be
25     * run on it.
26     */
27    protected abstract Matcher<?> createMatcher();
28
29    public static <T> void assertMatches(Matcher<T> matcher, T arg) {
30        assertMatches("Expected match, but mismatched", matcher, arg);
31    }
32
33    public static <T> void assertMatches(String message, Matcher<T> matcher,
34        T arg) {
35        if (!matcher.matches(arg)) {
36            Assert.fail(message + " because: '"
37                + mismatchDescription(matcher, arg) + "'");
38        }
39    }
40
41    public static <T> void assertDoesNotMatch(Matcher<? super T> c, T arg) {
42        assertDoesNotMatch("Unexpected match", c, arg);
43    }
44
45    public static <T> void assertDoesNotMatch(String message,
46        Matcher<? super T> c, T arg) {
47        Assert.assertFalse(message, c.matches(arg));
48    }
49
50    public static void assertDescription(String expected, Matcher<?> matcher) {
51        Description description = new StringDescription();
52        description.appendDescriptionOf(matcher);
53        Assert.assertEquals("Expected description", expected, description
54            .toString().trim());
55    }
56
57    public static <T> void assertMismatchDescription(String expected,
58        Matcher<? super T> matcher, T arg) {
59        Assert.assertFalse("Precondition: Matcher should not match item.",
60                           matcher.matches(arg));
61        Assert.assertEquals("Expected mismatch description", expected,
62                            mismatchDescription(matcher, arg));
63    }
64
65    public static void assertNullSafe(Matcher<?> matcher) {
66        try {
67            matcher.matches(null);
68        }
69        catch (Exception e) {
70            Assert.fail("Matcher was not null safe");
71        }
72    }
73
74    public static void assertUnknownTypeSafe(Matcher<?> matcher) {
75        try {
76            matcher.matches(new UnknownType());
77        }
78        catch (Exception e) {
79            Assert.fail("Matcher was not unknown type safe");
80        }
81    }
82
83    public static <T> String mismatchDescription(Matcher<? super T> matcher,
84        T arg) {
85        Description description = new StringDescription();
86        matcher.describeMismatch(arg, description);
87        return description.toString().trim();
88    }
89
90    public void testIsNullSafe() {
91        assertNullSafe(createMatcher());
92    }
93
94    public void testCopesWithUnknownTypes() {
95        assertUnknownTypeSafe(createMatcher());
96    }
97
98    public static class UnknownType {
99    }
100
101}
102
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 AbstractMatcherTest

    No relevant questions found for this class 😞

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)