jo87casi
jo87casi

Reputation: 440

Running simple Tests in Spring Tool Suite fails

I'm doing a Spring Tutorial right now, so i don't have much knowledge about the framework. Anyway i am already failing testing the first program. I'm using Eclipse Spring Boot Tools and i want to run my test class using JUnit Tests or Gradle Test.

Running a JUnit Test it throws:

java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils
    at org.junit.platform.launcher.core.LauncherFactory.loadAndFilterTestExecutionListeners(LauncherFactory.java:122)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:108)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:75)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:34)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
    at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:350)
    at java.base/java.lang.Class.newInstance(Class.java:645)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:371)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:366)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:310)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:225)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.util.ClassNamePatternFilterUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 16 more

My gradle configuration looks like:

buildscript {
    ext {
        springBootVersion = '2.3.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 11

repositories {
    mavenCentral()
}

dependencies {
    implementation( 
            'org.springframework.boot:spring-boot-starter',
            'javax.xml.bind:jaxb-api:2.3.0'
    )
    testImplementation(
            'org.springframework.boot:spring-boot-starter-test'
    )
}

The ServiceTest.java

package de.evaspringbuch.eva02hellogibberV1;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import de.evaspringbuch.eva02hellogibberV1.boundary.HelloController;

@SpringBootTest //(classes = Eva02HelloGibberV1.class)
public class ServiceTest {

    @Autowired
    private HelloController helloController;

    @Test
    public void testSayHello() {
        //assertThat(helloController.helloMessage("hello")).isEqualTo("hello");
        assertThat(helloController.helloMessage("hello")).isEqualTo(" na sowas :: hello");
    }
}

The HelloController.java:

package de.evaspringbuch.eva02hellogibberV1.boundary;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.evaspringbuch.eva02hellogibberV1.service.HelloService;

@Component
public class HelloController {

    @Autowired
    private HelloService helloService;

    public String helloMessage(String message) {
        return helloService.setMyMessage(" na sowas :: " + message);
    }
}

Eva02HelloGibberV1.jaba

package de.evaspringbuch.eva02hellogibberV1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import de.evaspringbuch.eva02hellogibberV1.boundary.HelloController;

@SpringBootApplication
public class Eva02HelloGibberV1 {

    private static final Logger LOGGER = LoggerFactory.getLogger(Eva02HelloGibberV1.class);
    @Autowired
    private HelloController helloController;

    public static void main(String[] args) {
        SpringApplication.run(Eva02HelloGibberV1.class);
    }

  @Bean
  CommandLineRunner init() {
      return (evt) -> {
          LOGGER.debug(helloController.helloMessage("hallo"));
          LOGGER.debug(helloController.helloMessage("nochmal hallo"));
      };
  }
}

Upvotes: 1

Views: 674

Answers (1)

Mario Varchmin
Mario Varchmin

Reputation: 3782

Judging from the exception and your setup, you may have to add this dependency, which is not part of spring-boot-starter-test:

testRuntimeOnly 'org.junit.platform:junit-platform-commons:1.7.1'

Upvotes: 1

Related Questions