Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JMockit to Mockito IllegalArumentException No enum constant VerificationsInOrder #616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package org.openrewrite.java.testing.jmockit;

import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.java.tree.TypeUtils;

import java.util.Arrays;
import java.util.Optional;

import static java.util.Optional.empty;
Expand All @@ -29,20 +31,19 @@ static Optional<JMockitBlockType> getJMockitBlock(Statement s) {
if (!(s instanceof J.NewClass)) {
return empty();
}

J.NewClass nc = (J.NewClass) s;
if (!(nc.getClazz() instanceof J.Identifier)) {
if (nc.getBody() == null || nc.getClazz() == null) {
return empty();
}
J.Identifier clazz = (J.Identifier) nc.getClazz();

// JMockit block should be composed of a block within another block
if (nc.getBody() == null ||
(nc.getBody().getStatements().size() != 1 &&
!TypeUtils.isAssignableTo("mockit.Expectations", clazz.getType()) &&
!TypeUtils.isAssignableTo("mockit.Verifications", clazz.getType()))) {
JavaType type = nc.getClazz().getType();
if (type == null) {
return empty();
}

return Optional.of(JMockitBlockType.valueOf(clazz.getSimpleName()));
return Arrays.stream(JMockitBlockType.values())
.filter(supportedType -> TypeUtils.isOfClassType(type, supportedType.getFqn()))
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ void test() {
}

@Test
void whenMultipleExpectationsNoResults() {
void whenMultipleExpectationsNoResults() {
//language=java
rewriteRun(
java(
Expand Down Expand Up @@ -1619,4 +1619,46 @@ void test() {
)
);
}

@Test
void whenEmptyBlock() {
//language=java
rewriteRun(
java(
"""
import mockit.Expectations;
import mockit.Mocked;
import mockit.integration.junit5.JMockitExtension;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(JMockitExtension.class)
class MyTest {
@Mocked
Object myObject;

void test() {
new Expectations() {{
}};
myObject.wait(1L);
}
}
""",
"""
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
Object myObject;

void test() {
myObject.wait(1L);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,55 @@ void test() {
)
);
}

@Test
void whenUnsupportedType() {
//language=java
rewriteRun(
java(
"""
import mockit.VerificationsInOrder;
import mockit.Mocked;
import mockit.integration.junit5.JMockitExtension;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(JMockitExtension.class)
class MyTest {
@Mocked
Object myObject;

void test() {
myObject.wait(1L);
myObject.wait(2L, 1);
new VerificationsInOrder() {{
myObject.wait();
myObject.wait(anyLong, anyInt);
}};
}
}
""",
"""
import mockit.VerificationsInOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
Object myObject;

void test() {
myObject.wait(1L);
myObject.wait(2L, 1);
new VerificationsInOrder() {{
myObject.wait();
myObject.wait(anyLong, anyInt);
}};
}
}
"""
)
);
}
}