-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not move resource directories to the end of classpath (#1101)
- add integration test
- Loading branch information
1 parent
f44786f
commit 5b10921
Showing
6 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
integration-tests/test-classpath-project-resources/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
20 changes: 20 additions & 0 deletions
20
integration-tests/test-classpath-project-resources/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
7 changes: 7 additions & 0 deletions
7
integration-tests/test-classpath-project-resources/src/main/resources/META-INF/plugin.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
24 changes: 24 additions & 0 deletions
24
integration-tests/test-classpath-project-resources/src/test/java/MyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ath-project-resources/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mock-maker-inline |
19 changes: 19 additions & 0 deletions
19
integration-tests/test-classpath-project-resources/verify.main.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |