How to use method of org.jmock.test.unit.internal.StateMachineTests class

Best Jmock-library code snippet using org.jmock.test.unit.internal.StateMachineTests.

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.internal;
2
3import java.util.HashSet;
4import java.util.Set;
5
6import junit.framework.TestCase;
7
8import org.hamcrest.StringDescription;
9import org.jmock.States;
10import org.jmock.internal.StateMachine;
11
12public class StateMachineTests extends TestCase {
13    States stateMachine = new StateMachine("stateMachineName");
14    
15    public void testIsInitiallyInNoState() {
16        for (String state : anyState) {
17            assertFalse("should not report being in state " + state, 
18                        stateMachine.is(state).isActive());
19            assertTrue("should report not being in state " + state, 
20                       stateMachine.isNot(state).isActive());
21        }
22    }
23    
24    public void testCanEnterAState() {
25        String state = "A";
26        Set<String> otherStates = except(anyState, state);
27        
28        stateMachine.is(state).activate();
29        
30        assertTrue("should report being in state " + state, 
31                   stateMachine.is(state).isActive());
32        assertFalse("should not report not being in state " + state, 
33                    stateMachine.isNot(state).isActive());
34        
35        for (String otherState : otherStates) {
36            assertFalse("should not report being in state " + otherState, 
37                        stateMachine.is(otherState).isActive());
38            assertTrue("should report not being in state " + otherState, 
39                       stateMachine.isNot(otherState).isActive());
40        }
41    }
42
43    public void testCanChangeState() {
44        String state = "B";
45        Set<String> otherStates = except(anyState, state);
46        
47        stateMachine.is("A").activate();
48        stateMachine.is(state).activate();
49        
50        assertTrue("should report being in state " + state, 
51                   stateMachine.is(state).isActive());
52        assertFalse("should not report not being in state " + state, 
53                    stateMachine.isNot(state).isActive());
54        
55        for (String otherState : otherStates) {
56            assertFalse("should not report being in state " + otherState, 
57                        stateMachine.is(otherState).isActive());
58            assertTrue("should report not being in state " + otherState, 
59                       stateMachine.isNot(otherState).isActive());
60        }
61    }
62
63    public void testCanBePutIntoAnInitialState() {
64        String initialState = "A";
65        Set<String> otherStates = except(anyState, initialState);
66        
67        stateMachine.startsAs(initialState);
68        
69        assertTrue("should report being in state " + initialState, 
70                   stateMachine.is(initialState).isActive());
71        assertFalse("should not report not being in state " + initialState, 
72                    stateMachine.isNot(initialState).isActive());
73        
74        for (String otherState : otherStates) {
75            assertFalse("should not report being in state " + otherState, 
76                        stateMachine.is(otherState).isActive());
77            assertTrue("should report not being in state " + otherState, 
78                       stateMachine.isNot(otherState).isActive());
79        }
80    }
81    
82    public void testCanBePutIntoANewState() {
83        String nextState = "B";
84        
85        Set<String> otherStates = except(anyState, nextState);
86        stateMachine.startsAs("A");
87        
88        stateMachine.become(nextState);
89        
90        assertTrue("should report being in state " + nextState, 
91                   stateMachine.is(nextState).isActive());
92        assertFalse("should not report not being in state " + nextState, 
93                    stateMachine.isNot(nextState).isActive());
94        
95        for (String otherState : otherStates) {
96            assertFalse("should not report being in state " + otherState, 
97                        stateMachine.is(otherState).isActive());
98            assertTrue("should report not being in state " + otherState, 
99                       stateMachine.isNot(otherState).isActive());
100        }
101    }
102    
103    public void testDescribesItselfAsNameAndCurrentState() {
104        assertEquals("description with no current state",
105                     "stateMachineName has no current state", StringDescription.toString(stateMachine));
106        
107        stateMachine.is("stateName").activate();
108        
109        assertEquals("description with a current state",
110                     "stateMachineName is stateName", StringDescription.toString(stateMachine));
111        assertEquals("description with a current state from toString",
112                     "stateMachineName is stateName", stateMachine.toString());
113    }
114    
115    public void testHasSelfDescribingStates() {
116        assertEquals("stateMachineName is A", StringDescription.toString(stateMachine.is("A")));
117        assertEquals("stateMachineName is not A", StringDescription.toString(stateMachine.isNot("A")));
118    }
119    
120    private <T> Set<T> except(Set<T> s, T e) {
121        Set<T> result = new HashSet<T>(s);
122        result.remove(e);
123        return result;
124    }
125    
126    private final Set<String> anyState = new HashSet<String>() {{
127        add("A");
128        add("B");
129        add("C");
130        add("D");
131    }};
132}
133
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)