Is there a way to unit test plugin action that needs DataContext?

Answered

Hello.

I have developed a plugin File action that extends the JavaCreateTemplateInPackageAction and I'm trying to unit-test the overridden the isAvailable(dataContext: DataContext) function.

The test will verify that the action is available only if the user executes it in the src/test/groovy folder.

Is there a way to create an instance of the DataContext that will emulate the setup where the user has selected the src/test/groovy folder in the Project Tool Window and executed the action?

0
4 comments

You can provide “fake” DataContext via com.intellij.openapi.actionSystem.impl.SimpleDataContext

0

This is what I'm trying to do. Is this way the right way?

val virtualDirectory = tempDirFixture.findOrCreateDir("main/groovy/test/test1/test2")
val psiDirectory = findOrCreateDirectory(virtualDirectory)

val ideView = object : IdeView {
    override fun getDirectories(): Array<PsiDirectory> = arrayOf(psiDirectory)
    override fun getOrChooseDirectory(): PsiDirectory = psiDirectory
}

val context = SimpleDataContext.builder()
    .add(LangDataKeys.PROJECT, project)
    .add(LangDataKeys.MODULE, module)
    .add(LangDataKeys.IDE_VIEW, ideView)
    .build()
0

If it works, then it LGTM

0

OK. I was just curious whether I'm doing it more complex than necessary :)

0

Please sign in to leave a comment.