Home > Blockchain >  IntelliJ-IDEA Gradle: trouble getting started (following JetBrain's tutorial)
IntelliJ-IDEA Gradle: trouble getting started (following JetBrain's tutorial)

Time:12-15

I'm doing JetBrains' tutorial on Gradle in IntelliJ-IDEA, and, having completed "Getting Started with Gradle Step 3. Run the application with Gradle", I'm stuck at Step 4. The tutorial says "Click ▶ in the left gutter of the editor"; I don't see ▶. Instead, the file (FizzBuzzTest.java) has 11 errors and 16 warnings, starting with "Cannot resolve symbol 'Assert'". The file name has a red wavy underline. So I tried to run the test from Gradle's test task; that too returned an error message: "error: package org.junit does not exist".

Here's the content of my FizzBuzz\src\test\java\com.gradle.tutorial\FizzBuzzTest.java:

package com.gradle.tutorial;
import org.junit.Assert;
import org.junit.Test;

public class FizzBuzzTest {
    @Test
    public void FizzBuzzNormalNumbers() {

        FizzBuzzProcessor fb = new FizzBuzzProcessor();
        Assert.assertEquals("1", fb.convert(1));
        Assert.assertEquals("2", fb.convert(2));
    }

    @Test
    public void FizzBuzzThreeNumbers() {

        FizzBuzzProcessor fb = new FizzBuzzProcessor();
        Assert.assertEquals("Fizz", fb.convert(3));
    }

    @Test
    public void FizzBuzzFiveNumbers() {

        FizzBuzzProcessor fb = new FizzBuzzProcessor();
        Assert.assertEquals("Buzz", fb.convert(5));
    }

    @Test
    public void FizzBuzzThreeAndFiveNumbers() {

        FizzBuzzProcessor fb = new FizzBuzzProcessor();
        Assert.assertEquals("Buzz", fb.convert(5));
    }
}

The only other files I manually created by the tutorial are ...

FizzBuzz\src\main\java\com.gradle.tutorial\FizzBuzzProcessor.java

package com.gradle.tutorial;

public class FizzBuzzProcessor {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i  ) {
            System.out.println(convert(i));
        }
    }

    public static String convert(int fizzBuzz) {
        if (fizzBuzz % 15 == 0) {
            return "FizzBuzz";
        }
        if (fizzBuzz % 3 == 0) {
            return "Fizz";
        }
        if (fizzBuzz % 5 == 0) {
            return "Buzz";
        }
        return String.valueOf(fizzBuzz);
    }
}

and

FizzBuzz\build.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
}

test {
    useJUnitPlatform()
}

These are all as the tutorial have them (I simply copy-pasted them.)

Because FizzBuzzTest.java doesn't have ▶, I cannot run the test to complete Step 4. What should I do?

(When creating FizzBuzzTest.java following the step "Create a test class", the dialogue automatically selected JUnit5 instead of JUnit4 that the tutorial was suggesting. I tried JUnit4 but the dialogue said it could not be found, so I changed it back to JUnit5.)

CodePudding user response:

When you create a project, the build.gradle file is created automatically and contains a junit dependency by default.

You don't need to add anything else. Since the tutorial is based on JUnit4, please consider downgrading JUnit5 to JUnit4 while we update the tutorial.

CodePudding user response:

We have updated the documentation, so that the source of the project test class will use JUnit 5 classes now, since the latest IDE versions uses JUnit 5 version as a dependency by default:

package com.gradle.tutorial;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class FizzBuzzTest {
    @Test
    public void FizzBuzzNormalNumbers() {

        FizzBuzzProcessor fb = new FizzBuzzProcessor();
        Assertions.assertEquals("1", fb.convert(1));
        Assertions.assertEquals("2", fb.convert(2));
    }

    @Test
    public void FizzBuzzThreeNumbers() {

        FizzBuzzProcessor fb = new FizzBuzzProcessor();
        Assertions.assertEquals("Fizz", fb.convert(3));
    }

    @Test
    public void FizzBuzzFiveNumbers() {

        FizzBuzzProcessor fb = new FizzBuzzProcessor();
        Assertions.assertEquals("Buzz", fb.convert(5));
    }

    @Test
    public void FizzBuzzThreeAndFiveNumbers() {

        FizzBuzzProcessor fb = new FizzBuzzProcessor();
        Assertions.assertEquals("Buzz", fb.convert(5));
    }
}
  • Related