Skip to content
Closed
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 @@ -118,6 +118,70 @@ public void usingAnnotationBasedMock() {
);
}

@Test
void removeOpenMocksWhenExtensionAddedDuringJUnit4to5Migration() {
//language=java
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "mockito-core", "junit-4", "hamcrest-3", "junit-jupiter-api-5")),
java(
"""
package org.openrewrite.java.testing.junit5;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.List;

import static org.mockito.Mockito.verify;

public class MockitoTests {
@Mock
List<String> mockedList;

@Before
public void initMocks() {
MockitoAnnotations.openMocks(this);
}

@Test
public void usingAnnotationBasedMock() {
mockedList.add("one");
verify(mockedList).add("one");
}
}
""",
"""
package org.openrewrite.java.testing.junit5;

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

import java.util.List;

import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
public class MockitoTests {
@Mock
List<String> mockedList;

@Test
public void usingAnnotationBasedMock() {
mockedList.add("one");
verify(mockedList).add("one");
}
}
"""
)
);
}

/**
* Mockito 1 used Matchers.anyVararg() to match the arguments to a variadic function.
* Mockito 2+ uses Matchers.any() to match anything including the arguments to a variadic function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,109 @@ void migratesCorrectlyWhenManagedDependency() {
);
}

@Test
void removeOpenMocksWhenExtensionAdded() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api", "mockito-core", "mockito-junit-jupiter")),
//language=java
java(
"""
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

class MyTest {
@Mock
Object myMock;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void test() {
}
}
""",
"""
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

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

@Test
void test() {
}
}
"""
)
);
}

@Test
void removeOpenMocksWithCloseableWhenExtensionAdded() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api", "mockito-core", "mockito-junit-jupiter")),
//language=java
java(
"""
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

class MyTest {
private AutoCloseable mocks;

@Mock
Object myMock;

@BeforeEach
void setUp() {
mocks = MockitoAnnotations.openMocks(this);
}

@AfterEach
void tearDown() throws Exception {
mocks.close();
}

@Test
void test() {
}
}
""",
"""
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class MyTest {

@Mock
Object myMock;

@Test
void test() {
}
}
"""
)
);
}

@Test
void handlesAnyObjectFromMockitoWildCardImport() {
rewriteRun(
Expand Down
Loading