How to use AnnouncerTests class of org.jmock.example.announcer package

Best Jmock-library code snippet using org.jmock.example.announcer.AnnouncerTests

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.example.announcer;
2
3import java.util.EventListener;
4
5import org.jmock.Expectations;
6import org.jmock.Sequence;
7import org.jmock.integration.junit3.MockObjectTestCase;
8
9
10public class AnnouncerTests extends MockObjectTestCase {
11	public static class CheckedException extends Exception {}
12	
13	public interface Listener extends EventListener {
14		public void eventA();
15		public void eventB();
16		public void eventWithArguments(int a, int b);
17		public void badEvent() throws CheckedException;
18	}
19	
20	Announcer<Listener> announcer = Announcer.to(Listener.class);
21	
22	Listener listener1 = mock(Listener.class, "listener1");
23	Listener listener2 = mock(Listener.class, "listener2");
24	
25	@Override
26    public void setUp() {
27		announcer.addListener(listener1);
28		announcer.addListener(listener2);
29	}
30	
31	public void testAnnouncesToRegisteredListenersInOrderOfAddition() {
32		final Sequence eventOrder = sequence("eventOrder");
33		
34		checking(new Expectations() {{
35			oneOf (listener1).eventA(); inSequence(eventOrder);
36			oneOf (listener2).eventA(); inSequence(eventOrder);
37			oneOf (listener1).eventB(); inSequence(eventOrder);
38			oneOf (listener2).eventB(); inSequence(eventOrder);
39		}});
40		
41		announcer.announce().eventA();
42		announcer.announce().eventB();
43	}
44	
45	public void testPassesEventArgumentsToListeners() {
46		checking(new Expectations() {{
47			oneOf (listener1).eventWithArguments(1, 2);
48			oneOf (listener2).eventWithArguments(1, 2);
49			oneOf (listener1).eventWithArguments(3, 4);
50			oneOf (listener2).eventWithArguments(3, 4);
51		}});
52		
53		announcer.announce().eventWithArguments(1, 2);
54		announcer.announce().eventWithArguments(3, 4);
55	}
56	
57	public void testCanRemoveListeners() {
58		announcer.removeListener(listener1);
59		
60		checking(new Expectations() {{
61			oneOf (listener2).eventA();
62		}});
63		
64		announcer.announce().eventA();
65	}
66	
67	public void testDoesNotAllowListenersToThrowCheckedExceptions() throws Exception {
68		checking(new Expectations() {{
69			allowing (listener1).badEvent(); will(throwException(new CheckedException()));
70		}});
71		
72		try {
73			announcer.announce().badEvent();
74			fail("should have thrown UnsupportedOperationException");
75		}
76		catch (UnsupportedOperationException expected) {}
77	}
78}
79
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 AnnouncerTests

    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)