How to use currentBuilder method of org.jmock.AbstractExpectations class

Best Jmock-library code snippet using org.jmock.AbstractExpectations.currentBuilder

Source:AbstractExpectations.java Github

copy

Full Screen

...22public abstract class AbstractExpectations implements ExpectationBuilder,23 CardinalityClause, ArgumentConstraintPhrases, ActionClause 24{25 private List<InvocationExpectationBuilder> builders = new ArrayList<InvocationExpectationBuilder>();26 private InvocationExpectationBuilder currentBuilder = null;27 /**28 * Syntactic sugar for specifying arguments that are matchers for primitive types29 * or are untyped matchers.30 */31 protected final WithClause with = new WithClause() {32 public boolean booleanIs(Matcher<?> matcher) {33 addParameterMatcher(matcher);34 return false;35 }36 public byte byteIs(Matcher<?> matcher) {37 addParameterMatcher(matcher);38 return 0;39 }40 public char charIs(Matcher<?> matcher) {41 addParameterMatcher(matcher);42 return 0;43 }44 public double doubleIs(Matcher<?> matcher) {45 addParameterMatcher(matcher);46 return 0;47 }48 public float floatIs(Matcher<?> matcher) {49 addParameterMatcher(matcher);50 return 0;51 }52 public int intIs(Matcher<?> matcher) {53 addParameterMatcher(matcher);54 return 0;55 }56 public long longIs(Matcher<?> matcher) {57 addParameterMatcher(matcher);58 return 0;59 }60 public short shortIs(Matcher<?> matcher) {61 addParameterMatcher(matcher);62 return 0;63 }64 public <T> T is(Matcher<?> matcher) {65 addParameterMatcher(matcher);66 return null;67 }68 };69 70 71 private void initialiseExpectationCapture(Cardinality cardinality) {72 checkLastExpectationWasFullySpecified();73 74 currentBuilder = new InvocationExpectationBuilder();75 currentBuilder.setCardinality(cardinality);76 builders.add(currentBuilder);77 }78 79 public void buildExpectations(Action defaultAction, ExpectationCollector collector) {80 checkLastExpectationWasFullySpecified();81 82 for (InvocationExpectationBuilder builder : builders) {83 collector.add(builder.toExpectation(defaultAction));84 }85 }86 87 protected InvocationExpectationBuilder currentBuilder() {88 if (currentBuilder == null) {89 throw new IllegalStateException("no expectations have been specified " +90 "(did you forget to to specify the cardinality of the first expectation?)");91 }92 return currentBuilder;93 }94 95 private void checkLastExpectationWasFullySpecified() {96 if (currentBuilder != null) {97 currentBuilder.checkWasFullySpecified();98 }99 }100 101 /* 102 * Syntactic sugar103 */104 105 public ReceiverClause exactly(int count) {106 initialiseExpectationCapture(Cardinality.exactly(count));107 return currentBuilder;108 }109 110 // Makes the entire expectation more readable than one111 public <T> T oneOf(T mockObject) {112 return exactly(1).of(mockObject);113 }114 115 /**116 * @deprecated Use {@link #oneOf(Object) oneOf} instead.117 */118 public <T> T one (T mockObject) {119 return oneOf(mockObject);120 }121 122 public ReceiverClause atLeast(int count) {123 initialiseExpectationCapture(Cardinality.atLeast(count));124 return currentBuilder;125 }126 127 public ReceiverClause between(int minCount, int maxCount) {128 initialiseExpectationCapture(Cardinality.between(minCount, maxCount));129 return currentBuilder;130 }131 132 public ReceiverClause atMost(int count) {133 initialiseExpectationCapture(Cardinality.atMost(count));134 return currentBuilder;135 }136 137 public MethodClause allowing(Matcher<?> mockObjectMatcher) {138 return atLeast(0).of(mockObjectMatcher);139 }140 141 public <T> T allowing(T mockObject) {142 return atLeast(0).of(mockObject);143 }144 145 public <T> T ignoring(T mockObject) {146 return allowing(mockObject);147 }148 149 public MethodClause ignoring(Matcher<?> mockObjectMatcher) {150 return allowing(mockObjectMatcher);151 }152 153 public <T> T never(T mockObject) {154 return exactly(0).of(mockObject);155 }156 157 /*158 * protected because the byte code injected values need to be able to call this159 */160 protected void addParameterMatcher(Matcher<?> matcher) {161 currentBuilder().addParameterMatcher(matcher);162 }163 164 /**165 * For Matchers with primitive types use the <em>with</em> field, for example:166 * <pre>with.intIs(equalTo(34));</pre>167 * For untyped matchers use:168 * <pre>with.&lt;T&gt;is(equalTo(anObject));</pre>169 */170 public <T> T with(Matcher<T> matcher) {171 addParameterMatcher(matcher);172 return null;173 }174 public boolean with(boolean value) {175 addParameterMatcher(equal(value));176 return false;177 }178 179 public byte with(byte value) {180 addParameterMatcher(equal(value));181 return 0;182 }183 184 public short with(short value) {185 addParameterMatcher(equal(value));186 return 0;187 }188 189 public char with(char value) {190 addParameterMatcher(equal(value));191 return 0;192 }193 194 public int with(int value) {195 addParameterMatcher(equal(value));196 return 0;197 }198 199 public long with(long value) {200 addParameterMatcher(equal(value));201 return 0;202 }203 204 public float with(float value) {205 addParameterMatcher(equal(value));206 return 0;207 }208 209 public double with(double value) {210 addParameterMatcher(equal(value));211 return 0;212 }213 214 public <T> T with(T value) {215 addParameterMatcher(equal(value));216 return value;217 }218 219 public void will(Action action) {220 currentBuilder().setAction(action);221 }222 223 /* Common constraints224 */225 226 public static <T> Matcher<T> equal(T value) {227 return new IsEqual<T>(value);228 }229 230 public static <T> Matcher<T> same(T value) {231 return new IsSame<T>(value);232 }233 234 public static <T> Matcher<T> any(Class<T> type) {235 return CoreMatchers.any(type);236 }237 238 public static <T> Matcher<T> anything() {239 return new IsAnything<T>();240 }241 242 /**243 * @deprecated 244 * use {@link #aNonNull} or {@link #any} until type inference actually works in a future version of Java245 * @param type Class to match. Do not use for native parameters.246 * @return an IsInstanceOf matcher247 */248 @Deprecated249 public static Matcher<Object> a(Class<?> type) {250 return new IsInstanceOf(type);251 }252 /**253 * @deprecated 254 * use {@link #aNonNull} or {@link #any} until type inference actually works in a future version of Java255 * @param type Class to match. Do not use for native parameters. 256 * @return an IsInstanceOf matcher257 */258 @Deprecated259 public static Matcher<Object> an(Class<?> type) {260 return new IsInstanceOf(type);261 }262 263 public static <T> Matcher<T> aNull(Class<T> type) {264 return new IsNull<T>();265 }266 267 public static <T> Matcher<T> aNonNull(Class<T> type) {268 return new IsNot<T>(new IsNull<T>());269 }270 271 /* Common actions272 */273 274 public static Action returnValue(Object result) {275 return new ReturnValueAction(result);276 }277 278 public static Action throwException(Throwable throwable) {279 return new ThrowAction(throwable);280 }281 282 public static Action returnIterator(Collection<?> collection) {283 return new ReturnIteratorAction(collection);284 }285 286 public static <T> Action returnIterator(T ... items) {287 return new ReturnIteratorAction(items);288 }289 290 public static Action returnEnumeration(Collection<?> collection) {291 return new ReturnEnumerationAction(collection);292 }293 294 public static <T> Action returnEnumeration(T ... items) {295 return new ReturnEnumerationAction(items);296 }297 298 public static Action doAll(Action...actions) {299 return new DoAllAction(actions);300 }301 302 public static Action onConsecutiveCalls(Action...actions) {303 return new ActionSequence(actions);304 }305 306 /* Naming and ordering307 */308 309 public void when(StatePredicate predicate) {310 currentBuilder().addOrderingConstraint(new InStateOrderingConstraint(predicate));311 }312 313 public void then(State state) {314 currentBuilder().addSideEffect(new ChangeStateSideEffect(state));315 }316 317 public void inSequence(Sequence sequence) {318 currentBuilder().addInSequenceOrderingConstraint(sequence);319 }320 public void inSequences(Sequence... sequences) {321 for (Sequence sequence : sequences) {322 inSequence(sequence);323 }324 }325}...

Full Screen

Full Screen

currentBuilder

Using AI Code Generation

copy

Full Screen

1import org.jmock.Expectations;2import org.jmock.Mockery;3import org.jmock.integration.junit4.JUnitRuleMockery;4import org.jmock.lib.legacy.ClassImposteriser;5import org.junit.Rule;6import org.junit.Test;7public class JMockTest {8 public JUnitRuleMockery context = new JUnitRuleMockery() {9 {10 setImposteriser(ClassImposteriser.INSTANCE);11 }12 };13 public void test() {14 final Foo foo = context.mock(Foo.class);15 context.checking(new Expectations() {16 {17 oneOf(foo).bar();18 will(currentBuilder().returnValue("baz"));19 }20 });21 }22 public static interface Foo {23 String bar();24 }25}26import org.jmock.Expectations;27import org.jmock.Mockery;28import org.jmock.integration.junit4.JUnitRuleMockery;29import org.jmock.lib.legacy.ClassImposteriser;30import org.junit.Rule;31import org.junit.Test;32public class JMockTest {33 public JUnitRuleMockery context = new JUnitRuleMockery() {34 {35 setImposteriser(ClassImposteriser.INSTANCE);36 }37 };38 public void test() {39 final Foo foo = context.mock(Foo.class);40 context.checking(new Expectations() {41 {42 oneOf(foo).bar();43 will(returnValue("baz"));44 }45 });46 }47 public static interface Foo {48 String bar();49 }50}51The JUnitRuleMockery class also provides a checkExpectations() method that can be used to check the expectations for the current test method. This method is called automatically when the test method finishes, but it can be called earlier if required. This method can be used to check the expectations for the current test method. This method is called automatically when the test method finishes, but it can be called

Full Screen

Full Screen

currentBuilder

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.acceptance;2import org.jmock.Expectations;3import org.jmock.Mockery;4import org.jmock.Sequence;5import org.jmock.api.ExpectationBuilder;6import org.jmock.api.Imposteriser;7import org.jmock.lib.legacy.ClassImposteriser;8import org.junit.Test;9public class JUnit4ExpectationsAcceptanceTests {10 Mockery context = new Mockery();11 Imposteriser imposteriser = new ClassImposteriser();12 ExpectationBuilder expectationBuilder = context.expectations();13 Sequence sequence = context.sequence("sequence");14 public void canUseCurrentBuilderMethodToCreateExpectations() {15 final Runnable runnable = context.mock(Runnable.class, "runnable");16 context.checking(expectationBuilder17 .with(sequence)18 .oneOf(runnable).run());19 runnable.run();20 }21}22package org.jmock.test.acceptance;23import org.junit.runner.RunWith;24import org.junit.runners.Suite;25import org.junit.runners.Suite.SuiteClasses;26@RunWith(Suite.class)27@SuiteClasses({ JUnit4ExpectationsAcceptanceTests.class })28public class AllAcceptanceTests {29}30package org.jmock.test.acceptance;31import org.junit.runner.RunWith;32import org.junit.runners.Suite;33import org.junit.runners.Suite.SuiteClasses;34@RunWith(Suite.class)35@SuiteClasses({ JUnit4ExpectationsAcceptanceTests.class })36public class AllAcceptanceTests {37}38package org.jmock.test.acceptance;39import org.junit.runner.RunWith;40import org.junit.runners.Suite;41import org.junit.runners.Suite.SuiteClasses;42@RunWith(Suite.class)43@SuiteClasses({ JUnit4ExpectationsAcceptanceTests.class })44public class AllAcceptanceTests {45}46package org.jmock.test.acceptance;47import org.junit.runner.RunWith;48import org.junit.runners.Suite;49import org.junit.runners.Suite.SuiteClasses;50@RunWith(Suite.class)51@SuiteClasses({ JUnit4ExpectationsAcceptanceTests.class })52public class AllAcceptanceTests {

Full Screen

Full Screen

currentBuilder

Using AI Code Generation

copy

Full Screen

1@Grab('org.jmock:jmock:2.5.1')2import org.jmock.integration.junit4.JUnitRuleMockery3import org.jmock.Expectations4import org.jmock.Mockery5import org.jmock.integration.junit4.JUnitRuleMockery6import org.jmock.lib.legacy.ClassImposteriser7import static org.jmock.AbstractExpectations.*8class JMockTest {9 @Rule public JUnitRuleMockery context = new JUnitRuleMockery() {10 {11 setImposteriser(ClassImposteriser.INSTANCE)12 }13 }14 def "test mock"() {15 def mock = context.mock(CalculatorService.class)16 context.checking(new Expectations() {17 {18 oneOf(mock).add(1, 1)19 will(returnValue(2))20 }21 })22 def result = mock.add(1, 1)23 }24}

Full Screen

Full Screen

currentBuilder

Using AI Code Generation

copy

Full Screen

1import org.jmock.Expectations;2import org.jmock.Mockery;3import org.jmock.Sequence;4import org.jmock.api.Imposteriser;5import org.jmock.lib.legacy.ClassImposteriser;6public class JMockTest {7 public static void main(String[] args) {8 Mockery context = new Mockery() {9 {10 setImposteriser(ClassImposteriser.INSTANCE);11 }12 };13 Imposteriser imposteriser = ClassImposteriser.INSTANCE;14 final Foo foo = context.mock(Foo.class, "foo", imposteriser);15 final Bar bar = context.mock(Bar.class, "bar", imposteriser);16 final Sequence sequence = context.sequence("sequence");17 context.checking(new Expectations() {18 {19 currentBuilder().with(sequence);20 oneOf(foo).doSomething();21 oneOf(bar).doSomething();22 }23 });24 foo.doSomething();25 bar.doSomething();26 }27}28interface Foo {29 void doSomething();30}31interface Bar {32 void doSomething();33}

Full Screen

Full Screen

currentBuilder

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery2import org.jmock.Expectations3import org.jmock.lib.legacy.ClassImposteriser4class JMockTest {5 def "test"() {6 def context = new Mockery()7 context.setImposteriser(ClassImposteriser.INSTANCE)8 def mock = context.mock(TestInterface.class)9 def test = new TestClass(mock)10 context.checking(new Expectations() {11 {12 oneOf(mock).testMethod()13 }14 })15 test.testMethod()16 context.assertIsSatisfied()17 }18}19interface TestInterface {20 void testMethod()21}22class TestClass {23 TestClass(TestInterface testInterface) {24 }25 void testMethod() {26 testInterface.testMethod()27 }28}

Full Screen

Full Screen

currentBuilder

Using AI Code Generation

copy

Full Screen

1import org.jmock.integration.junit4.JUnitRuleMockery2import org.jmock.lib.legacy.ClassImposteriser3import org.junit.Rule4import org.junit.Test5import org.junit.rules.TestName6class JMockTest {7 val context = JUnitRuleMockery().apply {8 setImposteriser(ClassImposteriser.INSTANCE)9 }10 val testName = TestName()11 fun `test with custom builder`() {12 val mock = context.mock(MyInterface::class.java, testName.methodName)13 context.checking {14 oneOf(mock).myMethod()15 }16 mock.myMethod()17 }18}19interface MyInterface {20 fun myMethod()21}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful