Skip to content
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

Fixes #1597 : Adds lenient for BDD Mockito #2048

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
159 changes: 148 additions & 11 deletions src/main/java/org/mockito/BDDMockito.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
*/
package org.mockito;

import org.mockito.internal.stubbing.OngoingStubbingImpl;
import org.mockito.quality.Strictness;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.LenientStubber;
import org.mockito.stubbing.OngoingStubbing;
import org.mockito.stubbing.Stubber;
import org.mockito.verification.VerificationMode;
Expand Down Expand Up @@ -196,7 +199,7 @@ public <M> M getMock() {
}

/**
* see original {@link Mockito#when(Object)}
* See original {@link Mockito#when(Object)}
* @since 1.8.0
*/
public static <T> BDDMyOngoingStubbing<T> given(T methodCall) {
Expand Down Expand Up @@ -344,7 +347,7 @@ public void shouldHaveNoInteractions() {
* See original {@link Stubber}
* @since 1.8.0
*/
public interface BDDStubber {
public interface BaseBDDStubber {
/**
* See original {@link Stubber#doAnswer(Answer)}
* @since 1.8.0
Expand Down Expand Up @@ -413,7 +416,13 @@ BDDStubber willThrow(
* @since 1.9.0
*/
BDDStubber willCallRealMethod();
}

/**
* See original {@link Stubber}
* @since 1.8.0
*/
public interface BDDStubber extends BaseBDDStubber {
/**
* See original {@link Stubber#when(Object)}
* @since 1.8.0
Expand Down Expand Up @@ -482,23 +491,140 @@ public BDDStubber willCallRealMethod() {
}

/**
* see original {@link Mockito#doThrow(Throwable[])}
* See original {@link org.mockito.stubbing.LenientStubber}
*
* @since 1.8.0
*/
public interface BDDLenientStubber extends BaseBDDStubber {

/**
* See original {@link org.mockito.stubbing.LenientStubber#when(Object)}
*
* @since 3.6.24
*/
<T> BDDMyOngoingStubbing<T> given(T mock);
}

private static class BDDLenientStubberImpl implements BDDLenientStubber {

private LenientStubber mockitoStubber;

public BDDLenientStubberImpl(LenientStubber stubber) {
this.mockitoStubber = stubber;
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doAnswer(Answer)}
* @since 3.6.24
*/
public BDDStubber willAnswer(Answer<?> answer) {
return new BDDStubberImpl(mockitoStubber.doAnswer(answer));
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doAnswer(Answer)}
* @since 3.6.24
*/
public BDDStubber will(Answer<?> answer) {
return new BDDStubberImpl(mockitoStubber.doAnswer(answer));
}

/**
* @deprecated please use {@link #willDoNothing()} instead
*/
@Deprecated
public BDDStubber willNothing() {
return willDoNothing();
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doNothing()}
* @since 3.6.24
*/
public BDDStubber willDoNothing() {
return new BDDStubberImpl(mockitoStubber.doNothing());
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doAnswer(Answer)}
* @since 3.6.24
*/
public BDDStubber willReturn(Object toBeReturned) {
return new BDDStubberImpl(mockitoStubber.doReturn(toBeReturned));
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doReturn(Object, Object...)}
* @since 3.6.24
*/
public BDDStubber willReturn(Object toBeReturned, Object... nextToBeReturned) {
return new BDDStubberImpl(mockitoStubber.doReturn(toBeReturned, nextToBeReturned));
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doThrow(Throwable...)}
* @since 3.6.24
*/
public BDDStubber willThrow(Throwable... toBeThrown) {
return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown));
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doThrow(Class)}
* @since 3.6.24
*/
public BDDStubber willThrow(Class<? extends Throwable> toBeThrown) {
return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown));
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doThrow(Class, Class[])}
* @since 3.6.24
*/
public BDDStubber willThrow(
Class<? extends Throwable> toBeThrown,
Class<? extends Throwable>... nextToBeThrown) {
return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown, nextToBeThrown));
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#doCallRealMethod()}
* @since 3.6.24
*/
public BDDStubber willCallRealMethod() {
return new BDDStubberImpl(mockitoStubber.doCallRealMethod());
}

/**
* See original {@link org.mockito.internal.stubbing.DefaultLenientStubber#when(Object)}
* @since 3.6.24
*/
@Override
public <T> BDDMyOngoingStubbing<T> given(T mock) {
OngoingStubbingImpl<T> ongoingStubbing = (OngoingStubbingImpl) Mockito.when(mock);
ongoingStubbing.setStrictness(Strictness.LENIENT);
return new BDDOngoingStubbingImpl<T>(ongoingStubbing);
}
}

