Springboot test fails to autowire TestRestTemplate when using junit5


username

I am using springboot version 2.2.2.RELEASE.

I am trying to add tests using junit5, this is how it is set up in build.gradle:

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'junit', module: 'junit'
}
testImplementation "org.junit.jupiter:junit-jupiter-api:5.5.2"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.2"

Here is my test class:

//@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT, classes = UserServiceApplication.class)
@ActiveProfiles("it")
public class UserControllerIntegrationTest {
    @Autowired
    private TestRestTemplate testRestTemplate;

}

The problem is that restRestTemplate is null. The only way it works is when using:

@RunWith(SpringRunner.class)

However, as I understand it, this is junit4 support, right?

I am using the deprecated TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate).

I also tried adding the following, but that didn't work either:

@ExtendWith(SpringExtension.class)

What am I missing?

thanks.

Rick Peel

Make sure the entire test is using JUnit 5 related annotations. Maybe your @Testannotations are still using JUnit 4.

The following example works for JUnit 5 and Spring Boot:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit.jupiter.SpringExtension;

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

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = YourApplication.class)
public class TestOne {

  @Autowired
  private TestRestTemplate testRestTemplate;

  @Test
  public void test() {
    assertNotNull(testRestTemplate);
  }
}

Related


Exception when using TestRestTemplate

VíťaPlšek-angular.cz I have normal SpringBoot application with custom authentication filter which works fine. But there is a problem when using TestRestTemplate in integration tests. I want to check here if there is a wrong credit user can't log in. But instea

Exception when using TestRestTemplate

VíťaPlšek-angular.cz I have normal SpringBoot application with custom authentication filter which works fine. But there is a problem when using TestRestTemplate in integration tests. I want to check here if there is a wrong credit user can't log in. But instea

Exception when using TestRestTemplate

VíťaPlšek-angular.cz I have normal SpringBoot application with custom authentication filter which works fine. But there is a problem when using TestRestTemplate in integration tests. I want to check here if there is a wrong credit user can't log in. But instea

Exception when using TestRestTemplate

VíťaPlšek-angular.cz I have normal SpringBoot application with custom authentication filter which works fine. But there is a problem when using TestRestTemplate in integration tests. I want to check here if there is a wrong credit user can't log in. But instea

SpringBoot CRUD repository fails to autowire

Nicolas Gallegos I am using SpringBoot and JPA. I am getting an error that @Autowiredcannot be completed . Here is my main class: package com; @SpringBootApplication @EnableJpaRepositories(basePackages="com.repository") public class InitBatch implements Comma

Contract testing using random ports with jUnit5 and SpringBoot

Silvio When using jUnit5, I can't get the protocol provider tests to run on any other port than 8080. I have the following code: @ExtendWith(SpringExtension.class) @Provider(PROVIDER) @PactFolder("pacts") @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_

How to test PACT for Kafka message consumers using JUnit5

Claus Radloff: In our project we use JUnit5. We want to test our Kafka messages via pact. On the internet I found some JUnit 4 examples and some about JUnit 5 and REST. So far I have: import au.com.dius.pact.consumer.MessagePactBuilder; import au.com.dius.pact

Selecting single test failure using Cucumber JUnit5 engine

Ekalin: Consider this simple project : https://github.com/ekalin/cucumber-junit5-gradle . It contains Cucumber functionality using JUnit5. Following the instructions at https://github.com/cucumber/cucumber-jvm/tree/master/junit-platform-engine , I created an a

How to test EJB beans in OpenEJB using JUnit5?

Lexia RB In JUnit 4, I use the following setup to test my EJB beans: @RunWith(EJBContainerRunner.class) public class MyEETestWithOneOpenEJB { @Inject private ACdiBean bean; @Test public void theTest() { // do test } } But in JUni

How to test PACT for Kafka message consumers using JUnit5

Claus Radloff: In our project we use JUnit5. We want to test our Kafka messages via pact. On the internet I found some JUnit 4 examples and some about JUnit 5 and REST. So far I have: import au.com.dius.pact.consumer.MessagePactBuilder; import au.com.dius.pact

Selecting single test failure using Cucumber JUnit5 engine

Ekalin: Consider this simple project : https://github.com/ekalin/cucumber-junit5-gradle . It contains Cucumber functionality using JUnit5. Following the instructions at https://github.com/cucumber/cucumber-jvm/tree/master/junit-platform-engine , I created an a

Kotlin JUnit5 test using Bazel target not found

Hector I have a Bazel project in Kotlin and I'm trying to run unit tests with JUnit 5. Here is my goal: kt_jvm_library( name = "lib", srcs = glob(["src/main/kotlin/**/*.kt"]), ) kt_jvm_test( name = "tests", main_class = "org.junit.platform.con

How to test PACT for Kafka message consumers using JUnit5

Claus Radloff: In our project we use JUnit5. We want to test our Kafka messages via pact. On the internet I found some JUnit 4 examples and some about JUnit 5 and REST. So far I have: import au.com.dius.pact.consumer.MessagePactBuilder; import au.com.dius.pact

Selecting single test failure using Cucumber JUnit5 engine

Ekalin: Consider this simple project : https://github.com/ekalin/cucumber-junit5-gradle . It contains Cucumber functionality using JUnit5. Following the instructions at https://github.com/cucumber/cucumber-jvm/tree/master/junit-platform-engine , I created an a

How to test PACT for Kafka message consumers using JUnit5

Claus Radloff: In our project we use JUnit5. We want to test our Kafka messages via pact. On the internet I found some JUnit 4 examples and some about JUnit 5 and REST. So far I have: import au.com.dius.pact.consumer.MessagePactBuilder; import au.com.dius.pact

Kotlin JUnit5 test using Bazel target not found

Hector I have a Bazel project in Kotlin and I'm trying to run unit tests with JUnit 5. Here is my goal: kt_jvm_library( name = "lib", srcs = glob(["src/main/kotlin/**/*.kt"]), ) kt_jvm_test( name = "tests", main_class = "org.junit.platform.con

How to test PACT for Kafka message consumers using JUnit5

Claus Radloff: In our project we use JUnit5. We want to test our Kafka messages via pact. On the internet I found some JUnit 4 examples and some about JUnit 5 and REST. So far I have: import au.com.dius.pact.consumer.MessagePactBuilder; import au.com.dius.pact

Selecting single test failure using Cucumber JUnit5 engine

Ekalin: Consider this simple project : https://github.com/ekalin/cucumber-junit5-gradle . It contains Cucumber functionality using JUnit5. Following the instructions at https://github.com/cucumber/cucumber-jvm/tree/master/junit-platform-engine , I created an a

Kotlin JUnit5 test using Bazel target not found

Hector I have a Bazel project in Kotlin and I'm trying to run unit tests with JUnit 5. Here is my goal: kt_jvm_library( name = "lib", srcs = glob(["src/main/kotlin/**/*.kt"]), ) kt_jvm_test( name = "tests", main_class = "org.junit.platform.con