Best Jmock-library code snippet using org.jmock.AbstractExpectations.returnValue
Source:ExperimentMgmtServiceTest.java  
...102                MakeItEasy.with(ExperimentMakers.paused, true));103        final Experiment updatedExperiment = experiment.but(MakeItEasy.with(ExperimentMakers.description, "blah")).make();104        context.checking(new Expectations() {{105            allowing(fullPetriClient).fetchExperimentById(updatedExperiment.getId());106            will(AbstractExpectations.returnValue(updatedExperiment));107            allowing(fullPetriClient).fetchAllExperiments();108            will(AbstractExpectations.returnValue(asList(updatedExperiment, experiment.make())));109        }});110        context.checking(new Expectations() {{111            allowing(fullPetriClient).updateExperiment(with(updatedExperiment.terminateAsOf(clock.getCurrentDateTime(), trigger)));112            will(AbstractExpectations.returnValue(updatedExperiment));113            oneOf(fullPetriClient).updateExperiment(with(experiment.make().terminateAsOf(clock.getCurrentDateTime(), trigger)));114            will(AbstractExpectations.returnValue(experiment.make()));115            allowing(specService).isSpecActive("", ImmutableList.of());116            will(AbstractExpectations.returnValue(true));117            oneOf(experimentEventPublisher).publish(with(isForAction(ExperimentEvent.TERMINATED)));118        }});119        ems.terminateExperiment(updatedExperiment.getId(), "", userName);120    }121    @Test(expected = IllegalArgumentException.class)122    public void recognizesInvalidFiltersForScope() throws IOException {123        final ScopeDefinition someScope = ScopeDefinition.aScopeDefinitionForAllUserTypes("someScope");124        final SpecDefinition.ExperimentSpecBuilder someSpec = SpecDefinition.ExperimentSpecBuilder.125                aNewlyGeneratedExperimentSpec(someKey).withScopes(someScope);126        final ExperimentSnapshot experimentSnapshot = anExperimentSnapshot().127                withScopes(ImmutableList.of(someScope.getName())).128                withKey(someKey).129                withOnlyForLoggedInUsers(false).130                withGroups(TEST_GROUPS_WITH_SECOND_ALWAYS_WINNING).131                withFeatureToggle(false).132                withStartDate(now).133                withEndDate(now.plusMinutes(5)).134                withFilters(asList(new WixEmployeesFilter())).135                build();136        context.checking(new Expectations() {{137            allowing(fullPetriClient).fetchSpecs();138            will(AbstractExpectations.returnValue(asList(someSpec.build())));139        }});140        ems.newExperiment(experimentSnapshot);141    }142    private boolean terminateExperimentWithSpecActive(final Experiment futureExperiment, final boolean isSpecActive) {143        context.checking(new Expectations() {{144            allowing(fullPetriClient).fetchAllExperiments();145            will(AbstractExpectations.returnValue(asList(futureExperiment)));146            allowing(fullPetriClient).fetchExperimentById(futureExperiment.getId());147            will(AbstractExpectations.returnValue(futureExperiment));148        }});149        assumingPetriClientContains(asList(futureExperiment));150        context.checking(new Expectations() {{151            allowing(fullPetriClient).updateExperiment(with(futureExperiment.terminateAsOf(152                    clock.getCurrentDateTime(), new Trigger("terminate experiment", userName))));153            will(AbstractExpectations.returnValue(futureExperiment));154            oneOf(experimentEventPublisher).publish(with(isForAction(ExperimentEvent.TERMINATED)));155            allowing(specService).isSpecActive(futureExperiment.getKey(), ImmutableList.of());156            will(AbstractExpectations.returnValue(isSpecActive));157        }});158        return ems.terminateExperiment(futureExperiment.getId(), "terminate experiment", userName);159    }160    private void assumingTimeSourceReturnsNow() {161        context.checking(new Expectations() {{162            allowing(clock).getCurrentDateTime();163            will(AbstractExpectations.returnValue(now));164        }});165    }166    private void assumingPetriClientContains(final List<Experiment> contents) {167        context.checking(new Expectations() {{168            allowing(fullPetriClient).fetchAllExperimentsGroupedByOriginalId();169            will(AbstractExpectations.returnValue(contents));170        }});171    }172    private void assumingPetriClientContains(final ImmutableList<ConductExperimentSummary> reports) {173        context.checking(new Expectations() {{174            allowing(fullPetriClient).getExperimentReport(2);175            will(AbstractExpectations.returnValue(reports));176        }});177    }178    private UiExperiment convertToUiExperiment(Experiment experiment) throws JsonProcessingException, ClassNotFoundException {179        ExperimentConverter converter = new ExperimentConverter(new IsEditablePredicate(), new NoOpFilterAdapterExtender());180        return converter.convert(experiment);181    }182    public static final String NOT_EDITABLE_SCOPE = "NOT_EDITABLE_SCOPE";183    public class IsEditablePredicate implements Predicate<Experiment> {184        @Override185        public boolean apply(@Nullable Experiment experiment) {186            return !NOT_EDITABLE_SCOPE.equals(experiment.getScope());187        }188    }189}...Source:MetaDataServiceTest.java  
...60    public void userAgentRegexListReadsValuesFromDao() {61        final UserAgentRegex userAgentRegex = new UserAgentRegex("*test", "This is a test");62        context.checking(new Expectations() {{63            allowing(metaDataDao).get(UserAgentRegex.class);64            will(AbstractExpectations.returnValue(ImmutableList.of(userAgentRegex)));65        }});66        List<FilterOption> fullList = metaDataService.getUserAgentRegexList();67        assertEquals(fullList.get(0).id(), userAgentRegex.regex());68        assertEquals(fullList.get(0).text(), userAgentRegex.description());69        assertThat(fullList.size(), is(1));70    }71    @Test72    public void userGroupsListReadFromGlobalGroupsManagementService() {73        ImmutableList<String> userGroupsList = ImmutableList.of("group1");74        context.checking(new Expectations() {{75            allowing(globalGroupsManagementService).allGlobalGroups();76            will(AbstractExpectations.returnValue(JavaConversions.asScalaBuffer(userGroupsList)));77        }});78        assertEquals(metaDataService.getUserGroupsList().get(0).id(), userGroupsList.get(0));79    }80    @Test81    public void userGroupsListReturnsEmptyListWhenReadFromGlobalGroupsManagementServiceFails() {82        context.checking(new Expectations() {{83            allowing(globalGroupsManagementService).allGlobalGroups();84            will(AbstractExpectations.throwException(new NullPointerException()));85        }});86        assertThat(metaDataService.getUserGroupsList().size(), is(0));87    }88    @Test89    public void languagesListContainsAllWixLanguages() {90        List<FilterOption> fullList = metaDataService.getLangList();91        assertEquals(fullList.size(), languagesSet.size());92    }93    @Test94    public void specWithMultipleScopesIsAddedCorrectly() {95        context.checking(new Expectations() {{96            allowing(specExposureIdDao).getAll();97            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));98        }});99        ScopeDefinition newRegisteredScope = new ScopeDefinition("new-scope", true);100        ExperimentSpec multipleScopeSpec = aNewlyGeneratedExperimentSpec("f.q.n.Class1").101                withTestGroups(asList("1", "2")).102                withScopes(editorScope, newRegisteredScope).build();103        scala.collection.immutable.Map<String, scala.collection.immutable.List<UiSpecForScope>> map =104                metaDataService.createScopeToSpecMap(asList(multipleScopeSpec));105        assertThat(map.contains(EDITOR_SCOPE + ",new-scope"), is(true));106    }107    @Test108    public void editorScopeIsAddedWhenKeyCaseChanged() {109        context.checking(new Expectations() {{110            allowing(specExposureIdDao).getAll();111            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));112        }});113        ExperimentSpec viewerEditorSpec = aNewlyGeneratedExperimentSpec("SPEC_KEY").114                withTestGroups(asList("1", "2")).115                withScopes(editorScope, viewerScope).build();116        scala.collection.immutable.Map<String, scala.collection.immutable.List<UiSpecForScope>> map =117                metaDataService.createScopeToSpecMap(asList(viewerEditorSpec));118        assertThat(map.get(EDITOR_SCOPE).get().head().getKey(), is(viewerEditorSpec.getKey()));119    }120    @Test121    public void scopesMapAddsHardcodedScope() {122        context.checking(new Expectations() {{123            allowing(fullPetriClient).fetchSpecs();124            will(AbstractExpectations.returnValue(ImmutableList.of(viewerEditorSpec)));125            allowing(specExposureIdDao).getAll();126            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));127            allowing(fullPetriClient).fetchAllExperimentsGroupedByOriginalId();128            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));129        }});130        java.util.Map<String, List<UiSpecForScope>> map = metaDataService.createScopeToSpecMap();131        assertThat(map.size(), is(4));132        assertTrue(map.containsKey(MockHardCodedScopesProvider$.MODULE$.HARD_CODED_SPEC_FOR_NON_REG()));133    }134    @Test135    public void scopeMapExposureId() {136        final ExperimentSpec specWithExposure = aNewlyGeneratedExperimentSpec("f.q.n.Class1")137                .withScopes(editorScope)138                .build();139        final ExperimentSpec specWithoutExposure = aNewlyGeneratedExperimentSpec("f.q.n.Class2")140                .withScopes(viewerScope)141                .build();142        final SpecExposureIdViewDto specExposure = new SpecExposureIdViewDto(143                "f.q.n.Class1", new Some<>("GUID"), new DateTime(), new DateTime()144        );145        context.checking(new Expectations() {{146            allowing(fullPetriClient).fetchSpecs();147            will(AbstractExpectations.returnValue(asList(specWithExposure, specWithoutExposure)));148            allowing(specExposureIdDao).getAll();149            will(AbstractExpectations.returnValue(asList(specExposure)));150            allowing(fullPetriClient).fetchAllExperimentsGroupedByOriginalId();151            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));152        }});153        java.util.Map<String, List<UiSpecForScope>> map = metaDataService.createScopeToSpecMap();154        assertThat(map.get(editorScope.getName()).get(0).getExposureId(), is("GUID"));155        assertNull(map.get(viewerScope.getName()).get(0).getExposureId());156    }157    @Test158    public void checkGetSpecExposureMapFromDB() {159        context.checking(new Expectations() {{160            allowing(specExposureIdDao).getAll();161            will(AbstractExpectations.returnValue(asList()));162        }});163        metaDataService.getSpecExposureMapFromDB();164    }165}...Source:RasaResourceAvailabilityStatusCheckerTest.java  
...29import java.util.AbstractMap;30import java.util.Map;31import java.util.stream.Stream;32import static org.jmock.AbstractExpectations.any;33import static org.jmock.AbstractExpectations.returnValue;34import org.jmock.Expectations;35import org.jmock.Mockery;36import org.jmock.integration.junit4.JUnit4Mockery;37import static org.junit.Assert.assertEquals;38import static org.junit.Assert.assertNotNull;39import static org.junit.Assert.assertTrue;40import org.junit.Before;41import org.junit.Test;42/**43 * @author Twan Goosen <twan@clarin.eu>44 */45public class RasaResourceAvailabilityStatusCheckerTest {46    private final Mockery context = new JUnit4Mockery();47    private CheckedLinkResource checkedLinkResource;48    private RasaResourceAvailabilityStatusChecker instance;49    private CheckedLinkFilter checkedLinkFilter;50    private final ImmutableMap<String, CheckedLink> checkedLinksMap = ImmutableMap.<String, CheckedLink>builder()51            .put(createResponseMapEntry("http://uri1", 200))52            .put(createResponseMapEntry("http://uri2", 404))53            .build();54    @Before55    public void setUp() throws SQLException {56        checkedLinkResource = context.mock(CheckedLinkResource.class);57        checkedLinkFilter = context.mock(CheckedLinkFilter.class);58        instance = new RasaResourceAvailabilityStatusChecker(checkedLinkResource,59                new RasaResourceAvailabilityStatusCheckerConfiguration(Duration.ofDays(10))) {60            @Override61            public void writeStatusSummary(Writer writer) throws IOException {62                writer.write("Status - test " + getClass());63            }64        };65    }66    /**67     * Test of getLinkStatusForRefs method, of class68     * RasaResourceAvailabilityStatusChecker.69     */70    @Test71    public void testGetLinkStatusForRefs() throws Exception {72        context.checking(new Expectations() {73            {74                atLeast(1).of(checkedLinkResource).getCheckedLinkFilter();75                will(returnValue(checkedLinkFilter));76                atLeast(1).of(checkedLinkResource).getMap(with(any(CheckedLinkFilter.class)));77                will(returnValue(checkedLinksMap));78                oneOf(checkedLinkFilter).setUrlIn(with(any(String[].class)));79                oneOf(checkedLinkFilter).setCheckedBetween(with(any(Timestamp.class)), with(any(Timestamp.class)));80            }81        });82        System.out.println("getLinkStatusForRefs");83        Stream<String> hrefs = Stream.of("http://uri3", "http://uri2", "http://uri1");84        Map<String, CheckedLink> result = instance.getLinkStatusForRefs(hrefs);85        assertEquals(2, result.size());86        assertNotNull(result.get("http://uri1"));87        assertEquals("http://uri1", result.get("http://uri1").getUrl());88        assertNotNull(result.get("http://uri1").getStatus());89        assertEquals(200, result.get("http://uri1").getStatus().intValue());90        assertNotNull(result.get("http://uri2"));91        assertEquals("http://uri2", result.get("http://uri2").getUrl());...returnValue
Using AI Code Generation
1package org.jmock.test.acceptance;2import junit.framework.TestCase;3import org.jmock.Mock;4import org.jmock.MockObjectTestCase;5import org.jmock.core.Constraint;6import org.jmock.core.constraint.IsEqual;7import org.jmock.core.constraint.IsSame;8import org.jmock.core.constraint.IsInstanceOf;9import org.jmock.core.constraint.IsIn;10import org.jmock.core.constraint.IsAnything;11import org.jmock.core.constraint.IsNot;12import org.jmock.core.constraint.IsNull;13import org.jmock.core.constraint.IsNotNull;14import org.jmock.core.constraint.IsNotSame;15import org.jmock.core.constraint.IsSame;16import org.jmock.core.constraint.IsTypeCompatible;17import org.jmock.core.constraint.IsNotEqual;18import org.jmock.core.constraint.IsNotIn;19import org.jmock.core.constraint.IsNotSame;20import org.jmock.core.constraint.IsSame;21import org.jmock.core.constraint.IsTypeCompatible;22import org.jmock.core.constraint.IsNotEqual;23import org.jmock.core.constraint.IsNotIn;24import org.jmock.core.constraint.IsNotSame;25import org.jmock.core.constraint.IsSame;26import org.jmock.core.constraint.IsTypeCompatible;27import org.jmock.core.constraint.IsNotEqual;28import org.jmock.core.constraint.IsNotIn;29import org.jmock.core.constraint.IsNotSame;30import org.jmock.core.constraint.IsSame;31import org.jmock.core.constraint.IsTypeCompatible;32import org.jmock.core.constraint.IsNotEqual;33import org.jmock.core.constraint.IsNotIn;34import org.jmock.core.constraint.IsNotSame;35import org.jmock.core.constraint.IsSame;36import org.jmock.core.constraint.IsTypeCompatible;37import org.jmock.core.constraint.IsNotEqual;38import org.jmock.core.constraint.IsNotIn;39import org.jmock.core.constraint.IsNotSame;40import org.jmock.core.constraint.IsSame;41import org.jmock.core.constraint.IsTypeCompatible;42import org.jmock.core.constraint.IsNotEqual;43import org.jmock.core.constraint.IsNotIn;44import org.jmock.core.constraint.IsNotSame;45import org.jmock.core.constraint.IsSame;46import org.jmock.core.constraint.IsTypeCompatible;47import org.jmock.core.constraint.IsNotEqual;48import org.jmock.core.constraint.IsNotIn;49import org.jmock.core.constraint.IsNotSame;50import org.jmock.core.constraint.IsSame;51import org.jmock.core.constraint.IsTypeCompatible;52import org.jmock.core.constraint.IsNotEqual;53import org.jmock.core.constraint.IsNotIn;54importreturnValue
Using AI Code Generation
1import org.jmock.*;2import org.jmock.core.*;3import org.jmock.util.*;4import org.jmock.expectation.*;5import org.jmock.core.matcher.*;6import org.jmock.core.constraint.*;7import org.jmock.core.stub.*;8import org.jmock.core.dynamic.*;9import org.jmock.core.invocation.*;10import org.jmock.core.stub.ReturnValue;11import org.jmock.core.stub.ReturnValues;12import org.jmock.core.stub.ThrowStub;13import org.jmock.core.stub.ThrowThrowable;14import org.jmock.core.constraint.IsAnything;15import org.jmock.core.constraint.IsEqual;16import org.jmock.core.constraint.IsSame;17import org.jmock.core.constraint.IsInstanceOf;18import org.jmock.core.constraint.IsIn;19import org.jmock.core.constraint.IsCollectionContaining;20import org.jmock.core.constraint.IsArrayContaining;21import org.jmock.core.constraint.IsSubstring;22import org.jmock.core.constraint.IsGreaterThan;23import org.jmock.core.constraint.IsLessThan;24import org.jmock.core.constraint.IsBetween;25import org.jmock.core.constraint.IsNot;26import org.jmock.core.constraint.IsNull;27import org.jmock.core.constraint.IsNotNull;28import org.jmock.core.constraint.IsSame;29import org.jmock.core.constraint.IsNotSame;30import org.jmock.core.constraint.IsRegexp;31import org.jmock.core.constraint.IsTypeCompatible;returnValue
Using AI Code Generation
1package org.jmock.examples;2import org.jmock.MockObjectTestCase;3import org.jmock.Mock;4import org.jmock.core.Constraint;5import org.jmock.core.DynamicMockError;6public class ReturnValueTest extends MockObjectTestCase {7    public void testReturnValue() {8        Mock mock = mock(Runnable.class, "mock");9        mock.expects(once()).method("run").withNoArguments().will(returnValue("hello"));10        String result = ((Runnable) mock.proxy()).run();11        assertEquals("hello", result);12    }13    public void testReturnValueWithConstraint() {14        Mock mock = mock(Runnable.class, "mock");15        mock.expects(once()).method("run").with(same("hello")).will(returnValue("hello"));16        String result = ((Runnable) mock.proxy()).run();17        assertEquals("hello", result);18    }19    public void testReturnValueWithConstraintThatDoesntMatch() {20        Mock mock = mock(Runnable.class, "mock");21        mock.expects(once()).method("run").with(same("hello")).will(returnValue("hello"));22        try {23            String result = ((Runnable) mock.proxy()).run();24            fail("should throw DynamicMockError");25        } catch (DynamicMockError e) {26            assertEquals("Unexpected invocation run(null)", e.getMessage());27        }28    }29    public void testReturnValueWithConstraintThatDoesntMatchAndNoReturnValue() {30        Mock mock = mock(Runnable.class, "mock");31        mock.expects(once()).method("run").with(same("hello"));32        try {33            String result = ((Runnable) mock.proxy()).run();34            fail("should throw DynamicMockError");35        } catch (DynamicMockError e) {36            assertEquals("Unexpected invocation run(null)", e.getMessage());37        }38    }39    public void testReturnValueWithConstraintThatDoesntMatchAndNoReturnValueAndNoArguments() {40        Mock mock = mock(Runnable.class, "mock");41        mock.expects(once()).method("run");42        try {43            String result = ((Runnable) mock.proxy()).run();44            fail("should throw DynamicMockError");45        } catch (DynamicMockError e) {46            assertEquals("Unexpected invocation run()", e.getMessage());47        }48    }49    public void testReturnValueWithConstraintThatDoesntMatchAndNoReturnValueAndNoArgumentsAndWithAnyArguments() {50        Mock mock = mock(Runnable.class, "mock");returnValue
Using AI Code Generation
1public class returnValueTest {2    public static void main(String[] args) {3        Mock mock = new Mock(MockedInterface.class);4        MockedInterface mocked = (MockedInterface) mock.proxy();5        mock.expects(once()).method("doSomething").will(returnValue("Hello"));6        System.out.println(mocked.doSomething());7    }8}9interface MockedInterface {10    public String doSomething();11}returnValue
Using AI Code Generation
1import org.jmock.Expectations;2import org.jmock.Mockery;3import org.jmock.integration.junit4.JUnit4Mockery;4import org.junit.Test;5import static org.jmock.Expectations.returnValue;6import static org.junit.Assert.assertEquals;7public class TestClass{8    public void testMethod(){9        Mockery context = new JUnit4Mockery();10        final Interface1 interface1 = context.mock(Interface1.class);11        context.checking(new Expectations(){{12            oneOf(interface1).method1();13            will(returnValue("Hello"));14        }});15        assertEquals("Hello",interface1.method1());16    }17}18interface Interface1{19    public String method1();20}21import org.jmock.Expectations;22import org.jmock.Mockery;23import org.jmock.integration.junit4.JUnit4Mockery;24import org.junit.Test;25import static org.jmock.Expectations.returnValue;26import static org.junit.Assert.assertEquals;27public class TestClass{28    public void testMethod(){29        Mockery context = new JUnit4Mockery();30        final Interface1 interface1 = context.mock(Interface1.class);31        context.checking(new Expectations(){{32            oneOf(interface1).method1();33            will(returnValue("Hello"));34        }});35        assertEquals("Hello",interface1.method1());36    }37}38interface Interface1{39    public String method1();40}41import org.jmock.Expectations;42import org.jmock.Mockery;43import org.jmock.integration.junit4.JUnit4Mockery;44import org.junit.Test;45import static org.jmock.Expectations.returnValue;46import static org.junit.Assert.assertEquals;47public class TestClass{48    public void testMethod(){49        Mockery context = new JUnit4Mockery();50        final Interface1 interface1 = context.mock(Interface1.class);51        context.checking(new Expectations(){{52            oneOf(interface1).method1();53            will(returnValue("Hello"));54        }});55        assertEquals("Hello",interface1.method1());56    }57}58interface Interface1{returnValue
Using AI Code Generation
1import org.jmock.*;2import org.jmock.core.*;3import org.jmock.core.constraint.*;4import org.jmock.core.matcher.*;5import org.jmock.core.stub.*;6import org.jmock.util.*;7{8   public void test1()9   {10      final Interface1 mockInterface1 = (Interface1) mock(Interface1.class);11      checking(new Expectations()12      {{13         one (mockInterface1).method1(); will(returnValue(10));14      }});15      mockInterface1.method1();16   }17}18{19   public int method1();20}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!!
