How to use CustomAction class of org.jmock.lib.action package

Best Jmock-library code snippet using org.jmock.lib.action.CustomAction

Source:BaseCachedJMockTestCase.java Github

copy

Full Screen

...15import org.jmock.api.Invokable;16import org.jmock.auto.internal.Mockomatic;17import org.jmock.imposters.ByteBuddyClassImposteriser;18import org.jmock.internal.ExpectationBuilder;19import org.jmock.lib.action.CustomAction;20import org.jmock.lib.concurrent.Synchroniser;21import java.lang.reflect.*;22import java.util.Arrays;23import java.util.HashMap;24import java.util.Map;25import java.util.concurrent.atomic.AtomicInteger;26abstract public class BaseCachedJMockTestCase extends TestCase {27 protected final Mockery context;28 {29 // use an initializer rather than setUp so forgetting to30 // call super.setUp won't use the wrong imposteriser31 context = new Mockery();32 context.setThreadingPolicy(new Synchroniser());33 context.setImposteriser(CachingImposteriser.INSTANCE);34 new Mockomatic(context).fillIn(this);35 }36 public <T> T mock(Class<T> tClass) {37 return context.mock(tClass);38 }39 public <T> T mock(Class<T> tClass, String s) {40 return context.mock(tClass, s);41 }42 public States states(String s) {43 return context.states(s);44 }45 public Sequence sequence(String s) {46 return context.sequence(s);47 }48 public void checking(ExpectationBuilder expectationBuilder) {49 context.checking(expectationBuilder);50 }51 public void assertIsSatisfied() {52 context.assertIsSatisfied();53 }54 @Override55 protected void tearDown() throws Exception {56 context.assertIsSatisfied();57 super.tearDown();58 }59 public static class Expectations extends org.jmock.Expectations {60 public static <T> Matcher<T> some(Class<T> type) {61 return CoreMatchers.instanceOf(type);62 }63 public static <T> Matcher<T> any(Class<T> type) {64 return CoreMatchers.anyOf(CoreMatchers.instanceOf(type), CoreMatchers.nullValue(type));65 }66 private static AtomicInteger willDoCounter = new AtomicInteger(0);67 public void willDo(final Function.Nullary<Object> proc) {68 this.currentBuilder().setAction(run(proc));69 }70 public static CustomAction run(final Function.Nullary<Object> proc) {71 return new CustomAction("willDo_" + willDoCounter.incrementAndGet()) {72 @Override73 public Object invoke(Invocation invocation) throws Throwable {74 return proc.call();75 }76 };77 }78 public void willDo(final Function.Unary<Object, Invocation> proc) {79 this.currentBuilder().setAction(run(proc));80 }81 public static CustomAction run(final Function.Unary<Object, Invocation> proc) {82 return new CustomAction("willDo_" + willDoCounter.incrementAndGet()) {83 @Override84 public Object invoke(Invocation invocation) throws Throwable {85 return proc.call(invocation);86 }87 };88 }89 }90 // ----------------------------------------------------------------91 public static class CachingImposteriser implements Imposteriser {92 public static final BaseCachedJMockTestCase.CachingImposteriser INSTANCE = new CachingImposteriser();93 private final static Class[] CONSTRUCTOR_PARAMS = {InvocationHandler.class};94 private static Map<ProxyInfo, Function.Unary<?, Invokable>> proxyInfoToConstructorMap = new HashMap<>();95 // ----------------------------------------------------------------96 @Override // from Imposteriser...

Full Screen

Full Screen

Source:ServiceProviderTimeLimiterTest.java Github

copy

Full Screen

...20import org.jmock.Expectations;21import org.jmock.Mockery;22import org.jmock.api.Invocation;23import org.jmock.integration.junit4.JUnit4Mockery;24import org.jmock.lib.action.CustomAction;25import org.jmock.lib.concurrent.Synchroniser;26import org.jmock.lib.legacy.ClassImposteriser;27import org.junit.After;28import org.junit.Before;29import org.junit.Test;30import com.google.common.util.concurrent.UncheckedTimeoutException;31import com.infinities.skyport.exception.InitializationException;32import com.infinities.skyport.model.FunctionConfiguration;33import com.infinities.skyport.model.Time;34public class ServiceProviderTimeLimiterTest {35 protected Mockery context = new JUnit4Mockery() {36 {37 setThreadingPolicy(new Synchroniser());38 setImposteriser(ClassImposteriser.INSTANCE);39 }40 };41 private ExecutorService executor;42 private ServiceProviderTimeLimiter limiter;43 private Services services;44 private Configuration configuration;45 private FunctionConfiguration fc;46 @Before47 public void setUp() throws Exception {48 fc = new FunctionConfiguration();49 Time timeout = new Time();50 timeout.setNumber(1);51 timeout.setUnit(TimeUnit.SECONDS);52 fc.setTimeout(timeout);53 executor = Executors.newCachedThreadPool();54 limiter = new ServiceProviderTimeLimiter(executor);55 services = context.mock(Services.class);56 configuration = context.mock(Configuration.class);57 }58 @After59 public void tearDown() throws Exception {60 }61 @Test62 public void testNewProxyExecute() throws InitializationException {63 fc.getTimeout().setNumber(0);64 context.checking(new Expectations() {65 {66 exactly(1).of(services).execute();67 will(new CustomAction("execute with no sleep") {68 @Override69 public Object invoke(Invocation invocation) throws Throwable {70 System.err.println("exeucte with no sleep");71 return null;72 }73 });74 exactly(1).of(configuration).getExecute();75 will(returnValue(fc));76 }77 });78 Services proxy = limiter.newProxy(services, Services.class, configuration);79 proxy.execute();80 }81 @Test82 public void testNewProxyExecuteWithZeroTimeout() throws InitializationException {83 context.checking(new Expectations() {84 {85 exactly(1).of(services).execute();86 will(new CustomAction("execute with no sleep") {87 @Override88 public Object invoke(Invocation invocation) throws Throwable {89 System.err.println("exeucte with no sleep");90 return null;91 }92 });93 exactly(1).of(configuration).getExecute();94 will(returnValue(fc));95 }96 });97 Services proxy = limiter.newProxy(services, Services.class, configuration);98 proxy.execute();99 }100 @Test(expected = UncheckedTimeoutException.class)101 public void testNewProxyExecuteWithTimeout() throws InitializationException {102 context.checking(new Expectations() {103 {104 exactly(1).of(services).execute();105 will(new CustomAction("execute with no sleep") {106 @Override107 public Object invoke(Invocation invocation) throws Throwable {108 Thread.sleep(2000l);109 System.err.println("exeucte with no sleep");110 return null;111 }112 });113 exactly(1).of(configuration).getExecute();114 will(returnValue(fc));115 }116 });117 Services proxy = limiter.newProxy(services, Services.class, configuration);118 proxy.execute();119 }120 @Test121 public void testNewProxyExecuteWithDeprecated() throws InitializationException {122 context.checking(new Expectations() {123 {124 exactly(1).of(services).deprecated();125 will(new CustomAction("execute with no sleep") {126 @Override127 public Object invoke(Invocation invocation) throws Throwable {128 Thread.sleep(2000l);129 System.err.println("exeucte with no sleep");130 return null;131 }132 });133 }134 });135 Services proxy = limiter.newProxy(services, Services.class, configuration);136 proxy.deprecated();137 }138 @Test(expected = InitializationException.class)139 public void testNewProxyInitializationFail() throws InitializationException {...

Full Screen

Full Screen

Source:CustomActionTests.java Github

copy

Full Screen

...3package org.jmock.test.unit.lib.action;4import junit.framework.TestCase;5import org.hamcrest.StringDescription;6import org.jmock.api.Invocation;7import org.jmock.lib.action.CustomAction;8public class CustomActionTests extends TestCase {9 static class ConcreteSideEffect extends CustomAction {10 public ConcreteSideEffect( String description ) {11 super(description);12 }13 public Object invoke( Invocation invocation ) throws Throwable {14 return null;15 }16 }17 public void testAppendsGivenDescription() {18 String description = "DESCRIPTION";19 CustomAction sideEffect = new ConcreteSideEffect(description);20 21 StringDescription buf = new StringDescription();22 sideEffect.describeTo(buf);23 assertEquals("should be description", description, buf.toString());24 }25}...

Full Screen

Full Screen

CustomAction

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Invocation;4import org.jmock.core.Stub;5import org.jmock.lib.action.CustomAction;6import org.jmock.lib.action.ReturnValueAction;7{8 public void testCustomAction()9 {10 Mock mock = mock(Stub.class);11 mock.stubs().method("invoke").will(new CustomAction("invoke")12 {13 public Object invoke(Invocation invocation) throws Throwable14 {15 return new Integer(1);16 }17 });18 assertEquals(new Integer(1), mock.proxy().invoke(null));19 }20 public void testCustomActionWithReturnValueAction()21 {22 Mock mock = mock(Stub.class);23 mock.stubs().method("invoke").will(new CustomAction("invoke")24 {25 public Object invoke(Invocation invocation) throws Throwable26 {27 return new Integer(1);28 }29 }.andThen(new ReturnValueAction(new Integer(2))));30 assertEquals(new Integer(1), mock.proxy().invoke(null));31 assertEquals(new Integer(2), mock.proxy().invoke(null));32 }33}34import org.jmock.Mock;35import org.jmock.MockObjectTestCase;36import org.jmock.core.Invocation;37import org.jmock.core.Stub;38import org.jmock.lib.action.CustomAction;39import org.jmock.lib.action.ReturnValueAction;40{41 public void testCustomAction()42 {43 Mock mock = mock(Stub.class);44 mock.stubs().method("invoke").will(new CustomAction("invoke")45 {46 public Object invoke(Invocation invocation) throws Throwable47 {48 return new Integer(1);49 }50 });51 assertEquals(new Integer(1), mock.proxy().invoke(null));52 }53 public void testCustomActionWithReturnValueAction()54 {55 Mock mock = mock(Stub.class);56 mock.stubs().method("invoke").will(new CustomAction("invoke")57 {58 public Object invoke(Invocation invocation) throws Throwable59 {60 return new Integer(1);61 }62 }.andThen(new ReturnValueAction(new Integer(2))));63 assertEquals(new Integer(1), mock.proxy().invoke(null));64 assertEquals(new Integer(2), mock.proxy().invoke(null));65 }66}

Full Screen

Full Screen

CustomAction

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.action.CustomAction;4public class CustomActionTest extends MockObjectTestCase {5 public void testCustomAction() {6 Mock mock = mock(Interface.class);7 mock.expects(once()).method("doSomething").will(new CustomAction("Custom Action") {8 public Object invoke(org.jmock.MockObjectInvocation invocation) throws Throwable {9 return "Hello World";10 }11 });12 Interface i = (Interface) mock.proxy();13 assertEquals("Hello World", i.doSomething());14 }15 public interface Interface {16 public String doSomething();17 }18}19import org.jmock.Mock;20import org.jmock.MockObjectTestCase;21import org.jmock.lib.action.CustomAction;22public class CustomActionTest extends MockObjectTestCase {23 public void testCustomAction() {24 Mock mock = mock(Interface.class);25 mock.expects(once()).method("doSomething").will(new CustomAction("Custom Action") {26 public Object invoke(org.jmock.MockObjectInvocation invocation) throws Throwable {27 return "Hello World";28 }29 });30 Interface i = (Interface) mock.proxy();31 assertEquals("Hello World", i.doSomething());32 }33 public interface Interface {34 public String doSomething();35 }36}37import org.jmock.Mock;38import org.jmock.MockObjectTestCase;39import org.jmock.lib.action.CustomAction;40public class CustomActionTest extends MockObjectTestCase {41 public void testCustomAction() {42 Mock mock = mock(Interface.class);43 mock.expects(once()).method("doSomething").will(new CustomAction("Custom Action") {44 public Object invoke(org.jmock.MockObjectInvocation invocation) throws Throwable {45 return "Hello World";46 }47 });48 Interface i = (Interface) mock.proxy();49 assertEquals("Hello World", i.doSomething());50 }51 public interface Interface {52 public String doSomething();53 }54}55import org.jmock.Mock;56import org.jmock.MockObjectTestCase;57import org.jmock.lib.action.CustomAction;58public class CustomActionTest extends MockObjectTestCase {

Full Screen

Full Screen

CustomAction

Using AI Code Generation

copy

Full Screen

1package com.jmock.action;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.Invocation;5import org.jmock.lib.action.CustomAction;6import org.jmock.lib.action.ReturnValueAction;7public class CustomActionExample extends MockObjectTestCase {8 public void testCustomAction() {9 Mock mock = mock(Runnable.class);10 mock.expects(once()).method("run").will(11 new CustomAction("custom action") {12 public Object invoke(Invocation invocation)13 throws Throwable {14 .println("custom action invoked");15 return null;16 }17 });18 Runnable r = (Runnable) mock.proxy();19 r.run();20 }21}22package com.jmock.action; import org.jmock.Mock; import org.jmock.MockObjectTestCase; import org.jmock.core.Invocation; import org.jmock.lib.action.CustomAction; import org.jmock.lib.action.ReturnValueAction; public class CustomActionExample extends MockObjectTestCase { public void testCustomAction() { Mock mock = mock(Runnable.class); mock.expects(once()).method("run").will( new CustomAction("custom action") { public Object invoke(Invocation invocation) throws Throwable { System.out.println("custom action invoked"); return null; } }); Runnable r = (Runnable) mock.proxy(); r.run(); } }23package com.jmock.action; import org.jmock.Mock; import org.jmock.MockObjectTestCase; import org.jmock.core.Invocation; import org.jmock.lib.action.CustomAction; import org.jmock.lib.action.ReturnValueAction; public class CustomActionExample extends MockObjectTestCase { public void testCustomAction() { Mock mock = mock(Runnable.class); mock.expects(once()).method("run").will( new CustomAction("custom action") { public Object invoke(Invocation invocation) throws Throwable { System.out.println("custom action invoked"); return null; } }); Runnable r = (Runnable) mock.proxy(); r.run(); } }24package com.jmock.action; import org.jmock.Mock; import org.jmock.MockObjectTestCase; import org.jmock.core.Invocation; import org.jmock.lib.action.CustomAction; import org.jmock.lib.action.ReturnValueAction; public class CustomActionExample extends MockObjectTestCase { public void testCustomAction() { Mock

Full Screen

Full Screen

CustomAction

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.action.CustomAction;4public class TestCustomAction extends MockObjectTestCase{5 public void testCustomAction(){6 final String expected = "expected";7 Mock mock = mock(SomeInterface.class);8 mock.expects(once()).method("someMethod").will(9 new CustomAction("returning expected string"){10 public Object invoke(org.jmock.MockInvocation invocation)11 throws Throwable {12 return expected;13 }14 }15 );16 SomeInterface si = (SomeInterface) mock.proxy();17 assertEquals("expected string", expected, si.someMethod());18 }19}20import org.jmock.Mock;21import org.jmock.MockObjectTestCase;22import org.jmock.core.CustomAction;23public class TestCustomAction extends MockObjectTestCase{24 public void testCustomAction(){25 final String expected = "expected";26 Mock mock = mock(SomeInterface.class);27 mock.expects(once()).method("someMethod").will(28 new CustomAction("returning expected string"){29 public Object invoke(org.jmock.MockInvocation invocation)30 throws Throwable {31 return expected;32 }33 }34 );35 SomeInterface si = (SomeInterface) mock.proxy();36 assertEquals("expected string", expected, si.someMethod());37 }38}39import org.jmock.Mock;40import org.jmock.MockObjectTestCase;41import org.jmock.lib.action.CustomAction;42public class TestCustomAction extends MockObjectTestCase{43 public void testCustomAction(){44 final String expected = "expected";45 Mock mock = mock(SomeInterface.class);46 mock.expects(once()).method("someMethod").will(47 new CustomAction("returning expected string"){48 public Object invoke(org.jmock.MockInvocation invocation)49 throws Throwable {50 return expected;51 }52 }53 );54 SomeInterface si = (SomeInterface) mock.proxy();55 assertEquals("expected string", expected, si.someMethod());56 }57}58import org.jmock.Mock;59import org.jmock.MockObjectTestCase;60import org.jmock.core.CustomAction;61public class TestCustomAction extends MockObjectTestCase{

Full Screen

Full Screen

CustomAction

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.action.CustomAction;4public class 1 extends MockObjectTestCase {5 public void testMockObject() {6 Mock mockObject = mock(Interface.class);7 mockObject.expects(once()).method("method").will(returnValue("Hello World"));8 Interface proxyObject = (Interface) mockObject.proxy();9 assertEquals("Hello World", proxyObject.method());10 }11}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Jmock-library automation tests on LambdaTest cloud grid

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

Most used methods in CustomAction

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful