Skip to content

Commit

Permalink
Fix times, minTimes, maxTimes, no times logic and add test cases for …
Browse files Browse the repository at this point in the history
…corresponding
  • Loading branch information
timtebeek committed Jun 20, 2024
1 parent dd1b2c4 commit 3fda82d
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.*;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -128,16 +128,22 @@ private void rewriteMethodBody(List<Statement> expectationStatements) {
removeExpectationsStatement();
}

// TODO doesn't cover cases with multiple times eg minTimes and maxTimes - this is typically rare scenario
if (mockInvocationResults.getTimes() != null) {
writeMethodVerification(invocation, mockInvocationResults.getTimes(), "times");
} else if (mockInvocationResults.getMinTimes() != null) {
writeMethodVerification(invocation, mockInvocationResults.getMinTimes(), "atLeast");
} else if (mockInvocationResults.getMaxTimes() != null) {
writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost");
} else if (!hasExpectationsResults) {
// no times, no results
writeMethodVerification(invocation, null, null);
} else {
boolean hasMinTimes = mockInvocationResults.getMinTimes() != null;
boolean hasMaxTimes = mockInvocationResults.getMaxTimes() != null;
if (hasMinTimes || hasMaxTimes) {
if (hasMinTimes) {
writeMethodVerification(invocation, mockInvocationResults.getMinTimes(), "atLeast");
}
if (hasMaxTimes) {
writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost");
}
} else if (!hasExpectationsResults) {
// no times, no results
writeMethodVerification(invocation, null, null);
}
}
}

Expand Down Expand Up @@ -263,7 +269,7 @@ private static void appendToTemplate(StringBuilder templateBuilder, boolean buil
private static String getVerifyTemplate(List<Expression> arguments, String fqn, @Nullable String verificationMode, List<Object> templateParams) {
StringBuilder templateBuilder = new StringBuilder("verify(#{any(" + fqn + ")}"); // verify(object
if (verificationMode != null) {
templateBuilder.append(", " + verificationMode + "(#{any(int)})"); // verify(object, times(2)
templateBuilder.append(", ").append(verificationMode).append("(#{any(int)})"); // verify(object, times(2)
}
templateBuilder.append(").#{}("); // verify(object, times(2)).method(

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,149 @@ void test() {
);
}

@Test
void whenMinTimes() {
//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(anyLong, anyInt);
minTimes = 2;
}};
myObject.wait(10L, 10);
}
}
""",
"""
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
Object myObject;
void test() {
myObject.wait(10L, 10);
verify(myObject, atLeast(2)).wait(anyLong(), anyInt());
}
}
"""
)
);
}

@Test
void whenMaxTimes() {
//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(anyLong, anyInt);
maxTimes = 5;
}};
myObject.wait(10L, 10);
}
}
""",
"""
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
Object myObject;
void test() {
myObject.wait(10L, 10);
verify(myObject, atMost(5)).wait(anyLong(), anyInt());
}
}
"""
)
);
}

@Test
void whenMinTimesMaxTimes() {
//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(anyLong, anyInt);
minTimes = 1;
maxTimes = 3;
}};
myObject.wait(10L, 10);
}
}
""",
"""
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
Object myObject;
void test() {
myObject.wait(10L, 10);
verify(myObject, atLeast(1)).wait(anyLong(), anyInt());
verify(myObject, atMost(3)).wait(anyLong(), anyInt());
}
}
"""
)
);
}

@Test
void whenSpy() {
//language=java
Expand Down

0 comments on commit 3fda82d

Please sign in to comment.