How to use VmArgAssumptions class of org.mockitoutil package

Best Mockito code snippet using org.mockitoutil.VmArgAssumptions

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.mockito.internal.util.reflection;
6
7
8import java.util.Observable;
9import org.junit.Test;
10import org.mockitoutil.VmArgAssumptions;
11
12
13public class AccessibilityChangerTest {
14    @SuppressWarnings("unused")
15    private Observable whatever;
16
17    @Test
18    public void should_enable_and_safely_disable() throws Exception {
19        AccessibilityChanger changer = new AccessibilityChanger();
20        changer.enableAccess(field("whatever"));
21        changer.safelyDisableAccess(field("whatever"));
22    }
23
24    @Test(expected = AssertionError.class)
25    public void safelyDisableAccess_should_fail_when_enableAccess_not_called() throws Exception {
26        VmArgAssumptions.assumeVmArgPresent("-ea");
27        new AccessibilityChanger().safelyDisableAccess(field("whatever"));
28    }
29}
30
31
Full Screen
copy
1package org.mockito.internal.creation.bytebuddy;
2
3import org.junit.Before;
4import org.junit.Test;
5import org.mockito.mock.SerializableMode;
6import org.mockitoutil.VmArgAssumptions;
7
8import java.lang.ref.WeakReference;
9import java.util.Collections;
10import java.util.WeakHashMap;
11
12import static org.assertj.core.api.Assertions.assertThat;
13import static org.mockito.internal.creation.bytebuddy.MockFeatures.withMockFeatures;
14import static org.mockitoutil.ClassLoaders.inMemoryClassLoader;
15import static org.mockitoutil.SimpleClassGenerator.makeMarkerInterface;
16
17public class TypeCachingMockBytecodeGeneratorTest {
18
19    @Before
20    public void ensure_disable_gc_is_activated() throws Exception {
21        VmArgAssumptions.assumeVmArgNotPresent("-XX:+DisableExplicitGC");
22    }
23
24    @Test
25    public void ensure_cache_is_cleared_if_no_reference_to_classloader_and_classes() throws Exception {
26        // given
27        ClassLoader classloader_with_life_shorter_than_cache = inMemoryClassLoader()
28                .withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar"))
29                .build();
30
31        TypeCachingBytecodeGenerator cachingMockBytecodeGenerator = new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
32        Class<?> the_mock_type = cachingMockBytecodeGenerator.mockClass(withMockFeatures(
33                classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
34                Collections.<Class<?>>emptySet(),
35                SerializableMode.NONE
36        ));
37
38        assertThat(cachingMockBytecodeGenerator.avoidingClassLeakageCache).hasSize(1);
39
40        // when
41        classloader_with_life_shorter_than_cache = is_no_more_referenced();
42        the_mock_type = is_no_more_referenced();
43
44        System.gc();
45        ensure_gc_happened();
46
47        cachingMockBytecodeGenerator.cleanUpCachesForObsoleteClassLoaders();
48
49        // then
50        assertThat(cachingMockBytecodeGenerator.avoidingClassLeakageCache).isEmpty();
51    }
52
53    @Test
54    public void ensure_cache_returns_same_instance() throws Exception {
55        // given
56        ClassLoader classloader_with_life_shorter_than_cache = inMemoryClassLoader()
57                .withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar"))
58                .build();
59
60        TypeCachingBytecodeGenerator cachingMockBytecodeGenerator = new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
61        Class<?> the_mock_type = cachingMockBytecodeGenerator.mockClass(withMockFeatures(
62                        classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
63                        Collections.<Class<?>>emptySet(),
64                        SerializableMode.NONE
65                ));
66
67        Class<?> other_mock_type = cachingMockBytecodeGenerator.mockClass(withMockFeatures(
68                classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
69                Collections.<Class<?>>emptySet(),
70                SerializableMode.NONE
71        ));
72
73        assertThat(other_mock_type).isSameAs(the_mock_type);
74        assertThat(cachingMockBytecodeGenerator.avoidingClassLeakageCache).hasSize(1);
75
76        // when
77        classloader_with_life_shorter_than_cache = is_no_more_referenced();
78        the_mock_type = is_no_more_referenced();
79
80        System.gc();
81        ensure_gc_happened();
82
83        cachingMockBytecodeGenerator.cleanUpCachesForObsoleteClassLoaders();
84
85        // then
86        assertThat(cachingMockBytecodeGenerator.avoidingClassLeakageCache).hasSize(1);
87    }
88
89    @Test
90    public void validate_simple_code_idea_where_weakhashmap_with_classloader_as_key_get_GCed_when_no_more_references() throws Exception {
91        // given
92        WeakHashMap<ClassLoader, Object> cache = new WeakHashMap<ClassLoader, Object>();
93        ClassLoader short_lived_classloader = inMemoryClassLoader()
94                .withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar"))
95                .build();
96
97        cache.put(short_lived_classloader, new HoldingAReference(new WeakReference<Class<?>>(short_lived_classloader.loadClass("foo.Bar"))));
98
99        assertThat(cache).hasSize(1);
100
101        // when
102        short_lived_classloader = is_no_more_referenced();
103
104        System.gc();
105        ensure_gc_happened();
106
107        // then
108        assertThat(cache).isEmpty();
109    }
110
111    static class HoldingAReference {
112        final WeakReference<Class<?>> a;
113
114        HoldingAReference(WeakReference<Class<?>> a) {
115            this.a = a;
116        }
117    }
118
119
120    private static <T> T is_no_more_referenced() {
121        return null;
122    }
123
124    private static void ensure_gc_happened() throws InterruptedException {
125        // wait in order to make sure the GC happened
126        Thread.sleep(500);
127    }
128}
129
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 VmArgAssumptions

    No relevant questions found for this class 😞

Most used methods in VmArgAssumptions

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)