Skip to content

Commit

Permalink
Merge pull request #1523 from mockito/next-ann
Browse files Browse the repository at this point in the history
Added 'lenient' annotation toggle
  • Loading branch information
mockitoguy committed Nov 19, 2018
2 parents 7799c46 + 7fd9a23 commit 6435ef5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/org/mockito/Mock.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.annotation.Target;

import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
Expand Down Expand Up @@ -67,13 +68,41 @@
@Documented
public @interface Mock {

/**
* Mock will have custom answer, see {@link MockSettings#defaultAnswer(Answer)}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
Answers answer() default Answers.RETURNS_DEFAULTS;

/**
* Mock will be 'stubOnly', see {@link MockSettings#stubOnly()}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
boolean stubOnly() default false;

/**
* Mock will have custom name (shown in verification errors), see {@link MockSettings#name(String)}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
String name() default "";

/**
* Mock will have extra interfaces, see {@link MockSettings#extraInterfaces(Class[])}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
Class<?>[] extraInterfaces() default {};

/**
* Mock will be serializable, see {@link MockSettings#serializable()}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
boolean serializable() default false;

/**
* Mock will be lenient, see {@link MockSettings#lenient()}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*
* @since 2.23.3
*/
boolean lenient() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static Object processAnnotationForMock(Mock annotation, Class<?> type, St
if(annotation.stubOnly()){
mockSettings.stubOnly();
}
if(annotation.lenient()){
mockSettings.lenient();
}

// see @Mock answer default value
mockSettings.defaultAnswer(annotation.answer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/

package org.mockitousage.strictness;

import org.assertj.core.api.Assertions;
import org.assertj.core.api.ThrowableAssert;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.exceptions.misusing.PotentialStubbingProblem;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;
import org.mockitousage.IMethods;

import static org.mockito.Mockito.when;

public class LenientMockAnnotationTest {

public @Rule MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Mock(lenient = true) IMethods lenientMock;
@Mock IMethods regularMock;

@Test
public void mock_is_lenient() {
when(lenientMock.simpleMethod("1")).thenReturn("1");
when(regularMock.simpleMethod("2")).thenReturn("2");

//then lenient mock does not throw:
lenientMock.simpleMethod("3");

//but regular mock throws:
Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
public void call() {
regularMock.simpleMethod("4");
}
}).isInstanceOf(PotentialStubbingProblem.class);
}
}

0 comments on commit 6435ef5

Please sign in to comment.