How to use SimplePerRealmReloadingClassLoader class of org.mockitoutil package

Best Mockito code snippet using org.mockitoutil.SimplePerRealmReloadingClassLoader

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockitousage.serialization;
6
7import static org.assertj.core.api.Assertions.assertThat;
8
9import java.io.ByteArrayInputStream;
10import java.util.Collections;
11import java.util.List;
12import java.util.concurrent.Callable;
13
14import org.junit.Before;
15import org.junit.Test;
16import org.mockito.Mockito;
17import org.mockito.mock.SerializableMode;
18import org.mockitousage.IMethods;
19import org.mockitoutil.SimplePerRealmReloadingClassLoader;
20import org.mockitoutil.SimpleSerializationUtil;
21
22public class AcrossClassLoaderSerializationTest {
23
24    public IMethods mock;
25
26    @Before
27    public void reproduce_CCE_by_creating_a_mock_with_IMethods_before() throws Exception {
28        mock = Mockito.mock(IMethods.class);
29    }
30
31    @Test
32    public void check_that_mock_can_be_serialized_in_a_classloader_and_deserialized_in_another() throws Exception {
33        byte[] bytes = create_mock_and_serialize_it_in_class_loader_A();
34
35        Object the_deserialized_mock = read_stream_and_deserialize_it_in_class_loader_B(bytes);
36        assertThat(the_deserialized_mock.getClass().getName()).startsWith("org.mockito.codegen.AClassToBeMockedInThisTestOnlyAndInCallablesOnly");
37    }
38
39    private Object read_stream_and_deserialize_it_in_class_loader_B(byte[] bytes) throws Exception {
40        return new SimplePerRealmReloadingClassLoader(this.getClass().getClassLoader(), isolating_test_classes())
41                .doInRealm(
42                        "org.mockitousage.serialization.AcrossClassLoaderSerializationTest$ReadStreamAndDeserializeIt",
43                        new Class<?>[]{ byte[].class },
44                        new Object[]{ bytes }
45                );
46    }
47
48    private byte[] create_mock_and_serialize_it_in_class_loader_A() throws Exception {
49        return (byte[]) new SimplePerRealmReloadingClassLoader(this.getClass().getClassLoader(), isolating_test_classes())
50                .doInRealm("org.mockitousage.serialization.AcrossClassLoaderSerializationTest$CreateMockAndSerializeIt");
51    }
52
53
54    private SimplePerRealmReloadingClassLoader.ReloadClassPredicate isolating_test_classes() {
55        return new SimplePerRealmReloadingClassLoader.ReloadClassPredicate() {
56            public boolean acceptReloadOf(String qualifiedName) {
57                return qualifiedName.contains("org.mockitousage")
58                        || qualifiedName.contains("org.mockitoutil")
59                        ;
60            }
61        };
62    }
63
64
65    // see create_mock_and_serialize_it_in_class_loader_A
66    public static class CreateMockAndSerializeIt implements Callable<byte[]> {
67        public byte[] call() throws Exception {
68            AClassToBeMockedInThisTestOnlyAndInCallablesOnly mock = Mockito.mock(
69                    AClassToBeMockedInThisTestOnlyAndInCallablesOnly.class,
70                    Mockito.withSettings().serializable(SerializableMode.ACROSS_CLASSLOADERS)
71            );
72            // use MethodProxy before
73            mock.returningSomething();
74
75            return SimpleSerializationUtil.serializeMock(mock).toByteArray();
76        }
77    }
78
79    // see read_stream_and_deserialize_it_in_class_loader_B
80    public static class ReadStreamAndDeserializeIt implements Callable<Object> {
81        private byte[] bytes;
82
83        public ReadStreamAndDeserializeIt(byte[] bytes) {
84            this.bytes = bytes;
85        }
86
87        public Object call() throws Exception {
88            ByteArrayInputStream to_unserialize = new ByteArrayInputStream(bytes);
89            return SimpleSerializationUtil.deserializeMock(
90                    to_unserialize,
91                    AClassToBeMockedInThisTestOnlyAndInCallablesOnly.class
92            );
93        }
94    }
95
96
97    public static class AClassToBeMockedInThisTestOnlyAndInCallablesOnly {
98        List returningSomething() { return Collections.emptyList(); }
99    }
100}
101
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 SimplePerRealmReloadingClassLoader

    No relevant questions found for this class 😞

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)