-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support authorizedCollections
option for listCollections
helpers
#1270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d91efbb
c646b12
537e9ab
9a7dbe7
b1d8b57
0ac0567
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb.internal.mockito; | ||
|
||
import com.mongodb.lang.Nullable; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static com.mongodb.assertions.Assertions.fail; | ||
import static java.lang.String.format; | ||
|
||
/** | ||
* @see MongoMockito#mock(Class, Consumer) | ||
*/ | ||
final class InsufficientStubbingDetector implements Answer<Void> { | ||
private boolean enabled; | ||
|
||
InsufficientStubbingDetector() { | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Void answer(final InvocationOnMock invocation) throws AssertionError { | ||
if (enabled) { | ||
throw fail(format("Insufficient stubbing. Unexpected invocation %s on the object %s.", invocation, invocation.getMock())); | ||
} | ||
return null; | ||
} | ||
|
||
void enable() { | ||
jyemin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
enabled = true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb.internal.mockito; | ||
|
||
import com.mongodb.internal.binding.ReadBinding; | ||
import com.mongodb.internal.connection.OperationContext; | ||
import com.mongodb.internal.diagnostics.logging.Logger; | ||
import com.mongodb.internal.diagnostics.logging.Loggers; | ||
import com.mongodb.internal.operation.ListCollectionsOperation; | ||
import org.bson.BsonDocument; | ||
import org.bson.codecs.BsonDocumentCodec; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.mockito.internal.stubbing.answers.ThrowsException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.when; | ||
|
||
final class InsufficientStubbingDetectorDemoTest { | ||
private static final Logger LOGGER = Loggers.getLogger(InsufficientStubbingDetectorDemoTest.class.getSimpleName()); | ||
|
||
private ListCollectionsOperation<BsonDocument> operation; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
operation = new ListCollectionsOperation<>("db", new BsonDocumentCodec()); | ||
} | ||
|
||
@Test | ||
void mockObjectWithDefaultAnswer() { | ||
ReadBinding binding = Mockito.mock(ReadBinding.class); | ||
LOGGER.info("", assertThrows(NullPointerException.class, () -> operation.execute(binding))); | ||
} | ||
|
||
@Test | ||
void mockObjectWithThrowsException() { | ||
ReadBinding binding = Mockito.mock(ReadBinding.class, | ||
new ThrowsException(new AssertionError("Insufficient stubbing for " + ReadBinding.class))); | ||
LOGGER.info("", assertThrows(AssertionError.class, () -> operation.execute(binding))); | ||
} | ||
|
||
@Test | ||
void mockObjectWithInsufficientStubbingDetector() { | ||
ReadBinding binding = MongoMockito.mock(ReadBinding.class); | ||
LOGGER.info("", assertThrows(AssertionError.class, () -> operation.execute(binding))); | ||
} | ||
|
||
@Test | ||
void stubbingWithThrowsException() { | ||
ReadBinding binding = Mockito.mock(ReadBinding.class, | ||
new ThrowsException(new AssertionError("Unfortunately, you cannot do stubbing"))); | ||
assertThrows(AssertionError.class, () -> when(binding.getOperationContext()).thenReturn(new OperationContext())); | ||
} | ||
|
||
@Test | ||
void stubbingWithInsufficientStubbingDetector() { | ||
MongoMockito.mock(ReadBinding.class, bindingMock -> | ||
when(bindingMock.getOperationContext()).thenReturn(new OperationContext()) | ||
); | ||
} | ||
Comment on lines
+69
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason, unused Mockito stubbing is not reported when we run tests (ideally, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the juniper extension and 💥
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, thank you! After looking at private MockitoSession session;
@BeforeEach
void beforeEach() {
session = Mockito.mockitoSession().startMocking();
}
@AfterEach
void afterEach() {
session.finishMocking();
}
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, actually, it's the |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb.internal.mockito; | ||
|
||
import com.mongodb.lang.Nullable; | ||
import org.mockito.Answers; | ||
import org.mockito.Mockito; | ||
import org.mockito.internal.stubbing.answers.ThrowsException; | ||
import org.mockito.stubbing.OngoingStubbing; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.withSettings; | ||
|
||
/** | ||
* Complements {@link Mockito}. | ||
*/ | ||
public final class MongoMockito { | ||
/** | ||
* Is equivalent to calling {@link #mock(Class, Consumer)} with a {@code null} {@code tuner}. | ||
*/ | ||
public static <T> T mock(final Class<T> classToMock) { | ||
return mock(classToMock, null); | ||
} | ||
|
||
/** | ||
* This method is similar to {@link Mockito#mock(Class)} but changes the default behavior of the methods of a mock object | ||
* such that insufficient stubbing is detected and reported. By default, Mockito uses {@link Answers#RETURNS_DEFAULTS}. | ||
* While this answer has potential to save users some stubbing work, the provided convenience may not be worth the cost: | ||
* if the default result (often {@code null} for reference types) is insufficient, | ||
* one likely gets an unhelpful {@link NullPointerException} | ||
* (see {@link InsufficientStubbingDetectorDemoTest#mockObjectWithDefaultAnswer()}), | ||
* or a silent incorrect behavior with no clear indication of the mock object method that caused the problem. | ||
* Furthermore, a working test that uses mock objects may be unwittingly broken when refactoring production code. | ||
* While this particular issue is inherent to tests that use mock objects, | ||
* broken tests not indicating clearly what is wrong make matters worse. | ||
* <p> | ||
* Mockito has {@link ThrowsException}, | ||
* and at first glance it may seem like using it may help detecting insufficient stubbing. | ||
* It can point us to a line where the insufficiently stubbed method was called at, but it cannot tell us the name of that method | ||
* (see {@link InsufficientStubbingDetectorDemoTest#mockObjectWithThrowsException()}). | ||
* Moreover, a mock object created with {@link ThrowsException} as its default answer cannot be stubbed: | ||
* stubbing requires calling methods of the mock object, but they all complete abruptly | ||
* (see {@link InsufficientStubbingDetectorDemoTest#stubbingWithThrowsException()}). | ||
* Therefore, {@link ThrowsException} is not suitable for detecting insufficient stubbing.</p> | ||
* <p> | ||
* This method overcomes both of the aforementioned limitations by using {@link InsufficientStubbingDetector} as the default answer | ||
* (see {@link InsufficientStubbingDetectorDemoTest#mockObjectWithInsufficientStubbingDetector()}, | ||
* {@link InsufficientStubbingDetectorDemoTest#stubbingWithInsufficientStubbingDetector()}). | ||
* Note also that for convenience, {@link InsufficientStubbingDetector} stubs the {@link Object#toString()} method by using | ||
* {@link OngoingStubbing#thenCallRealMethod()}, unless this stubbing is overwritten by the {@code tuner}.</p> | ||
*/ | ||
public static <T> T mock(final Class<T> classToMock, @Nullable final Consumer<T> tuner) { | ||
final InsufficientStubbingDetector insufficientStubbingDetector = new InsufficientStubbingDetector(); | ||
final T mock = Mockito.mock(classToMock, withSettings().defaultAnswer(insufficientStubbingDetector)); | ||
when(mock.toString()).thenCallRealMethod(); | ||
if (tuner != null) { | ||
tuner.accept(mock); | ||
} | ||
insufficientStubbingDetector.enable(); | ||
return mock; | ||
} | ||
|
||
private MongoMockito() { | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, add a unit test for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
The added
ListCollectionsOperationTest
tests the public methodListCollectionsOperation.execute
instead of testing the (currently private) methodgetCommand
. This way it covers more ofListCollectionsOperation
. Since mocking is needed for this, I introducedMongoMockito.mock
(see its documentation for the detailed explanation and links to demo tests). I demonstrated it to @vbabanin and @rozza, and my understanding was that they liked this improvement overMockito.mock
.