Add a maven library to the test fixture of a plugin

Answered
I want to test a plugin adding several inspections related to commonly used frameworks like Lombok and Hibernate(JPA). In my automated plugin tests I am trying to configure a project in my fixture having these libraries, but I am unable to do that. I already looked at PsiTestUtil.addLibrary, but that only works if I reference a Jar in my %HOME%/.m2/.... When I push to GitHub, my automated test pipeline fails. Is there any way to tell the test project in your fixture to resolve a Maven artifact. I think I understand how complicated this could be, but perhaps someone else had this problem, or perhaps there's a common workaround for the issue.
3 comments
Comment actions Permalink

Thank you. I have in my test

class SomeTestClass : LightJavaCodeInsightFixtureTestCase() {
...
@Test
fun test() {
MavenDependencyUtil.addFromMaven(module.rootManager.modifiableModel, "org.projectlombok:lombok:1.18.22")
....
}

This seems to work (because if the mistype the maven artifact coordinates it fails), but in my tests, my inspection does NOT detect a class from that library being in classpath. My inspection only operates if it detects a certain class from that jar. 

the code looking for that class (working if tested manually with 'run plugin') does:

fun lombokIsPresent(element: PsiElement) =
JavaPsiFacade.getInstance(element.project)
.findClass("lombok.RequiredArgsConstructor", element.resolveScope) != null

Do I have to do anything after calling addFromMaven()  ? eg some reimport of maven project to update the classpath (as we do when working with a real IJ) ?

Thank you !

0
Comment actions Permalink

See also de.plushnikov.intellij.plugin.LombokTestUtil from Lombok plugin as reference https://github.com/JetBrains/intellij-community/blob/master/plugins/lombok/src/test/java/de/plushnikov/intellij/plugin/LombokTestUtil.java

Usually library will be setup in configureModule() of LightProjectDescriptor and not inside test method (this might actually cause weird side effects for following tests).

Additionally check:

1) element.resolveScope might or might not include "library scope"

2) you might be missing required JDK classes, see https://plugins.jetbrains.com/docs/intellij/tests-prerequisites.html#set-the-run-configuration-parameters on how to setup Mock JDK in tests

0

Please sign in to leave a comment.