Testing Java code with Groovy in Intellij: Cannot resolve GroovyTestCase class

I need to write a short test for some Java code. I used CTRL + SHIFT + T to generate one with IntelliJ and selected "Groovy JUnit" as the test library, then wrote the following test:

package util class FibonacciHeapTest extends GroovyTestCase { FibonacciHeap<Integer> heap void setUp() { super.setUp() heap = new FibonacciHeap<>() } void testAddInOrder() { testForItems 1..1000 } private void testForItems(Range<Integer> items) { items.each {heap << it} assertEquals heap.size, items.to items.each {assertEquals heap.remove(), it} } } 

However, when I right-click on a test case in the project window, I do not get the Run All Tests option, which I usually do with JUnit tests, and the compiler generates the following error:

 Information:2/4/15 8:15 PM - Compilation completed with 2 errors and 0 warnings in 2 sec /home/patrick/IdeaProjects/hackerrank/src/test/java/util/FibonacciHeapTest.groovy Error:(3, 1) Groovyc: unable to resolve class util.FibonacciHeap Error:(9, 1) Groovyc: unable to resolve class GroovyTestCase 

Attempting to import GroovyTestCase or FibonacciHeap manually causes the same error. IntelliJ does not add any import statements when I let autocomplete end names for me, as is usually the case with Java code.

What am I doing wrong?

+15
java intellij-idea junit groovy
source share
8 answers

This worked for me:

  • Open the Gradle window (on the right side in my case)
  • Click refresh button
  • Done

screenshot

+9
source share

I had a similar problem with creating test classes in IntelliJ, and it was solved when creating a new directory outside the com.company folder (where I had a class that I wanted to test).

  1. Create a new directory for test classes at the same level as your src folder.
  2. Right-click the new test directory and select "Mark Directory As" → "Test Resources Root"
  3. Now create a test class that should automatically be added to your test directory.

enter image description here

+6
source share

Build -> The Rebuild project in the IDE itself (unlike maven, in my case) did this for me.

enter image description here

+2
source share

In my case, what I did to solve the problem was pretty simple.

  • Close IntelliJ
  • Open the attached home page ...
  • Delete your project by clicking on x then ...
  • Click Import Project , build.graddle to your project's build.graddle file and open.

enter image description here

That was all, and all the red highlight disappeared.

+1
source share

First you need to configure the Groovy SDK. See screenshot

enter image description here

A more detailed description in the white paper: Configuring Global, Design, and SDKs

0
source share

When creating a test in the Create Test dialog, you can select the GroovyTestCase test library and click the Fix button:

enter image description here

Here's how to open the test creation dialog: https://www.jetbrains.com/help/idea/2017.3/creating-tests.html

0
source share

As @ sman591 pointed out in a comment, if you get an error:

 groovyc: unable to resolve class groovy.util.GroovyTestCase 

and you already have Groovy as a dependency, then you probably just skip the junit dependency.

0
source share

In IntelliJ IDEA, I re-imported the project. It worked then. I closed the idea. I deleted the .idea folder in the project. And I imported the project.

Then I needed to configure Groovy, look at the previous answers, mark the test directory as a test source in all modules of my project.

0
source share

All Articles