Best Jmock-library code snippet using org.jmock.AbstractExpectations.same
Source:SimpleMovementRuleTest.java
...18import static org.hamcrest.CoreMatchers.is;19import static org.hamcrest.CoreMatchers.not;20import static org.hamcrest.MatcherAssert.assertThat;21import static org.jmock.AbstractExpectations.returnValue;22import static org.jmock.AbstractExpectations.same;23import static org.zet.cellularautomaton.algorithm.rule.RuleTestMatchers.executeableOn;24import java.util.List;25import java.util.Collections;26import java.util.function.Function;27import org.jmock.Expectations;28import org.jmock.Mockery;29import org.junit.Test;30import org.zet.cellularautomaton.EvacCell;31import org.zet.cellularautomaton.EvacCellInterface;32import org.zet.cellularautomaton.Individual;33import org.zet.cellularautomaton.RoomCell;34import org.zet.cellularautomaton.algorithm.computation.Computation;35import org.zet.cellularautomaton.algorithm.state.EvacuationState;36import org.zet.cellularautomaton.EvacuationCellularAutomaton;37import org.zet.cellularautomaton.algorithm.state.IndividualProperty;38import org.zet.cellularautomaton.results.MoveAction;39import org.zetool.rndutils.RandomUtils;40import org.zetool.rndutils.generators.MersenneTwister;41/**42 *43 * @author Jan-Philipp Kappmeier44 */45public class SimpleMovementRuleTest {46 private static class FakeSimpleMovementRule extends SimpleMovementRule {47 private final EvacCell targetCell;48 int counter = 0;49 public FakeSimpleMovementRule(EvacCell targetCell) {50 this.targetCell = targetCell;51 }52 53 @Override54 public EvacCellInterface selectTargetCell(EvacCellInterface cell, List<EvacCellInterface> targets) {55 counter++;56 return targetCell;57 }58 59 @Override60 protected List<EvacCellInterface> computePossibleTargets(EvacCellInterface fromCell, boolean onlyFreeNeighbours) {61 return Collections.emptyList();62 }63 @Override64 public boolean executableOn(EvacCellInterface cell) {65 return true;66 }67 };68 @Test69 public void executeableIfNotEmpty() {70 SimpleMovementRule rule = new SimpleMovementRule();71 EvacCell cell = new RoomCell(0, 0);72 assertThat(rule, is(not(executeableOn(cell))));73 Individual i = new Individual(0, 0, 0, 0, 0, 0, 1, 0);74 cell.getState().setIndividual(i);75 assertThat(rule, is(executeableOn(cell)));76 }77 @Test78 public void noActionIfSameCell() {79 EvacCell testCell = new RoomCell(0, 0);80 FakeSimpleMovementRule rule = new FakeSimpleMovementRule(testCell) {81 @Override82 public MoveAction move(EvacCellInterface from, EvacCellInterface targetCell) {83 throw new AssertionError("Move should not be called!");84 }85 };86 rule.execute(testCell);87 assertThat(rule.counter, is(equalTo(1)));88 }89 @Test90 public void moveExecuted() {91 EvacCell startCell = new RoomCell(0, 0);92 EvacCell expectedTargetCell = new RoomCell(0, 0);93 FakeSimpleMovementRule rule = new FakeSimpleMovementRule(expectedTargetCell) {94 @Override95 public MoveAction move(EvacCellInterface from, EvacCellInterface to) {96 assertThat(from, is(same(startCell)));97 assertThat(to, is(same(expectedTargetCell)));98 return MoveAction.NO_MOVE;99 }100 };101 rule.execute(startCell);102 assertThat(rule.counter, is(equalTo(1)));103 }104 @Test105 public void moveCallsCellularAutomaton() {106 Mockery context = new Mockery();107 EvacuationState es = context.mock(EvacuationState.class);108 EvacCell startCell = new RoomCell(0, 0);109 EvacCell targetCell = new RoomCell(0, 0);110 Individual i = new Individual(0, 0, 0, 0, 0, 0, 1, 0);111 IndividualProperty ip = new IndividualProperty(i);112 ip.setStepEndTime(4);113 ip.setStepStartTime(2.4);114 startCell.getState().setIndividual(i);115 context.checking(new Expectations() {116 {117 allowing(es).propertyFor(i);118 will(returnValue(ip));119 }120 });121 SimpleMovementRule rule = new SimpleMovementRule();122 rule.setEvacuationState(es);123 MoveAction action = rule.move(startCell, targetCell);124 assertThat(action.getIndividualNumber(), is(equalTo(0)));125 assertThat(action.startTime(), is(equalTo(2.4)));126 assertThat(action.arrivalTime(), is(equalTo(4.0)));127 }128 @Test129 public void sameCellSelectedIfEmptyTargetList() {130 List<EvacCellInterface> targets = Collections.emptyList();131 SimpleMovementRule rule = new SimpleMovementRule();132 EvacCell currentCell = new RoomCell(0, 0);133 EvacCellInterface selectedCell = rule.selectTargetCell(currentCell, targets);134 assertThat(selectedCell, is(same(currentCell)));135 }136 @Test137 public void singleCellSelected() {138 SimpleMovementRule rule = new SimpleMovementRule();139 EvacCellInterface currentCell = new RoomCell(0, 0);140 Individual i = new Individual(0, 0, 0, 0, 0, 0, 1, 0);141 currentCell.getState().setIndividual(i); 142 EvacCell targetCell = new RoomCell(0, 0);143 List<EvacCellInterface> targets = Collections.singletonList(targetCell);144 Mockery context = new Mockery();145 EvacuationState es = context.mock(EvacuationState.class);146 Computation c = context.mock(Computation.class);147 EvacuationCellularAutomaton ca = context.mock(EvacuationCellularAutomaton.class);148 context.checking(new Expectations() {149 {150 allowing(es).getCellularAutomaton();151 will(returnValue(ca));152 allowing(c).effectivePotential(with(i), with(targetCell), with(any(Function.class)));153 will(returnValue(1.0));154 }155 });156 rule.setEvacuationState(es);157 rule.setComputation(c);158 159 // TODO: inject mock instance160 RandomUtils.getInstance().setRandomGenerator(new MersenneTwister());161 EvacCellInterface selectedCell = rule.selectTargetCell(currentCell, targets);162 assertThat(selectedCell, is(same(targetCell)));163 }164}...
Source:InitialPotentialRandomRuleTest.java
...18import static org.hamcrest.CoreMatchers.is;19import static org.hamcrest.CoreMatchers.not;20import static org.hamcrest.MatcherAssert.assertThat;21import static org.jmock.AbstractExpectations.returnValue;22import static org.jmock.AbstractExpectations.same;23import static org.zet.cellularautomaton.algorithm.rule.RuleTestMatchers.executeableOn;24import java.util.Collections;25import java.util.LinkedList;26import java.util.List;27import org.jmock.Expectations;28import org.jmock.Mockery;29import org.junit.Before;30import org.junit.Test;31import org.zet.cellularautomaton.DeathCause;32import org.zet.cellularautomaton.EvacCell;33import org.zet.cellularautomaton.EvacuationCellularAutomaton;34import org.zet.cellularautomaton.Exit;35import org.zet.cellularautomaton.Individual;36import org.zet.cellularautomaton.IndividualBuilder;37import org.zet.cellularautomaton.Room;38import org.zet.cellularautomaton.RoomCell;39import org.zet.cellularautomaton.algorithm.state.EvacuationState;40import org.zet.cellularautomaton.algorithm.state.EvacuationStateControllerInterface;41import org.zet.cellularautomaton.algorithm.state.IndividualProperty;42import org.zet.cellularautomaton.potential.Potential;43import org.zet.cellularautomaton.potential.StaticPotential;44import org.zet.cellularautomaton.results.DieAction;45import org.zet.cellularautomaton.statistic.CAStatisticWriter;46import org.zetool.rndutils.RandomUtils;47import org.zetool.rndutils.generators.MersenneTwister;48/**49 *50 * @author Jan-Philipp Kappmeier51 */52public class InitialPotentialRandomRuleTest {53 private final static IndividualBuilder INDIVIDUAL_BUILDER = new IndividualBuilder();54 private final Mockery context = new Mockery();55 private InitialPotentialRandomRule rule;56 private EvacCell cell;57 private Individual individual;58 private IndividualProperty ip;59 private EvacuationCellularAutomaton eca;60 private EvacuationStateControllerInterface ec;61 private EvacuationState es;62 private List<Exit> exitList;63 @Before64 public void init() {65 rule = new InitialPotentialRandomRule();66 Room room = context.mock(Room.class);67 es = context.mock(EvacuationState.class);68 eca = context.mock(EvacuationCellularAutomaton.class);69 individual = INDIVIDUAL_BUILDER.build();70 ip = new IndividualProperty(individual);71 ec = context.mock(EvacuationStateControllerInterface.class);72 exitList = new LinkedList<>();73 context.checking(new Expectations() {74 {75 allowing(es).getCellularAutomaton();76 will(returnValue(eca));77 allowing(room).getID();78 will(returnValue(1));79 allowing(room).getXOffset();80 allowing(room).getYOffset();81 allowing(room).getFloor();82 allowing(es).getStatisticWriter();83 will(returnValue(new CAStatisticWriter(es)));84 allowing(es).propertyFor(individual);85 will(returnValue(ip));86 allowing(eca).getExits();87 will(returnValue(exitList));88 }89 });90 cell = new RoomCell(1, 0, 0, room);91 ip.setCell(cell);92 cell.getState().setIndividual(individual);93 rule.setEvacuationState(es);94 }95 @Test96 public void testAppliccableIfNotEmpty() {97 cell = new RoomCell(0, 0);98 assertThat(rule, is(not(executeableOn(cell))));99 individual = INDIVIDUAL_BUILDER.build();100 context.checking(new Expectations() {101 {102 allowing(es).propertyFor(individual);103 will(returnValue(ip));104 }105 });106 cell.getState().setIndividual(individual);107 assertThat(rule, is(executeableOn(cell)));108 }109 @Test110 public void testNotApplicableIfPotentialSet() {111 StaticPotential sp = new StaticPotential();112 ip.setStaticPotential(sp);113 assertThat(rule, is(not(executeableOn(cell))));114 }115 @Test116 public void testDeadIfNoPotentials() {117 DieAction a = (DieAction)rule.execute(cell).get();118 assertThat(a.getDeathCause(), is(equalTo(DeathCause.EXIT_UNREACHABLE)));119 assertThat(a.getIndividual(), is(equalTo(individual)));120 }121 @Test122 public void testDeadIfPotentialsBad() {123 StaticPotential sp = new StaticPotential();124 addStaticPotential(sp);125 DieAction a = (DieAction)rule.execute(cell).get();126 assertThat(a.getDeathCause(), is(equalTo(DeathCause.EXIT_UNREACHABLE)));127 assertThat(a.getIndividual(), is(equalTo(individual)));128 }129 @Test130 public void testSinglePotentialTaken() {131 StaticPotential sp = new StaticPotential();132 sp.setPotential(cell, 1);133 addStaticPotential(sp);134 RandomUtils.getInstance().setRandomGenerator(new MersenneTwister());135 rule.execute(cell);136 assertThat(ip.getStaticPotential(), is(same(sp)));137 }138 @Test139 public void testRandomPotentialTaken() {140 // Need to insert seed into rule to manipulate random decision141// rule.execute(cell);142// assertThat(i.isDead(), is(false));143// assertThat(i.getDeathCause(), is(nullValue()));144// assertThat(i.getStaticPotential(), is(same(targetPotential)));145 }146 private void addStaticPotential(Potential p) {147 Exit e = new Exit("", Collections.emptyList());148 exitList.add(e);149 context.checking(new Expectations() {150 {151 allowing(eca).getPotentialFor(e);152 will(returnValue(p));153 }154 });155 }156}...
Source:TreeTableClientTableManagerTest.java
1package io.deephaven.treetable;2import io.deephaven.engine.table.Table;3import io.deephaven.engine.liveness.LivenessReferent;4import io.deephaven.engine.table.impl.QueryTableTestBase;5import org.hamcrest.Matcher;6import org.jmock.AbstractExpectations;7import java.lang.ref.WeakReference;8import java.lang.reflect.InvocationHandler;9import java.lang.reflect.Method;10import java.lang.reflect.Proxy;11import java.util.concurrent.ExecutionException;12import java.util.concurrent.ExecutorService;13import java.util.concurrent.Executors;14import java.util.concurrent.Future;15import java.util.function.Consumer;16import java.util.stream.IntStream;17public class TreeTableClientTableManagerTest extends QueryTableTestBase {18 private TreeTableClientTableManager.Client[] clients;19 private SnapshotState mockSnapshotState;20 private final ExecutorService pool = Executors.newFixedThreadPool(1);21 static class DelayingReleaseProxy implements InvocationHandler {22 private static Method RELEASE_METHOD;23 private static Method IS_REFRESHING;24 private static Method TRY_RETAIN;25 private static Method GET_WEAK_REFERENCE;26 static {27 try {28 RELEASE_METHOD = LivenessReferent.class.getMethod("dropReference");29 TRY_RETAIN = LivenessReferent.class.getMethod("tryRetainReference");30 IS_REFRESHING = Table.class.getMethod("isRefreshing");31 GET_WEAK_REFERENCE = LivenessReferent.class.getMethod("getWeakReference");32 } catch (NoSuchMethodException e) {33 e.printStackTrace();34 }35 }36 @Override37 public Object invoke(Object proxy, Method method, Object[] args) throws InterruptedException {38 if (method.equals(RELEASE_METHOD)) {39 // Sleep for a bit so we can generate CMEs40 Thread.sleep(250);41 } else if (method.equals(IS_REFRESHING)) {42 return true;43 } else if (method.equals(TRY_RETAIN)) {44 return true;45 } else if (method.equals(GET_WEAK_REFERENCE)) {46 return new WeakReference(proxy);47 }48 return null;49 }50 }51 private Table makeProxy() {52 return (Table) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {Table.class},53 new DelayingReleaseProxy());54 }55 @Override56 protected void setUp() throws Exception {57 super.setUp();58 clients = new TreeTableClientTableManager.Client[5];59 for (int i = 0; i < 5; i++) {60 clients[i] = mock(TreeTableClientTableManager.Client.class, "CLIENT_" + i);61 final int myI = i;62 checking(new Expectations() {63 {64 allowing(clients[myI]).addDisconnectHandler(65 with(AbstractExpectations.<Consumer<TreeTableClientTableManager.Client>>anything()));66 allowing(clients[myI]).removeDisconnectHandler(67 with(AbstractExpectations.<Consumer<TreeTableClientTableManager.Client>>anything()));68 }69 });70 }71 mockSnapshotState = mock(SnapshotState.class);72 }73 /**74 * This method tests for regression of the ConcurrentModificationException documented by IDS-513475 */76 public void testIds5134CME() throws ExecutionException, InterruptedException {77 final TreeTableClientTableManager.ClientState stateObj = TreeTableClientTableManager.DEFAULT.get(clients[0]);78 final TreeTableClientTableManager.TreeState treeState00 = stateObj.getTreeState(0, () -> mockSnapshotState);79 assertSame(mockSnapshotState, treeState00.getUserState());80 // Retain a few tables81 final Table[] proxies = IntStream.range(0, 10).mapToObj((i) -> makeProxy()).toArray(Table[]::new);82 for (int i = 0; i < 5; i++) {83 treeState00.retain(i, proxies[i]);84 }85 Future bacon = pool.submit(treeState00::releaseAll);86 for (int i = 5; i < 10; i++) {87 treeState00.retain(i, proxies[i]);88 try {89 Thread.sleep(250);90 } catch (InterruptedException e) {91 e.printStackTrace();92 }93 }94 bacon.get();95 }96}...
same
Using AI Code Generation
1package org.jmock;2public class Expectations extends AbstractExpectations {3public Expectations() {4}5public Expectations(String name) {6super(name);7}8}9package org.jmock;10public class Expectations extends AbstractExpectations {11public Expectations() {12}13public Expectations(String name) {14super(name);15}16}17package org.jmock;18public class Expectations extends AbstractExpectations {19public Expectations() {20}21public Expectations(String name) {22super(name);23}24}25package org.jmock;26public class Expectations extends AbstractExpectations {27public Expectations() {28}29public Expectations(String name) {30super(name);31}32}33package org.jmock;34public class Expectations extends AbstractExpectations {35public Expectations() {36}37public Expectations(String name) {38super(name);39}40}41package org.jmock;42public class Expectations extends AbstractExpectations {43public Expectations() {44}45public Expectations(String name) {46super(name);47}48}49package org.jmock;50public class Expectations extends AbstractExpectations {51public Expectations() {52}53public Expectations(String name) {54super(name);55}56}57package org.jmock;58public class Expectations extends AbstractExpectations {59public Expectations() {60}61public Expectations(String name) {62super(name);63}64}65package org.jmock;66public class Expectations extends AbstractExpectations {67public Expectations() {68}69public Expectations(String name) {70super(name);71}72}73package org.jmock;
same
Using AI Code Generation
1package org.jmock;2import org.jmock.core.DynamicMock;3import org.jmock.core.DynamicMockError;4import org.jmock.core.InvocationDispatcher;5import org.jmock.core.InvocationExpectation;6import org.jmock.core.InvocationMatcher;7import org.jmock.core.InvocationMocker;8import org.jmock.core.InvocationRecorder;9import org.jmock.core.InvocationStubber;10import org.jmock.core.Stub;11import org.jmock.core.StubError;12import org.jmock.core.constraint.Constraint;13import org.jmock.core.constraint.IsEqual;14import org.jmock.core.constraint.IsAnything;15import org.jmock.core.constraint.IsSame;16import org.jmock.core.constraint.IsTypeCompatible;17import org.jmock.core.constraint.IsAnything;18import org.jmock.core.constraint.IsEqual;19import org.jmock.core.constraint.IsSame;20import org.jmock.core.constraint.IsTypeCompatible;21import org.jmock.core.constraint.IsInstanceOf;22import org.jmock.core.constraint.IsIn;23import org.jmock.core.constraint.IsNot;24import org.jmock.core.constraint.IsSame;25import org.jmock.core.constraint.IsTypeCompatible;26import org.jmock.core.constraint.IsInstanceOf;27import org.jmock.core.constraint.IsIn;28import org.jmock.core.constraint.IsNot;29import org.jmock.core.constraint.IsSame;30import org.jmock.core.constraint.IsTypeCompatible;31import org.jmock.core.constraint.IsInstanceOf;32import org.jmock.core.constraint.IsIn;33import org.jmock.core.constraint.IsNot;34import org.jmock.core.constraint.IsSame;35import org.jmock.core.constraint.IsTypeCompatible;36import org.jmock.core.constraint.IsInstanceOf;37import org.jmock.core.constraint.IsIn;38import org.jmock.core.constraint.IsNot;39import org.jmock.core.constraint.IsSame;40import org.jmock.core.constraint.IsTypeCompatible;41import org.jmock.core.constraint.IsInstanceOf;42import org.jmock.core.constraint.IsIn;43import org.jmock.core.constraint.IsNot;44import org.jmock.core.constraint.IsSame;45import org.jmock.core.constraint.IsTypeCompatible;46import org.jmock.core.constraint.IsInstanceOf;47import org.jmock.core.constraint.IsIn;48import org.jmock.core.constraint.IsNot;49import org.jmock.core.constraint.IsSame;50import org.jmock.core.constraint.IsTypeCompatible;51import org.jmock.core.constraint.IsInstanceOf;52import org.jmock.core.constraint.IsIn;53import org.jmock.core.constraint.IsNot;54import org.jmock.core.constraint.IsSame
same
Using AI Code Generation
1List mockList = (List)mock(List.class);2List mockList = mockery.mock(List.class);3List mockList = mockery.mock(List.class, "mockList");4List mockList = mockery.mock(List.class, "mockList", new Class[]{List.class});5List mockList = mockery.mock(List.class, "mockList", new Class[]{List.class}, new Object[]{null});6List mockList = mockery.mock(List.class, "mockList", new Class[]{List.class}, new Object[]{null}, null);7List mockList = mockery.mock(List.class, "mockList", new Class[]{List.class}, new Object[]{null}, null, null);8List mockList = mockery.mock(List.class, "mockList", new Class[]{List.class}, new Object[]{null}, null, null, null);9List mockList = mockery.mock(List.class, "mockList", new Class[]{List.class}, new Object[]{null}, null, null, null, null);10List mockList = mockery.mock(List.class, "mockList", new Class[]{List
same
Using AI Code Generation
1public class Expectations extends AbstractExpectations {2 public Expectations() {3 super();4 }5 public Expectations(final String name) {6 super(name);7 }8 public Expectations(final String name, final Object mockObject) {9 super(name, mockObject);10 }11 public Expectations(final String name, final Object mockObject,12 final int defaultAction) {13 super(name, mockObject, defaultAction);14 }15 public Expectations(final String name, final Object mockObject,16 final int defaultAction, final int defaultResult) {17 super(name, mockObject, defaultAction, defaultResult);18 }19}20public class MyTest extends TestCase {21 public void testSomething() {22 Mockery context = new Mockery();23 final MyInterface myInterface = context.mock(MyInterface.class,24 new Expectations("myInterface"));25 context.checking(new Expectations() {26 {27 allowing(myInterface).doSomething();28 }29 });30 }31}32public class MyInterface {33 public void doSomething() {34 }35}
same
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) {3 Mockery context = new Mockery();4 final String s = "hello";5 final String s1 = "hello";6 final String s2 = "world";7 context.checking(new Expectations() {8 {9 oneOf(s).equals(s1);10 will(returnValue(true));11 oneOf(s).equals(s2);12 will(returnValue(false));13 }14 });15 System.out.println(s.equals(s1));16 System.out.println(s.equals(s2));17 context.assertIsSatisfied();18 }19}20Related posts: JMock – How to use with() method? JMock – How to use allowing() method? JMock – How to use never() method? JMock – How to use atMost() method? JMock – How to use atLeast() method? JMock – How to
same
Using AI Code Generation
1public class 1 extends TestCase {2 public void test1() {3 Mock mock = new Mock(Interface1.class);4 mock.expects(once()).method("method1");5 Interface1 i1 = (Interface1)mock.proxy();6 i1.method1();7 mock.verify();8 }9}10public class 2 extends TestCase {11 public void test2() {12 Mock mock = new Mock(Interface1.class);13 mock.expects(once()).method("method2").will(returnValue("Hello"));14 Interface1 i1 = (Interface1)mock.proxy();15 assertEquals("Hello", i1.method2());16 mock.verify();17 }18}19public class 3 extends TestCase {20 public void test3() {21 Mock mock = new Mock(Interface1.class);22 mock.expects(once()).method("method3").with(eq("Hello")).will(returnValue("Hello"));23 Interface1 i1 = (Interface1)mock.proxy();24 assertEquals("Hello", i1.method3("Hello"));25 mock.verify();26 }27}28public class 4 extends TestCase {29 public void test4() {30 Mock mock = new Mock(Interface1.class);31 mock.expects(once()).method("method4").with(eq("Hello"), eq("World")).will(returnValue("Hello"));32 Interface1 i1 = (Interface1)mock.proxy();33 assertEquals("Hello", i1.method4("Hello", "World"));34 mock.verify();35 }36}37public class 5 extends TestCase {38 public void test5() {39 Mock mock = new Mock(Interface1.class
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!