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

Add unit tests for JMockit Delegate to mockito migration. Also add test case for comments #557

Merged
merged 6 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,361 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.jmockit;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.testing.jmockit.JMockitTestUtils.setDefaultParserSettings;

/**
* At the moment, JMockit Delegates are not migrated to mockito. What I'm seeing is that they are being trashed
* with the template being printed out. These tests were written to try to replicate this issue, however I was unable to.
* They may help anyone who wants to add Delegate migration.
*/
@Disabled
class JMockitDelegateToMockitoTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
setDefaultParserSettings(spec);
}

@Test
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
void whenNoArgsVoidMethod() {
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
//language=java
rewriteRun(
java(
"""
import mockit.Expectations;
import mockit.Delegate;
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();
result = new Delegate() {
public void wait() {
System.out.println("bla");
}
};
}};
myObject.wait();
}
}
""",
"""
import mockit.Delegate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.when;

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

void test() {
when(myObject.wait()).thenAnswer(invocation -> {
System.out.println("bla");
});
myObject.wait();
}
}
"""
)
);
}

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

import static org.junit.jupiter.api.Assertions.assertEquals;

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

void test() {
new Expectations() {{
myObject.wait(anyLong);
result = new Delegate() {
void wait(long timeoutMs) {
System.out.println("bla");
System.out.println("bla");
}
};
}};
myObject.wait();
}
}
""",
"""
import mockit.Delegate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.when;

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

void test() {
when(myObject.wait(anyLong())).thenAnswer(invocation -> {
System.out.println("bla");
System.out.println("bla");
});
myObject.wait();
}
}
"""
)
);
}

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

import static org.junit.jupiter.api.Assertions.assertEquals;

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

void test() {
new Expectations() {{
myObject.toString();
result = new Delegate() {
String toString() {
String a = "bla";
return a + "foo";
}
};
}};
myObject.toString();
}
}
""",
"""
import mockit.Delegate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

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

void test() {
when(myObject.toString()).thenAnswer(invocation -> {
String a = "bla";
return a + "foo";
});
myObject.toString();
}
}
"""
)
);
}

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

import static org.junit.jupiter.api.Assertions.assertEquals;

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

void test() {
new Expectations() {{
myObject.hashCode();
result = 100;
myObject.toString();
result = new Delegate() {
@SuppressWarnings("unused")
String toString() {
String a = "bla";
return a + "foo";
}
};
}};
myObject.toString();
}
}
""",
"""
import mockit.Delegate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

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

void test() {
when(myObject.hashCode()).thenReturn(100);
when(myObject.toString()).thenAnswer(invocation -> {
String a = "bla";
return a + "foo";
});
myObject.toString();
}
}
"""
)
);
}

@Test
void whenClassArgumentMatcher() {
//language=java
rewriteRun(
java(
"""
import java.util.List;

class MyObject {
public String getSomeField(List<String> input) {
return "X";
}
public String getSomeOtherField(Object input) {
return "Y";
}
}
"""
),
java(
"""
import java.util.ArrayList;
import java.util.List;

import mockit.Delegate;
import mockit.Mocked;
import mockit.Expectations;
import mockit.integration.junit5.JMockitExtension;
import org.junit.jupiter.api.extension.ExtendWith;

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

void test() {
new Expectations() {{
myObject.getSomeField((List<String>) any);
result = new Delegate() {
String getSomeOtherField(List<String> input) {
input.add("foo");
return input.toString();
}
};
}};
myObject.getSomeField(new ArrayList<>());
myObject.getSomeOtherField(new Object());
}
}
""",
"""
import java.util.ArrayList;
import java.util.List;

import mockit.Delegate;

import static org.mockito.Mockito.anyList;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

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

void test() {
when(myObject.getSomeField(anyList())).thenAnswer(invocation -> {
List<String> input = invocation.getArgument(0);
input.add("foo");
return input.toString();
});
myObject.getSomeField(new ArrayList<>());
myObject.getSomeOtherField(new Object());
}
}
"""
)
);
}

}
Loading
Loading