Skip to content

Commit 1e85a69

Browse files
authored
Merge pull request #28 from picimako/v050
Changes for v0.5.0
2 parents 53d5527 + cca0e67 commit 1e85a69

File tree

131 files changed

+5820
-695
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+5820
-695
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44

55
## [Unreleased]
66

7+
## [0.5.0]
8+
### Added
9+
- [#19](https://github.com/picimako/mockitools/issues/19): Added bulk options to convert verifications from `BDDMockito.then()` and `Mockito.verify()` by selecting one or more
10+
verifications in the editor.
11+
- [#20](https://github.com/picimako/mockitools/issues/20): Extended the conversion of `InOrder.verify()` call chains with a bulk mode,
12+
so that if multiple (or just a single) such call chain is selected/highlighted in the editor, they all can be converted to the same approach at once.
13+
- [#21](https://github.com/picimako/mockitools/issues/21): Added an inspection to report InOrder local variables that are used in only one verification call.
14+
Besides `MockedStatic`, it can report for both `InOrder.verify()` and `BDDMockito.then().should(InOrder)` verifications.
15+
- [#22](https://github.com/picimako/mockitools/issues/22): Added a new action to convert from and to `InOrder.verify()` based verification.
16+
Along with it, extended the possible conversion options from and to `Mockito.verify()` and `BDDMockito.then()` as well.
17+
Details are available in the [Verifications](https://github.com/picimako/mockitools/blob/main/docs/verifications.md) document.
18+
- [#23](https://github.com/picimako/mockitools/issues/23): Added a quick fix, so that checked exceptions can be added to the stubbed method's `throws` clause
19+
when they are specified in the stubbing, but not in the mentioned `throws` clause.
20+
- [#24](https://github.com/picimako/mockitools/issues/24): Strictness configuration is now supported by the conversion between `@Mock` and `Mockito.mock(Class, MockSettings)` in both directions.
21+
- `EnforceConventionInspection` can now report `InOrder.verify()` calls too, as part of enforcing `BDDMockito.then()` verification.
22+
- [#27](https://github.com/picimako/mockitools/issues/27): Added an inspection that can report mock objects used in misconfigured `InOrder` verifications.
23+
24+
### Changed
25+
- Removed support for IJ2021.1. From now on 2021.2 is the earliest version supported.
26+
27+
### Fixed
28+
- Fixed an issue that when converting from `BDDMockito.then().should(InOrder)` to `Mockito.verify()` the InOrder variable remained.
29+
730
## [0.4.1]
831
### Changed
932
- Added support for IJ2022.2.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Version](https://img.shields.io/jetbrains/plugin/v/18117-mockitools.svg)](https://plugins.jetbrains.com/plugin/18117-mockitools)
44
![Build](https://github.com/picimako/mockitools/workflows/Build/badge.svg)
55
[![Downloads](https://img.shields.io/jetbrains/plugin/d/18117-mockitools.svg)](https://plugins.jetbrains.com/plugin/18117-mockitools)
6-
![](https://img.shields.io/badge/since-IJ2021.1-blue) ![](https://img.shields.io/badge/until-IJ2022.2-blue)
6+
![](https://img.shields.io/badge/since-IJ2021.2-blue) ![](https://img.shields.io/badge/until-IJ2022.2-blue)
77
[![PayPal](https://img.shields.io/badge/donate-paypal-yellow.svg)](https://www.paypal.com/donate/?hosted_button_id=VVLWNZWPBRUDL)
88
[![BuyMeACoffee](https://img.shields.io/badge/donate-buymeacoffee-yellow.svg)](https://www.buymeacoffee.com/picimako)
99

@@ -37,7 +37,8 @@ You can find the list of those inspections in the [Mockitools/SonarLint rules](d
3737

3838
## Why Mockitools is tasty
3939

40-
If [*"Mockito ... tastes really good"* and *"doesn’t give you hangover"*](https://site.mockito.org/#why), this IDE plugin will only make it better. You can find out why, in the dedicated documentation below.
40+
If [*"Mockito ... tastes really good"* and *"doesn’t give you hangover"*](https://site.mockito.org/#why),
41+
this IDE plugin will only make it better. You can find out why, in the dedicated documentation below.
4142

4243
- [Mock creation](docs/mock_creation.md)
4344
- [Stubbing](docs/stubbing.md)

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
// Java support
77
id("java")
88
// Kotlin support
9-
id("org.jetbrains.kotlin.jvm") version "1.6.21"
9+
id("org.jetbrains.kotlin.jvm") version "1.7.0"
1010
// Gradle IntelliJ Plugin
1111
id("org.jetbrains.intellij") version "1.5.3"
1212
// Gradle Changelog Plugin
@@ -26,7 +26,7 @@ repositories {
2626
// Set the JVM language level used to compile sources and generate files - Java 11 is required since 2020.3
2727
kotlin {
2828
jvmToolchain {
29-
(this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(11))
29+
languageVersion.set(JavaLanguageVersion.of(11))
3030
}
3131
}
3232

docs/mock_creation.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ to: @Mock Clazz clazz;
318318

319319
from: mock(Clazz.class, Mockito.withSettings().extraInterfaces(List.class))
320320
to: @Mock(extraInterfaces = List.class) Clazz clazz;
321+
322+
from: mock(Clazz.class, Mockito.withSettings().strictness(Strictness.WARN))
323+
to: @Mock(strictness = Mock.Strictness.WARN) Clazz clazz;
321324
```
322325

323326
Furthermore, the type that is being mocked should be mockable either by Mockito's rules or not being annotated with `@DoNotMock`.

docs/stubbing.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
![](https://img.shields.io/badge/inspection-orange) ![](https://img.shields.io/badge/since-0.3.0-blue) [![](https://img.shields.io/badge/implementation-ThrowsCheckedExceptionStubbingInspection-blue)](../src/main/java/com/picimako/mockitools/inspection/ThrowsCheckedExceptionStubbingInspection.java)
66

7-
Reports exception references in <code>*Throw()</code> stubbing methods based on Mockito's rule on checked exceptions
7+
Reports exception references in `*Throw()` stubbing methods based on Mockito's rule on checked exceptions
88

99
> If [the specified exception types] contain checked exceptions then they have to match one of the checked exceptions in the method signature.
1010
@@ -14,17 +14,20 @@ The following constructs are supported:
1414
- `Mockito.doThrow(...).when()`
1515
- `BDDMockito.willThrow(...).given()`
1616

17-
In case of each way of stubbing, further chained <code>*Throw()</code> calls are supported too. In case of an empty list, no problem is reported.
17+
In case of each way of stubbing, further chained `*Throw()` calls are supported too. In case of an empty list, no problem is reported.
18+
19+
A quick fix is also available that adds the reported exception to the `throws` clause of the stubbed method.
1820

1921
**Example:**
2022

2123
```java
2224
void testMethod() {
2325
MockObject mock = new MockObject();
24-
// IOException is NOT reported because it is in MockObject#doSomething()'s throws list
25-
// IllegalArgumentException is NOT reported because it is not a checked exception
26-
// SqlException IS reported because it is not is in the throws list
27-
Mockito.doThrow(IOException.class, IllegalArgumentException.class, SqlException.class).when(mock).doSomething();
26+
Mockito.doThrow(
27+
IOException.class, //NOT reported because it is in MockObject#doSomething()'s throws list
28+
IllegalArgumentException.class, //NOT reported because it is not a checked exception
29+
SqlException.class) //reported because it is not in the throws list
30+
.when(mock).doSomething();
2831
}
2932

3033
class MockObject {

docs/verifications.md

Lines changed: 111 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,72 @@ void testMethod() {
8181
}
8282
```
8383

84+
## InOrder with a single verification
85+
86+
![](https://img.shields.io/badge/inspection-orange) ![](https://img.shields.io/badge/since-0.5.0-blue)
87+
[![](https://img.shields.io/badge/impl-SingleInOrderVerificationInspection-blue)](../src/main/java/com/picimako/mockitools/inspection/verification/SingleInOrderVerificationInspection.java)
88+
89+
This inspection reports `InOrder` local variables on which only one verification is called in the form of either
90+
an `InOrder.verify()` or a `BDDMockito.then().should(InOrder)` call.
91+
92+
The report can be useful when someone starts implementing an `InOrder` verification to remind them that further verifications need to be implemented,
93+
and also cases when:
94+
- the user forgot to add further verification calls,
95+
- a verification started out as `InOrder` but was forgotten to be converted to simple verification when he/she changed his/her mind
96+
97+
```java
98+
InOrder inOrder = Mockito.inOrder(mock); //the variable name is highlighted
99+
inOrder.verify(mock).doSomething();
100+
```
101+
102+
## Misconfigured InOrder verifications
103+
104+
![](https://img.shields.io/badge/inspection-orange) ![](https://img.shields.io/badge/since-0.5.0-blue)
105+
[![](https://img.shields.io/badge/impl-UnusedOrUnconfiguredMockInInOrderVerificationInspection-blue)](../src/main/java/com/picimako/mockitools/inspection/verification/UnusedOrUnconfiguredMockInInOrderVerificationInspection.java)
106+
107+
This inspection reports mock objects in `InOrder` verifications in the following cases:
108+
109+
- The mock is added to the arguments of `Mockito.inOrder()` but is not used in any verification performed via that `InOrder` object.
110+
- The mock is used in an `InOrder` verification, but it is not added to the arguments of `Mockito.inOrder()`.
111+
112+
It can report mocks both in `InOrder.verify()` and `BDDMockito.then().should(InOrder)`.
113+
114+
```java
115+
InOrder unusedMock = Mockito.inOrder(mock, mock2); //mock2 is reported since it is not used in any of the verifications
116+
unusedMock.verify(mock).doSomething();
117+
unusedMock.verify(mock, Mockito.times(2)).doSomething();
118+
119+
InOrder unconfiguredMock = Mockito.inOrder(mock);
120+
unconfiguredMock.verify(mock).doSomething();
121+
unconfiguredMock.verify(mock2).doSomething(); //mock2 is reported since it is not added to 'Mockito.inOrder()'
122+
```
123+
84124
## Convert between various verification approaches
85125

86-
![](https://img.shields.io/badge/intention-orange) ![](https://img.shields.io/badge/since-0.4.0-blue)
87-
[![](https://img.shields.io/badge/impl-ConvertMockitoVerifyToBDDMockitoThenIntention-blue)](../src/main/java/com/picimako/mockitools/intention/convert/verification/ConvertMockitoVerifyToBDDMockitoThenIntention.java)
88-
[![](https://img.shields.io/badge/impl-ConvertBDDMockitoThenToMockitoVerifyIntention-blue)](../src/main/java/com/picimako/mockitools/intention/convert/verification/ConvertBDDMockitoThenToMockitoVerifyIntention.java)
126+
![](https://img.shields.io/badge/intention-orange) ![](https://img.shields.io/badge/since-0.4.0-blue) ![](https://img.shields.io/badge/since-0.5.0-blue)
127+
[![](https://img.shields.io/badge/impl-ConvertMockitoVerifyToBDDMockitoThenIntention-blue)](../src/main/java/com/picimako/mockitools/intention/convert/verification/mockitoverify/ConvertFromMockitoVerifyIntention.java)
128+
[![](https://img.shields.io/badge/impl-ConvertBDDMockitoThenToMockitoVerifyIntention-blue)](../src/main/java/com/picimako/mockitools/intention/convert/verification/bddmockitothen/ConvertFromBDDMockitoIntention.java)
129+
[![](https://img.shields.io/badge/impl-ConvertBDDMockitoThenToMockitoVerifyIntention-blue)](../src/main/java/com/picimako/mockitools/intention/convert/verification/inorderverify/ConvertFromInOrderVerifyIntention.java)
89130

90-
There are a couple of ways one can approach verification in Mockito: via `org.mockito.Mockito` and `org.mockito.BDDMockito`.
131+
There are a couple of ways one can approach verification in Mockito: via `org.mockito.Mockito`, `org.mockito.BDDMockito`, `org.mockito.InOrder`.
91132

92-
These intentions can convert between the `Mockito.verify()` and `BDDMockito.then()` call chains if they satisfy the following criteria:
93-
- if the ['Enforce conventions' inspection](conventions.md#enforce-orgmockitomockito-over-orgmockitobddmockito-and-vice-versa) doesn't enforce
94-
the verification the user converts from,
95-
- in case of `Mockito.verify()`, a call on the mock object after `verify()` must be present,
133+
These intentions can convert between those approaches if they satisfy some preconditions:
134+
- in case of `Mockito.verify()` and `InOrder.verify()`, a call must be present on the mock object after `verify()` ,
96135
- while in case of `BDDMockito.then()`, both the `should()` call and a call on the mock object after that must be present.
97136

98-
Conversion of `InOrder` verification is not supported at the moment.
137+
Below you can see the details of the conversion directions when converting single verifications. Bulk conversions are handled and detailed separately.
138+
139+
Also, reusing an existing `InOrder` instance is not possible when converting to an `InOrder` specific approach,
140+
it always creates a new `InOrder` local variable.
141+
142+
| Conversion from | Options by default | Options when `org.mockito.Mockito` is enforced | Options when `org.mockito.BDDMockito` is enforced |
143+
|-------------------------------------|------------------------------------------------------------------------------------|------------------------------------------------|-------------------------------------------------------------|
144+
| `Mockito.verify()` | `InOrder.verify()`<br/>`BDDMockito.then()`<br/>`BDDMockito.then().should(InOrder)` | `InOrder.verify()` | `BDDMockito.then()`<br/>`BDDMockito.then().should(InOrder)` |
145+
| `BDDMockito.then()` | `Mockito.verify()`<br/>`InOrder.verify()`<br/>`BDDMockito.then().should(InOrder)` | `Mockito.verify()`<br/>`InOrder.verify()` | `BDDMockito.then().should(InOrder)` |
146+
| `BDDMockito.then().should(InOrder)` | `Mockito.verify()`<br/>`InOrder.verify()` | `Mockito.verify()`<br/>`InOrder.verify()` | No action is available. |
147+
| `InOrder.verify()` | `Mockito.verify()`<br/>`BDDMockito.then()`<br/>`BDDMockito.then().should(InOrder)` | `Mockito.verify()` | `BDDMockito.then()`<br/>`BDDMockito.then().should(InOrder)` |
99148

100-
**Examples for Mockito.verify() -> BDDMockito.then() direction:**
149+
**Example (Mockito.verify() -> BDDMockito.then()):**
101150

102151
```java
103152
//Without verification mode
@@ -108,3 +157,55 @@ From: Mockito.verify(mock).doSomething();
108157
From: Mockito.verify(mock, times(2)).doSomething();
109158
to: BDDMockito.then(mock).should(times(2)).doSomething();
110159
```
160+
161+
**Example (BDDMockito.then() -> InOrder.verify()):**
162+
163+
```java
164+
From: BDDMockito.then(mock).should().doSomething();
165+
to:
166+
InOrder inOrder = Mockito.inOrder(mock);
167+
inOrder.verify(mock).doSomething();
168+
```
169+
170+
### Selection based conversion
171+
172+
![](https://img.shields.io/badge/since-0.5.0-blue)
173+
174+
Conversion of one or more verification call chains is also available via selection in the editor.
175+
It can convert between `org.mockito.Mockito`, `org.mockito.BDDMockito` and `org.mockito.InOrder` in any direction, with some nuances that should be taken into consideration.
176+
177+
The availability is the same, while the conversion logic is mostly the same, as for the single conversion options:
178+
- when converting from `BDDMockito.then()`
179+
- `InOrder.verify()` as a target is available only when **all** `BDDMockito.then()` chains in the selection use an `InOrder` variable, and they use the same one,
180+
- adding an `InOrder` to the `should()` call is available only when **none** of the `BDDMockito.then()` chains use an `InOrder` variable.
181+
- when a new `InOrder` local variable is created, it is used in all selected and converted verifications. If you want to use different ones for different verifications,
182+
you can convert them one by one.
183+
184+
#### Examples
185+
186+
Selections are between [\[ and ]].
187+
188+
**InOrder.verify() -> BDDMockito.then()**
189+
190+
```java
191+
From:
192+
InOrder inOrder = Mockito.inOrder(mock, mock2);
193+
[[inOrder.verify(mock).doSomething();
194+
inOrder.verify(mock2, times(2)).doSomething();]]
195+
to:
196+
InOrder inOrder = Mockito.inOrder(mock, mock2);
197+
BDDMockito.then(mock).should().doSomething();
198+
BDDMockito.then(mock2).should(times(2)).doSomething();
199+
```
200+
201+
**Mockito.verify() -> InOrder.verify()**
202+
203+
```java
204+
From:
205+
[[Mockito.verify(mock).doSomething();
206+
Mockito.verify(mock2, times(2)).doSomething();]]
207+
to:
208+
InOrder inOrder = Mockito.inOrder(mock, mock2);
209+
inOrder.verify(mock).doSomething();
210+
inOrder.verify(mock2, times(2)).doSomething();
211+
```

gradle.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33

44
pluginGroup = com.picimako.mockitools
55
pluginName = Mockitools
6-
pluginVersion = 0.4.1
6+
pluginVersion = 0.5.0
77

88
# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
99
# for insight into build numbers and IntelliJ Platform versions.
10-
pluginSinceBuild = 211
10+
pluginSinceBuild = 212
1111
pluginUntilBuild = 222.*
1212

1313
# IntelliJ Platform Properties -> https://github.com/JetBrains/gradle-intellij-plugin#intellij-platform-properties
1414
platformType = IC
15-
platformVersion = 2021.1.3
15+
platformVersion = 2021.2
1616

1717
# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
1818
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
1919
platformPlugins = java
2020

2121
# Gradle Releases -> https://github.com/gradle/gradle/releases
22-
gradleVersion = 7.4
22+
gradleVersion = 7.4.2
2323

2424
# Opt-out flag for bundling Kotlin standard library.
2525
# See https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library for details.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Binary file not shown.

src/main/java/com/picimako/mockitools/inspection/ClassObjectAccessUtil.java renamed to src/main/java/com/picimako/mockitools/ClassObjectAccessUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//Copyright 2021 Tamás Balog. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
22

3-
package com.picimako.mockitools.inspection;
3+
package com.picimako.mockitools;
44

55
import com.intellij.psi.PsiClass;
66
import com.intellij.psi.PsiClassObjectAccessExpression;

0 commit comments

Comments
 (0)