How to use ClassFromOtherClassLoader class of org.jmock.testjar package

Best Jmock-library code snippet using org.jmock.testjar.ClassFromOtherClassLoader

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1/*  Copyright (c) 2000-2004 jMock.org
2 */
3package org.jmock.test.acceptance;
4
5import static org.junit.Assert.assertTrue;
6
7import java.io.File;
8import java.lang.Thread.UncaughtExceptionHandler;
9import java.net.MalformedURLException;
10import java.net.URISyntaxException;
11import java.net.URL;
12import java.net.URLClassLoader;
13
14import org.jmock.Mockery;
15import org.jmock.api.Imposteriser;
16import org.jmock.test.unit.lib.legacy.CodeGeneratingImposteriserParameterResolver;
17import org.jmock.test.unit.lib.legacy.ImposteriserParameterResolver;
18import org.junit.jupiter.api.BeforeEach;
19import org.junit.jupiter.api.Disabled;
20import org.junit.jupiter.params.ParameterizedTest;
21import org.junit.jupiter.params.provider.ArgumentsSource;
22
23
24public class ClassLoaderAcceptanceTests {
25
26    private static final String UNSIGNED_JAR_NAME = "../testjar/target/unsigned.jar";
27    private static final String CLASS_FROM_OTHER_CLASS_LOADER = "org.jmock.testjar.ClassFromOtherClassLoader";
28    private static final String INTERFACE_FROM_OTHER_CLASS_LOADER = "org.jmock.testjar.InterfaceFromOtherClassLoader";
29    
30    Mockery mockery = new Mockery();
31    ClassLoader classLoader;
32    
33    @BeforeEach
34    public void setUp() throws MalformedURLException, URISyntaxException {
35        File unsignedFile = new File(UNSIGNED_JAR_NAME);
36        assertTrue("The unsigned  file is missing, mvn package will build it",unsignedFile.exists());
37        classLoader = new URLClassLoader(new URL[]{unsignedFile.toURI().toURL()}, null);
38    }
39        
40    @ParameterizedTest
41    @ArgumentsSource(ImposteriserParameterResolver.class)
42    public void testMockingInterfaceFromOtherClassLoaderWithClassImposteriser(Imposteriser imposteriserImpl) throws ClassNotFoundException {
43        mockery.setImposteriser(imposteriserImpl);
44        mockery.mock(classLoader.loadClass(INTERFACE_FROM_OTHER_CLASS_LOADER));
45    }
46    
47    @ParameterizedTest
48    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
49    public void testMockingClassFromOtherClassLoaderWithClassImposteriser(Imposteriser imposteriserImpl) throws ClassNotFoundException {
50        mockery.setImposteriser(imposteriserImpl);
51        mockery.mock(classLoader.loadClass(CLASS_FROM_OTHER_CLASS_LOADER));
52    }
53    
54    // I've been unable to reproduce the behaviour of the Maven Surefire plugin in plain JUnit tests
55    @Disabled
56    @ParameterizedTest
57    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
58    public void testMockingClassFromThreadContextClassLoader(final Imposteriser imposteriserImpl) throws Throwable {
59        Runnable task = new Runnable() {
60            public void run() {
61                try {
62                    Class<?> classToMock = Thread.currentThread().getContextClassLoader().loadClass(CLASS_FROM_OTHER_CLASS_LOADER);
63                    
64                    Mockery threadMockery = new Mockery();
65                    threadMockery.setImposteriser(imposteriserImpl);
66                    
67                    threadMockery.mock(classToMock);
68                }
69                catch (ClassNotFoundException e) {
70                    throw new IllegalStateException("could not load class", e);
71                }
72            }
73        };
74        
75        ExceptionTrap exceptionTrap = new ExceptionTrap();
76        
77        Thread thread = new Thread(task, getClass().getSimpleName() + " Thread");
78        thread.setContextClassLoader(new URLClassLoader(new URL[]{new URL("file:build/testdata/unsigned.jar")}, null));
79        thread.setUncaughtExceptionHandler(exceptionTrap);
80        
81        thread.start();
82        thread.join();
83        
84        if (exceptionTrap.exception != null) {
85            throw exceptionTrap.exception;
86        }
87    }
88    
89    private static class ExceptionTrap implements UncaughtExceptionHandler {
90        public Throwable exception = null;
91        
92        public void uncaughtException(Thread t, Throwable e) {
93            exception = e;
94        }
95    }
96}
97
Full Screen
copy
1package org.jmock.testjar;
2
3
4public class ClassFromOtherClassLoader {
5    public void doSomething() {}
6}
7
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 ClassFromOtherClassLoader

    No relevant questions found for this class 😞

Most used methods in ClassFromOtherClassLoader

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)