How to use HashCodeAndEqualsSafeSet class of org.mockito.internal.util.collections package

Best Mockito code snippet using org.mockito.internal.util.collections.HashCodeAndEqualsSafeSet

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.collections;
6
7
8import java.util.HashSet;
9import java.util.Iterator;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Observer;
13import org.junit.Rule;
14import org.junit.Test;
15import org.mockito.Mock;
16import org.mockito.Mockito;
17import org.mockito.junit.MockitoJUnit;
18import org.mockito.junit.MockitoRule;
19
20
21public class HashCodeAndEqualsSafeSetTest {
22    @Rule
23    public MockitoRule r = MockitoJUnit.rule();
24
25    @Mock
26    private HashCodeAndEqualsSafeSetTest.UnmockableHashCodeAndEquals mock1;
27
28    @Test
29    public void can_add_mock_that_have_failing_hashCode_method() throws Exception {
30        new HashCodeAndEqualsSafeSet().add(mock1);
31    }
32
33    @Test
34    public void mock_with_failing_hashCode_method_can_be_added() throws Exception {
35        new HashCodeAndEqualsSafeSet().add(mock1);
36    }
37
38    @Test
39    public void mock_with_failing_equals_method_can_be_used() throws Exception {
40        HashCodeAndEqualsSafeSet mocks = new HashCodeAndEqualsSafeSet();
41        mocks.add(mock1);
42        assertThat(mocks.contains(mock1)).isTrue();
43        HashCodeAndEqualsSafeSetTest.UnmockableHashCodeAndEquals mock2 = Mockito.mock(HashCodeAndEqualsSafeSetTest.UnmockableHashCodeAndEquals.class);
44        assertThat(mocks.contains(mock2)).isFalse();
45    }
46
47    @Test
48    public void can_remove() throws Exception {
49        HashCodeAndEqualsSafeSet mocks = new HashCodeAndEqualsSafeSet();
50        HashCodeAndEqualsSafeSetTest.UnmockableHashCodeAndEquals mock = mock1;
51        mocks.add(mock);
52        mocks.remove(mock);
53        assertThat(mocks.isEmpty()).isTrue();
54    }
55
56    @Test
57    public void can_add_a_collection() throws Exception {
58        HashCodeAndEqualsSafeSet mocks = HashCodeAndEqualsSafeSet.of(mock1, Mockito.mock(Observer.class));
59        HashCodeAndEqualsSafeSet workingSet = new HashCodeAndEqualsSafeSet();
60        workingSet.addAll(mocks);
61        assertThat(workingSet.containsAll(mocks)).isTrue();
62    }
63
64    @Test
65    public void can_retain_a_collection() throws Exception {
66        HashCodeAndEqualsSafeSet mocks = HashCodeAndEqualsSafeSet.of(mock1, Mockito.mock(Observer.class));
67        HashCodeAndEqualsSafeSet workingSet = new HashCodeAndEqualsSafeSet();
68        workingSet.addAll(mocks);
69        workingSet.add(Mockito.mock(List.class));
70        assertThat(workingSet.retainAll(mocks)).isTrue();
71        assertThat(workingSet.containsAll(mocks)).isTrue();
72    }
73
74    @Test
75    public void can_remove_a_collection() throws Exception {
76        HashCodeAndEqualsSafeSet mocks = HashCodeAndEqualsSafeSet.of(mock1, Mockito.mock(Observer.class));
77        HashCodeAndEqualsSafeSet workingSet = new HashCodeAndEqualsSafeSet();
78        workingSet.addAll(mocks);
79        workingSet.add(Mockito.mock(List.class));
80        assertThat(workingSet.removeAll(mocks)).isTrue();
81        assertThat(workingSet.containsAll(mocks)).isFalse();
82    }
83
84    @Test
85    public void can_iterate() throws Exception {
86        HashCodeAndEqualsSafeSet mocks = HashCodeAndEqualsSafeSet.of(mock1, Mockito.mock(Observer.class));
87        LinkedList<Object> accumulator = new LinkedList<Object>();
88        for (Object mock : mocks) {
89            accumulator.add(mock);
90        }
91        assertThat(accumulator).isNotEmpty();
92    }
93
94    @Test
95    public void toArray_just_work() throws Exception {
96        HashCodeAndEqualsSafeSet mocks = HashCodeAndEqualsSafeSet.of(mock1);
97        assertThat(mocks.toArray()[0]).isSameAs(mock1);
98        assertThat(mocks.toArray(new HashCodeAndEqualsSafeSetTest.UnmockableHashCodeAndEquals[0])[0]).isSameAs(mock1);
99    }
100
101    @Test(expected = CloneNotSupportedException.class)
102    public void cloneIsNotSupported() throws CloneNotSupportedException {
103        HashCodeAndEqualsSafeSet.of().clone();
104    }
105
106    @Test
107    public void isEmptyAfterClear() throws Exception {
108        HashCodeAndEqualsSafeSet set = HashCodeAndEqualsSafeSet.of(mock1);
109        set.clear();
110        assertThat(set).isEmpty();
111    }
112
113    @Test
114    public void isEqualToItself() {
115        HashCodeAndEqualsSafeSet set = HashCodeAndEqualsSafeSet.of(mock1);
116        assertThat(set).isEqualTo(set);
117    }
118
119    @Test
120    public void isNotEqualToAnOtherTypeOfSetWithSameContent() {
121        HashCodeAndEqualsSafeSet set = HashCodeAndEqualsSafeSet.of();
122        assertThat(set).isNotEqualTo(new HashSet<Object>());
123    }
124
125    @Test
126    public void isNotEqualWhenContentIsDifferent() {
127        HashCodeAndEqualsSafeSet set = HashCodeAndEqualsSafeSet.of(mock1);
128        assertThat(set).isNotEqualTo(HashCodeAndEqualsSafeSet.of());
129    }
130
131    @Test
132    public void hashCodeIsEqualIfContentIsEqual() {
133        HashCodeAndEqualsSafeSet set = HashCodeAndEqualsSafeSet.of(mock1);
134        assertThat(set.hashCode()).isEqualTo(HashCodeAndEqualsSafeSet.of(mock1).hashCode());
135    }
136
137    @Test
138    public void toStringIsNotNullOrEmpty() throws Exception {
139        HashCodeAndEqualsSafeSet set = HashCodeAndEqualsSafeSet.of(mock1);
140        assertThat(set.toString()).isNotEmpty();
141    }
142
143    @Test
144    public void removeByIterator() throws Exception {
145        HashCodeAndEqualsSafeSet set = HashCodeAndEqualsSafeSet.of(mock1);
146        Iterator<Object> iterator = set.iterator();
147        iterator.next();
148        iterator.remove();
149        assertThat(set).isEmpty();
150    }
151
152    private static class UnmockableHashCodeAndEquals {
153        @Override
154        public final int hashCode() {
155            throw new NullPointerException("I'm failing on hashCode and I don't care");
156        }
157
158        @Override
159        public final boolean equals(Object obj) {
160            throw new NullPointerException("I'm failing on equals and I don't care");
161        }
162    }
163}
164
165
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 HashCodeAndEqualsSafeSet

    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)