Skip to content

Commit e9497f1

Browse files
yurii-yuSheng Yutimtebeekgithub-actions[bot]
authored
Handle multiple blocks in JMockit Expectations (#596)
* Add logic to handle multiple blocks(with curly braces) in one Expectations declaration. This is a rare and bizzare way to "better" readability because it "groups" related statements together, but there's nothing wrong with the syntax. * Add test code for the "multiple blocks in one expectations" case * add redundant curly braces to test bizarre cases * format code * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * follow the right coding convention * Use TypeUtils and apply formatter * Reuse already assigned variable instead of repeat expression --------- Co-authored-by: Sheng Yu <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]>
1 parent e037d3d commit e9497f1

File tree

3 files changed

+98
-17
lines changed

3 files changed

+98
-17
lines changed

src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ static Optional<JMockitBlockType> getJMockitBlock(Statement s) {
3636
J.Identifier clazz = (J.Identifier) nc.getClazz();
3737

3838
// JMockit block should be composed of a block within another block
39-
if (nc.getBody() == null || nc.getBody().getStatements().size() != 1) {
39+
if (nc.getBody() == null || (nc.getBody().getStatements().size() != 1 &&
40+
!TypeUtils.isAssignableTo("mockit.Expectations", clazz.getType()))) {
4041
return empty();
4142
}
4243

43-
JMockitBlockType blockType = JMockitBlockType.valueOf(clazz.getSimpleName());
44-
if (blockType != null && TypeUtils.isOfClassType(clazz.getType(), blockType.getFqn())) {
45-
return Optional.of(blockType);
46-
}
47-
return empty();
44+
return Optional.of(JMockitBlockType.valueOf(clazz.getSimpleName()));
4845
}
4946
}

src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,29 @@ J.Block rewriteMethodBody() {
5252
assert nc.getBody() != null;
5353
J.Block expectationsBlock = (J.Block) nc.getBody().getStatements().get(0);
5454

55+
// Account for Expectations which may contain multiple blocks
56+
List<Statement> statementList = new ArrayList<>();
57+
if (TypeUtils.isAssignableTo("mockit.Expectations", nc.getType())) {
58+
statementList.addAll(nc.getBody().getStatements());
59+
} else {
60+
statementList.add(expectationsBlock);
61+
}
62+
5563
// statement needs to be moved directly before expectations class instantiation
5664
JavaCoordinates coordinates = nc.getCoordinates().before();
5765
List<Statement> newExpectationsBlockStatements = new ArrayList<>();
58-
for (Statement expectationStatement : expectationsBlock.getStatements()) {
59-
if (!isSetupStatement(expectationStatement, spies)) {
60-
newExpectationsBlockStatements.add(expectationStatement);
61-
continue;
66+
for (Statement st : statementList) {
67+
for (Statement expectationStatement : ((J.Block) st).getStatements()) {
68+
if (!isSetupStatement(expectationStatement, spies)) {
69+
newExpectationsBlockStatements.add(expectationStatement);
70+
continue;
71+
}
72+
rewriteBodyStatement(expectationStatement, coordinates);
73+
// subsequent setup statements are moved in order
74+
coordinates = expectationStatement.getCoordinates().after();
6275
}
63-
rewriteBodyStatement(expectationStatement, coordinates);
64-
// subsequent setup statements are moved in order
65-
coordinates = expectationStatement.getCoordinates().after();
6676
}
77+
6778
// the new expectations block has the setup statements removed
6879
J.Block newExpectationsBlock = expectationsBlock.withStatements(newExpectationsBlockStatements);
6980
nc = nc.withBody(nc.getBody().withStatements(Collections.singletonList(newExpectationsBlock)));
@@ -128,7 +139,7 @@ private static boolean isNotMockIdentifier(J.Identifier identifier, Set<String>
128139
return false;
129140
}
130141
if (identifier.getType() instanceof JavaType.Method
131-
&& TypeUtils.isAssignableTo("mockit.Invocations",
142+
&& TypeUtils.isAssignableTo("mockit.Invocations",
132143
((JavaType.Method) identifier.getType()).getDeclaringType())) {
133144
return false;
134145
}
@@ -138,8 +149,8 @@ private static boolean isNotMockIdentifier(J.Identifier identifier, Set<String>
138149
}
139150
for (JavaType.FullyQualified annotationType : fieldType.getAnnotations()) {
140151
if (TypeUtils.isAssignableTo("mockit.Mocked", annotationType)
141-
|| TypeUtils.isAssignableTo("mockit.Injectable", annotationType)
142-
|| TypeUtils.isAssignableTo("mockit.Tested", annotationType)) {
152+
|| TypeUtils.isAssignableTo("mockit.Injectable", annotationType)
153+
|| TypeUtils.isAssignableTo("mockit.Tested", annotationType)) {
143154
return false;
144155
}
145156
}

src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,80 @@ void test() {
14191419
}
14201420

14211421
@Test
1422-
void whenMultipleExpectationsNoResults() {
1422+
void whenMultipleBlockInSingleExpectation() {
1423+
//language=java
1424+
rewriteRun(
1425+
java(
1426+
"""
1427+
class MyObject {
1428+
public String getX() {
1429+
return "X";
1430+
}
1431+
public String getY() {
1432+
return "Y";
1433+
}
1434+
}
1435+
"""
1436+
),
1437+
java(
1438+
"""
1439+
import mockit.Expectations;
1440+
import mockit.Mocked;
1441+
import mockit.integration.junit5.JMockitExtension;
1442+
import org.junit.jupiter.api.extension.ExtendWith;
1443+
1444+
import static org.junit.jupiter.api.Assertions.assertEquals;
1445+
import static org.junit.jupiter.api.Assertions.assertNull;
1446+
1447+
@ExtendWith(JMockitExtension.class)
1448+
class MyTest {
1449+
@Mocked
1450+
MyObject myObject;
1451+
1452+
void test() {
1453+
new Expectations() {
1454+
{
1455+
myObject.getX();
1456+
result = "x1";
1457+
}
1458+
{
1459+
myObject.getY();
1460+
result = "y1";
1461+
}
1462+
};
1463+
assertEquals("x1", myObject.getX());
1464+
assertEquals("y1", myObject.getY());
1465+
}
1466+
}
1467+
""",
1468+
"""
1469+
import org.junit.jupiter.api.extension.ExtendWith;
1470+
import org.mockito.Mock;
1471+
import org.mockito.junit.jupiter.MockitoExtension;
1472+
1473+
import static org.junit.jupiter.api.Assertions.assertEquals;
1474+
import static org.junit.jupiter.api.Assertions.assertNull;
1475+
import static org.mockito.Mockito.when;
1476+
1477+
@ExtendWith(MockitoExtension.class)
1478+
class MyTest {
1479+
@Mock
1480+
MyObject myObject;
1481+
1482+
void test() {
1483+
when(myObject.getX()).thenReturn("x1");
1484+
when(myObject.getY()).thenReturn("y1");
1485+
assertEquals("x1", myObject.getX());
1486+
assertEquals("y1", myObject.getY());
1487+
}
1488+
}
1489+
"""
1490+
)
1491+
);
1492+
}
1493+
1494+
@Test
1495+
void whenMultipleExpectationsNoResults() {
14231496
//language=java
14241497
rewriteRun(
14251498
java(

0 commit comments

Comments
 (0)