How to use method of org.easymock.itests.OsgiBaseTest class

Best Easymock code snippet using org.easymock.itests.OsgiBaseTest.

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2009-2021 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock.itests;
17
18import org.easymock.MockType;
19import org.easymock.internal.MocksControl;
20import org.easymock.internal.matchers.Equals;
21import org.junit.Ignore;
22import org.junit.Test;
23
24import java.io.IOException;
25import java.util.ArrayList;
26
27import static org.easymock.EasyMock.*;
28import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertSame;
30
31/**
32 * Note: This is a JUnit 3 test because of the Spring OSGi test framework
33 *
34 * @author Henri Tremblay
35 */
36public class OsgiTest extends OsgiBaseTest {
37
38    public static abstract class A {
39        public abstract void foo();
40    }
41
42    @Test
43    public void testCanMock() throws IOException {
44        Appendable mock = mock(Appendable.class);
45        expect(mock.append("test")).andReturn(mock);
46        replayAll();
47        assertSame(mock, mock.append("test"));
48        verifyAll();
49    }
50
51    @Ignore("Doesn't work with pax-exam yet")
52    @Test
53    public void testCanUseMatchers() {
54        new Equals(new Object());
55    }
56
57    @Ignore("Doesn't work with pax-exam yet")
58    @Test
59    public void testCanUseInternal() {
60        new MocksControl(MockType.DEFAULT);
61    }
62
63    /**
64     * Class loaded in the bootstrap class loader have a null class loader. In
65     * this case, cglib creates the proxy in its own class loader. So I need to
66     * test this case is working
67     */
68    @Test
69    public void testCanMock_BootstrapClassLoader() {
70        ArrayList<?> mock = mock(ArrayList.class);
71        expect(mock.size()).andReturn(5);
72        replayAll();
73        assertEquals(5, mock.size());
74        verifyAll();
75    }
76
77    /**
78     * Normal case of a class in this class loader
79     */
80    @Test
81    public void testCanMock_OtherClassLoader() {
82        A mock = mock(A.class);
83        mock.foo();
84        replayAll();
85        mock.foo();
86        verifyAll();
87    }
88
89    @Test
90    public void  testCanPartialMock() {
91        A mock = partialMockBuilder(A.class).withConstructor().addMockedMethod("foo").createMock();
92
93        mock.foo();
94        replay(mock);
95        mock.foo();
96        verify(mock);
97    }
98}
99
Full Screen
copy
1/*
2 * Copyright 2009-2021 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock.itests;
17
18import org.easymock.EasyMockSupport;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.ops4j.pax.exam.Configuration;
22import org.ops4j.pax.exam.MavenUtils;
23import org.ops4j.pax.exam.Option;
24import org.ops4j.pax.exam.junit.PaxExam;
25import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
26import org.ops4j.pax.exam.spi.reactors.PerMethod;
27import org.osgi.framework.Bundle;
28import org.osgi.framework.BundleContext;
29import org.osgi.framework.Constants;
30
31import javax.inject.Inject;
32
33import static org.ops4j.pax.exam.CoreOptions.*;
34
35
36/**
37 * @author Henri Tremblay
38 */
39@RunWith(PaxExam.class)
40@ExamReactorStrategy(PerMethod.class)
41public abstract class OsgiBaseTest {
42
43    private final EasyMockSupport support = new EasyMockSupport();
44
45    protected <T> T mock(Class<T> toMock) {
46        return support.mock(toMock);
47    }
48
49    protected void replayAll() {
50        support.replayAll();
51    }
52
53    protected void verifyAll() {
54        support.verifyAll();
55    }
56
57    protected String getImplementationVersion(Class<?> c) {
58        return c.getPackage().getImplementationVersion();
59    }
60
61    @Inject
62    private BundleContext bundleContext;
63
64    @Configuration
65    public Option[] config() {
66        String version = MavenUtils.getArtifactVersion("org.easymock", "easymock");
67        return options(
68            bundle("file:../core/target/easymock-" + version + ".jar"),
69            mavenBundle().groupId("org.objenesis").artifactId("objenesis").versionAsInProject(),
70            junitBundles()
71        );
72    }
73
74    @Test
75    public void testOsgiPlatformStarts() {
76        System.out.println("Framework vendor: " + this.bundleContext.getProperty(Constants.FRAMEWORK_VENDOR));
77        System.out.println("Framework version: " + bundleContext.getProperty(Constants.FRAMEWORK_VERSION));
78        System.out.println("Require capability: " + bundleContext.getProperty(Constants.REQUIRE_CAPABILITY));
79        System.out.println("-----------------------------");
80
81        Bundle[] bundles = bundleContext.getBundles();
82        for (Bundle bundle : bundles) {
83            System.out.println(bundle + ": " + bundle.getHeaders().get(Constants.BUNDLE_VERSION));
84        }
85    }
86}
87
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

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)