Skip to content

Commit

Permalink
Do not move resource directories to the end of classpath (#1101)
Browse files Browse the repository at this point in the history
- add integration test
  • Loading branch information
karollewandowski committed Sep 20, 2022
1 parent f44786f commit 5b10921
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
29 changes: 29 additions & 0 deletions integration-tests/test-classpath-project-resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Test Classpath Project Resources

## Intention

The intention is to test that the project test resources take priority over resources provided by libraries
and IDE distribution JARs.

## Approach

The test project reproduces the usage of for mocking a final method with Mockito when the PowerMock dependency is
additionally configured in the project.

To be able to mock a final method Mockito must be provided with the inline MockMaker configuration,
which in our case is done by the `src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker` file.

When PowerMock is present, it provides its own `mockito-extensions/org.mockito.plugins.MockMaker` which can be loaded by
Mockito, if PowerMock is before project test resource in the classpath.
Such a situation caused the issue reported in:

- https://github.com/JetBrains/gradle-intellij-plugin/issues/1101

## Verification

The test has two verifications:

1. Project test passes when a final method is mocked with Mockito. It would fail if the test resources directory was
pushed after the PowerMock in the classpath.
2. The test resources directory is before any dependency and IDE distribution JAR in the classpath. We verify IDE JARs
because they may provide MockMaker config in some JAR for any reason.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tasks {
test {
doFirst {
classpath.files.forEach {
println("test-classpath-project-resources: Test classpath entry: ${it.absolutePath}")
}
}
}
}

repositories {
mavenCentral()
}

dependencies {
testImplementation("junit:junit:4.13.2")
testImplementation("org.mockito:mockito-core:4.7.0")
testImplementation("org.powermock:powermock-api-mockito2:2.0.9")
testImplementation("org.powermock:powermock-core:2.0.9")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<idea-plugin>
<id>org.jetbrains.plugins.integration-tests.test-classpath-project-resources</id>
<name>Any</name>
<vendor>Any</vendor>

<depends>com.intellij.modules.platform</depends>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import org.junit.Assert;
import org.junit.Test;

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

public class MyTest {

private final ClassWithFinalMethod testMock = mock(ClassWithFinalMethod.class);

@Test
public void testMockingFinalMethodWithPowerMock() {
String testedString = "overridden value";
when(testMock.finalMethod()).thenReturn(testedString);
String result = testMock.finalMethod();
Assert.assertEquals(testedString, result);
}
}

class ClassWithFinalMethod {
String finalMethod() {
return "final method result";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline
19 changes: 19 additions & 0 deletions integration-tests/test-classpath-project-resources/verify.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env -S kotlin -J-ea

@file:Import("../verify.utils.kts")

__FILE__.init {
runGradleTask("test").let { logs ->
val classpathEntries = logs.lines()
.filter { it.startsWith("test-classpath-project-resources: Test classpath entry:") }
val testResourcesEntryIndex = classpathEntries
.indexOfFirst { it.contains("/test-classpath-project-resources/build/resources/test") }
val powerMockDependencyEntryIndex = classpathEntries
.indexOfFirst { it.contains("powermock") }
val firstIdeJarEntryIndex = classpathEntries
.indexOfFirst { it.contains("/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/") }
assert(testResourcesEntryIndex < powerMockDependencyEntryIndex)
assert(testResourcesEntryIndex < firstIdeJarEntryIndex)
}

}

0 comments on commit 5b10921

Please sign in to comment.