How to use method of org.jmock.internal.InvocationDiverter class

Best Jmock-library code snippet using org.jmock.internal.InvocationDiverter.

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 junit.framework.TestCase;
4
5import org.jmock.api.Invocation;
6import org.jmock.internal.InvocationDiverter;
7import org.jmock.test.unit.support.StubInvokable;
8
9public class InvocationDiverterTests extends TestCase {
10    public interface TargetInterface {
11        void doSomething();
12    }
13    
14    public class Target implements TargetInterface {
15        public boolean wasInvoked = false;
16        
17        public void doSomething() {
18            wasInvoked = true;
19        }
20    }
21    
22    public interface OtherInterface {
23        void doSomethingElse();
24    }
25    
26    Target target = new Target();
27    StubInvokable next = new StubInvokable();
28    
29    InvocationDiverter<TargetInterface> diverter = 
30        new InvocationDiverter<TargetInterface>(TargetInterface.class, target, next);
31
32    
33    public void testAppliesInvocationToGivenObjectIfInvokedMethodDeclaredInGivenClass() throws Throwable {
34        Invocation invocation = 
35            new Invocation("invokedObject", 
36                           TargetInterface.class.getMethod("doSomething"), 
37                           Invocation.NO_PARAMETERS);
38        
39        diverter.invoke(invocation);
40        
41        assertTrue("target should have been invoked", 
42                   target.wasInvoked);
43        assertTrue("next should not have been invoked",
44                   !next.wasInvoked);
45    }
46    
47    public void testPassesInvocationToNextIfInvocationNotDeclaredInGivenClass() throws Throwable {
48        Invocation invocation = 
49            new Invocation("invokedObject", 
50                           OtherInterface.class.getMethod("doSomethingElse"), 
51                           Invocation.NO_PARAMETERS);
52        
53        diverter.invoke(invocation);
54        
55        assertTrue("target should not have been invoked", 
56                   !target.wasInvoked);
57        assertTrue("next should have been invoked",
58                   next.wasInvoked);
59    }
60    
61    public void testDelegatesToStringToNext() {
62        next.toStringResult = "next.toStringResult";
63        
64        assertEquals(next.toStringResult, diverter.toString());
65    }
66}
67
Full Screen
copy
1package org.jmock.internal;
2
3import org.jmock.api.Invocation;
4import org.jmock.api.Invokable;
5
6public class InvocationDiverter<T> implements Invokable {
7    private final Class<T> declaringType;
8    private final T target;
9    private final Invokable next;
10
11    public InvocationDiverter(Class<T> declaringType, T target, Invokable next) {
12        this.declaringType = declaringType;
13        this.target = target;
14        this.next = next;
15    }
16    
17    @Override
18    public String toString() {
19        return next.toString();
20    }
21    
22    public Object invoke(Invocation invocation) throws Throwable {
23        if (invocation.getInvokedMethod().getDeclaringClass() == declaringType) {
24            return invocation.applyTo(target);
25        }
26        else {
27            return next.invoke(invocation);
28        }
29    }
30}
31
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 used method in InvocationDiverter

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)