/**
* See original {@link Mockito#doThrow(Throwable[])}
* @since 2.1.0
*/
public static BDDStubber willThrow(Throwable... toBeThrown) {
return new BDDStubberImpl(Mockito.doThrow(toBeThrown));
}

/**
* see original {@link Mockito#doThrow(Class)}
* See original {@link Mockito#doThrow(Class)}
* @since 1.9.0
*/
public static BDDStubber willThrow(Class<? extends Throwable> toBeThrown) {
return new BDDStubberImpl(Mockito.doThrow(toBeThrown));
}

/**
* see original {@link Mockito#doThrow(Class)}
* See original {@link Mockito#doThrow(Class)}
* @since 1.9.0
*/
public static BDDStubber willThrow(
Expand All @@ -507,39 +633,39 @@ public static BDDStubber willThrow(
}

/**
* see original {@link Mockito#doAnswer(Answer)}
* See original {@link Mockito#doAnswer(Answer)}
* @since 1.8.0
*/
public static BDDStubber willAnswer(Answer<?> answer) {
return new BDDStubberImpl(Mockito.doAnswer(answer));
}

/**
* see original {@link Mockito#doAnswer(Answer)}
* See original {@link Mockito#doAnswer(Answer)}
* @since 2.1.0
*/
public static BDDStubber will(Answer<?> answer) {
return new BDDStubberImpl(Mockito.doAnswer(answer));
}

/**
* see original {@link Mockito#doNothing()}
* See original {@link Mockito#doNothing()}
* @since 1.8.0
*/
public static BDDStubber willDoNothing() {
return new BDDStubberImpl(Mockito.doNothing());
}

/**
* see original {@link Mockito#doReturn(Object)}
* See original {@link Mockito#doReturn(Object)}
* @since 1.8.0
*/
public static BDDStubber willReturn(Object toBeReturned) {
return new BDDStubberImpl(Mockito.doReturn(toBeReturned));
}

/**
* see original {@link Mockito#doReturn(Object, Object...)}
* See original {@link Mockito#doReturn(Object, Object...)}
* @since 2.1.0
*/
@SuppressWarnings({"unchecked", "varargs"})
Expand All @@ -548,10 +674,21 @@ public static BDDStubber willReturn(Object toBeReturned, Object... toBeReturnedN
}

/**
* see original {@link Mockito#doCallRealMethod()}
* See original {@link Mockito#doCallRealMethod()}
* @since 1.8.0
*/
public static BDDStubber willCallRealMethod() {
return new BDDStubberImpl(Mockito.doCallRealMethod());
}

/**
* Due to conflict with the original method {@link Mockito#lenient()}, this method has to break naming consistency
* (issue <a href="https://github.com/mockito/mockito/pull/2048">#2048</a>)
*
* See original {@link Mockito#lenient()}
* @since 3.6.24
*/
public static BDDLenientStubber leniently() {
return new BDDLenientStubberImpl(Mockito.lenient());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static java.util.Arrays.asList;

import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.leniently;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -148,4 +150,37 @@ public void deduplicates_stubbings_by_location() throws Exception {
// then
assertEquals(1, stubbings.size());
}

@Test
public void bdd_unused_and_lenient_stubbings() throws Exception {
given(mock1.simpleMethod(1)).willReturn("1");
given(mock1.simpleMethod(2)).willReturn("2");
leniently().given(mock2.simpleMethod(3)).willReturn("3");

mock1.simpleMethod(1);

// when
UnusedStubbings stubbings = finder.getUnusedStubbings((List) asList(mock1, mock2));

// then
assertEquals(1, stubbings.size());
assertEquals("[mock1.simpleMethod(2); stubbed with: [Returns: 2]]", stubbings.toString());
}

@Test
public void bdd_some_unused_stubbings_by_location() throws Exception {
given(mock1.simpleMethod(1)).willReturn("1");
given(mock2.simpleMethod(2)).willReturn("2");
given(mock2.simpleMethod(3)).willReturn("3");
leniently().given(mock2.differentMethod()).willReturn("4"); //will not be included in results

mock2.simpleMethod(2);

// when
Collection stubbings = finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2));

// then
assertEquals(2, stubbings.size());
assertEquals("[mock1.simpleMethod(1);, mock2.simpleMethod(3);]", stubbings.toString());
}
}
4 changes: 4 additions & 0 deletions src/test/java/org/mockitousage/strictness/ProductionCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public static void simpleMethod(IMethods mock, String argument) {
public static void simpleMethod(IMethods mock, int argument) {
mock.simpleMethod(argument);
}

public static void differentMethod(IMethods mock, String argument) {
mock.differentMethod(argument);
}
}