oneturkmen
oneturkmen

Reputation: 1330

Eclipse creates JUnit test in the wrong directory

I have a Gradle-based project in Eclipse which I want to test using JUnit 5. It does not have any tests right now, so I wanted to add a dummy one (e.g., assertEquals(true, true)) to make sure that Gradle can run the test.

However, I experience two problems:

1) When I want to create JUnit test case as "Project Right Click --> New --> JUnit Test Case", it creates a test case inside /src directory. See the image below:

enter image description here

2) When I try to build my Gradle configuration, I get the following output:

> Task :compileJava FAILED
/home/.../src/main/java/TestFoo.java:1: error: package org.junit does not exist
import static org.junit.Assert.*;
                       ^

As far as I understand, the test case cannot resolve the package org.junit. At the same time, Eclipse does not let me create a proper JUnit test case in the proper /test directory.

What do you think could be a possible solution here? I tried finding a way to override default test directory, but could not find any way to do that.

P.S. This is not duplicate as I tried this and I have all dependencies declared. The following snippet is the build.gradle contents (I apologize, but I cannot display all of its contents due to confidentiality reasons):


...

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {

    ...

    testCompile group: 'junit', name: 'junit', version: '4.12'
    // Reference: https://www.baeldung.com/junit-5-gradle
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}

run {
    ...
}

test {
    useJUnitPlatform {
        includeTags 'fast'
        excludeTags 'slow'
    }
}

shadowJar {
    mergeServiceFiles()
    manifest {
        attributes 'Implementation-Title': rootProject.name
        attributes 'Implementation-Version': rootProject.version
        attributes 'Implementation-Vendor-Id': rootProject.group
        attributes 'Created-By': 'Gradle ' + gradle.gradleVersion
        attributes 'Main-Class': mainClassName
    }
    archiveName rootProject.name + '-' + rootProject.version + '.jar'
}

Upvotes: 0

Views: 189

Answers (1)

jirka.pinkas
jirka.pinkas

Reputation: 940

You can either use this: How to create unit tests easily in eclipse (answer with most upvotes)

Or install this plugin: https://moreunit.github.io/MoreUnit-Eclipse/ (and create test using Ctrl + J)

Upvotes: 1

Related Questions