-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from mockito/fix-10
Possibility to configure strictness level
- Loading branch information
Showing
6 changed files
with
210 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2020 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.testng; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.mockito.quality.Strictness; | ||
|
||
/** | ||
* Annotation that can configure Mockito settings. Used by {@link MockitoTestNGListener} | ||
*/ | ||
@Inherited | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
@Documented | ||
public @interface MockitoSettings { | ||
|
||
/** | ||
* Configure the strictness used in this test. | ||
* | ||
* @return The strictness to configure, by default {@link Strictness#STRICT_STUBS} | ||
*/ | ||
Strictness strictness() default Strictness.STRICT_STUBS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/test/java/org/mockitousage/testng/InjectMocksWithConstructorAndFinalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2020 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.testng; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Random; | ||
|
||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
@Listeners(MockitoTestNGListener.class) | ||
public class InjectMocksWithConstructorAndFinalTest { | ||
|
||
interface Client { | ||
int aMethod(); | ||
} | ||
|
||
static class Service { | ||
final Client client; | ||
|
||
public Service(Client client) { | ||
this.client = client; | ||
} | ||
|
||
int callClient() { | ||
return client.aMethod(); | ||
} | ||
} | ||
|
||
@Mock | ||
private Client client; | ||
|
||
@InjectMocks | ||
private Service service; | ||
|
||
private int lastClientHash = 0; | ||
|
||
@AfterMethod | ||
void cleanup() { | ||
// final field will be not assigned for next test | ||
// for new object constructor will be used | ||
service = null; | ||
} | ||
|
||
@Test(invocationCount = 4) | ||
void inject_mock_should_be_refreshed() { | ||
|
||
// we have correct injected mock | ||
assertThat(client).isNotNull(); | ||
assertThat(service.client).isNotNull().isSameAs(client); | ||
|
||
// we have new mock | ||
assertThat(lastClientHash).isNotEqualTo(client.hashCode()); | ||
|
||
// clear mock | ||
assertThat(service.callClient()).isZero(); | ||
|
||
// make some stub | ||
int i = new Random().nextInt() + 1; | ||
when(client.aMethod()).thenReturn(i); | ||
|
||
// and test it | ||
assertThat(service.callClient()).isEqualTo(i); | ||
|
||
verify(client, times(2)).aMethod(); | ||
|
||
// remember last mock hash | ||
lastClientHash = client.hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/org/mockitousage/testng/StrictnessWarnTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2020 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.testng; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
|
||
import org.mockito.Mock; | ||
import org.mockito.quality.Strictness; | ||
import org.mockito.testng.MockitoSettings; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
@Listeners(MockitoTestNGListener.class) | ||
@MockitoSettings(strictness = Strictness.WARN) | ||
public class StrictnessWarnTest { | ||
|
||
@Mock | ||
private List<String> list; | ||
|
||
@BeforeMethod | ||
void setup() { | ||
when(list.add("a")).thenReturn(true); | ||
} | ||
|
||
@Test | ||
void not_used_stub_generate_warn() { | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.1.* | ||
version=0.2.* |