Skip to content

Commit

Permalink
Start MockitoSession only for test method and BeforeMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Mar 13, 2021
1 parent 949bdac commit f5a6e08
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ By default `MockitoSession` is started with `STRICT_STUBS`.
You can change this behavior by adding `@MockitoSettings` to your test class.

```java
import org.mockito.quality.Strictness;
import org.mockito.testng.MockitoTestNGListener;
import org.mockito.testng.MockitoSettings;
import org.testng.annotations.Listeners;
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/org/mockito/testng/MockitoTestNGListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.mockito.quality.Strictness;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.annotations.Listeners;

Expand All @@ -30,8 +29,9 @@
*
* <ul>
* <li>
* Before any TestNG method, either a <em>configuration method</em> (&#064;BeforeMethod, &#064;BeforeClass, etc)
* or a <em>test</em> method MockitoSession is started by:
* Before any <em>test</em> method or a <em>configuration method</em> <em>&#064;BeforeMethod</em>
* MockitoSession is started by:
*
* <pre class="code"><code class="java">
* Mockito.mockitoSession()
* .initMocks(testInstance)
Expand Down Expand Up @@ -125,16 +125,11 @@ public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
}

private boolean shouldBeRunBeforeInvocation(IInvokedMethod method, ITestResult testResult) {
return !isAfterConfigurationMethod(method) && hasMockitoTestNGListener(testResult);
return (method.isTestMethod() || isBeforeMethod(method)) && hasMockitoTestNGListener(testResult);
}

private boolean isAfterConfigurationMethod(IInvokedMethod method) {
ITestNGMethod testMethod = method.getTestMethod();
return testMethod.isAfterClassConfiguration()
|| testMethod.isAfterMethodConfiguration()
|| testMethod.isAfterGroupsConfiguration()
|| testMethod.isAfterTestConfiguration()
|| testMethod.isAfterSuiteConfiguration();
private boolean isBeforeMethod(IInvokedMethod method) {
return method.getTestMethod().isBeforeMethodConfiguration();
}

private boolean shouldBeRunAfterInvocation(IInvokedMethod method, ITestResult testResult) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
/*
* Copyright (c) 2017 Mockito contributors
* Copyright (c) 2021 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 java.util.Observer;

import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.util.Observer;

import static org.assertj.core.api.Assertions.assertThat;

@Listeners(MockitoTestNGListener.class)
public class EnsureMocksAreInitializedBeforeBeforeClassMethodTest {
public class EnsureMocksAreNotInitializedBeforeBeforeClassMethodTest {

@Mock Observer observer;
@Mock
Observer observer;

@BeforeClass
private void make_sure_mock_is_initialized() {
private void make_sure_mock_is_not_initialized() {
assertThat(observer).isNull();
}

@Test
public void dummy_test1() {
assertThat(observer).isNotNull();
}

@Test
public void dummy_test_see_BeforeClass_code() throws Exception {
public void dummy_test2() {
assertThat(observer).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021 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 java.util.Observer;

import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MockitoTestNGListener.class)
public class EnsureMocksAreNotInitializedBeforeBeforeGroupsMethodTest {

@Mock
Observer observer;

@BeforeGroups
private void make_sure_mock_is_not_initialized() {
assertThat(observer).isNull();
}

@Test
public void dummy_test1() {
assertThat(observer).isNotNull();
}

@Test
public void dummy_test2() {
assertThat(observer).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021 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 java.util.Observer;

import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MockitoTestNGListener.class)
public class EnsureMocksAreNotInitializedBeforeBeforeSuiteMethodTest {

@Mock
Observer observer;

@BeforeSuite
private void make_sure_mock_is_not_initialized() {
assertThat(observer).isNull();
}

@Test
public void dummy_test1() {
assertThat(observer).isNotNull();
}

@Test
public void dummy_test2() {
assertThat(observer).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021 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 java.util.Observer;

import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MockitoTestNGListener.class)
public class EnsureMocksAreNotInitializedBeforeBeforeTestMethodTest {

@Mock
Observer observer;

@BeforeTest
private void make_sure_mock_is_not_initialized() {
assertThat(observer).isNull();
}

@Test
public void dummy_test1() {
assertThat(observer).isNotNull();
}

@Test
public void dummy_test2() {
assertThat(observer).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Test(description = "Always failing, shouldn't be listed in 'mockito-testng.xml'")
public class HasUnusedStubs {
@Mock
List mock;
List<String> mock;
@Test public void test() {
when(mock.add("a")).thenReturn(true);
}
Expand Down
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.2.*
version=0.3.*

0 comments on commit f5a6e08

Please sign in to comment.