How to use MockNonPublicClassFailsTest class of org.mockito.osgitest.testbundle package

Best Mockito code snippet using org.mockito.osgitest.testbundle.MockNonPublicClassFailsTest

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (c) 2019 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.osgitest;
6
7import org.junit.runner.RunWith;
8import org.junit.runners.Suite;
9import org.junit.runners.model.RunnerBuilder;
10import org.osgi.framework.Bundle;
11import org.osgi.framework.BundleContext;
12import org.osgi.framework.BundleException;
13import org.osgi.framework.Constants;
14import org.osgi.framework.launch.Framework;
15import org.osgi.framework.launch.FrameworkFactory;
16
17import java.io.File;
18import java.io.IOException;
19import java.nio.file.*;
20import java.nio.file.attribute.BasicFileAttributes;
21import java.util.*;
22import java.util.regex.Pattern;
23import java.util.stream.Collectors;
24import java.util.stream.Stream;
25import org.junit.AfterClass;
26
27import static org.junit.Assert.fail;
28
29@RunWith(OsgiTest.class)
30public class OsgiTest extends Suite {
31
32    private static final FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
33    private static final String STORAGE_TEMPDIR_NAME = "osgi-test-storage";
34    private static final List<String> EXTRA_SYSTEMPACKAGES = Arrays.asList("org.junit", "sun.misc", "sun.reflect");
35
36    private static final List<Path> TEST_RUNTIME_BUNDLES = splitPaths(System.getProperty("testRuntimeBundles"));
37    private static final String TEST_BUNDLE_SYMBOLIC_NAME = "testBundle";
38
39    private static final long STOP_TIMEOUT_MS = 10000;
40
41    private static Path frameworkStorage;
42    private static Framework framework;
43    private static Bundle testBundle;
44
45    public OsgiTest(Class<?> osgiTestClass, RunnerBuilder builder) throws Exception {
46        super(builder, osgiTestClass, setUpClasses());
47    }
48
49    private static Class<?>[] setUpClasses() throws Exception {
50        frameworkStorage = Files.createTempDirectory(STORAGE_TEMPDIR_NAME);
51        Map<String, String> configuration = new HashMap<>();
52        configuration.put(Constants.FRAMEWORK_STORAGE, frameworkStorage.toString());
53        configuration.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, String.join(",", EXTRA_SYSTEMPACKAGES));
54        framework = frameworkFactory.newFramework(configuration);
55        framework.init();
56        BundleContext bundleContext = framework.getBundleContext();
57        for (Path dependencyPath : TEST_RUNTIME_BUNDLES) {
58            Bundle installedBundle;
59            try {
60                installedBundle = bundleContext.installBundle(dependencyPath.toUri().toString());
61            } catch (BundleException e) {
62                throw new IllegalStateException("Failed to install bundle: " + dependencyPath.getFileName(), e);
63            }
64            if (TEST_BUNDLE_SYMBOLIC_NAME.equals(installedBundle.getSymbolicName())) {
65                testBundle = installedBundle;
66            }
67        }
68        if (testBundle == null) {
69            fail("Test bundle not found.");
70        }
71        framework.start();
72        try {
73            // Manual start to get a better exception if the bundle cannot be resolved
74            testBundle.start();
75        } catch (BundleException e) {
76            throw new IllegalStateException("Failed to start test bundle.", e);
77        }
78        return getTestClasses();
79    }
80
81    private static Class<?>[] getTestClasses() throws Exception {
82        return new Class<?>[] {
83            loadTestClass("SimpleMockTest"),
84            loadTestClass("MockNonPublicClassFailsTest"),
85            loadTestClass("MockClassInOtherBundleTest")
86        };
87    }
88
89    @AfterClass
90    public static void tearDown() throws Exception {
91        try {
92            if (framework != null) {
93                framework.stop();
94                framework.waitForStop(STOP_TIMEOUT_MS);
95            }
96        } finally {
97            if (frameworkStorage != null) {
98                deleteRecursively(frameworkStorage);
99            }
100        }
101    }
102
103    private static Class<?> loadTestClass(String className) throws Exception {
104        return testBundle.loadClass("org.mockito.osgitest.testbundle." + className);
105    }
106
107    private static List<Path> splitPaths(String paths) {
108        return Stream.of(paths.split(Pattern.quote(File.pathSeparator)))
109            .map(p -> Paths.get(p))
110            .collect(Collectors.toList());
111    }
112
113    private static void deleteRecursively(Path pathToDelete) throws IOException {
114        Files.walkFileTree(pathToDelete,
115            new SimpleFileVisitor<Path>() {
116                @Override
117                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
118                    Files.delete(dir);
119                    return FileVisitResult.CONTINUE;
120                }
121
122                @Override
123                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
124                    Files.delete(file);
125                    return FileVisitResult.CONTINUE;
126                }
127            });
128    }
129}
130
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

...Most popular Stackoverflow questions on MockNonPublicClassFailsTest

    No relevant questions found for this class 😞

Most used methods in MockNonPublicClassFailsTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)