使用 junit5 时,Springboot 测试无法自动装配 TestRestTemplate
Springboot test fails to autowire TestRestTemplate when using junit5
我使用的是springboot 2.2.2.RELEASE.
我正在尝试使用 junit5 添加测试,这是我在 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"
这是我的测试 class:
//@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT, classes = UserServiceApplication.class)
@ActiveProfiles("it")
public class UserControllerIntegrationTest {
@Autowired
private TestRestTemplate testRestTemplate;
}
问题是 restRestTemplate 为空。它工作的唯一方法是当我使用:
@RunWith(SpringRunner.class)
但是,据我了解,这是为了支持 junit4,对吗?
我正在使用未弃用的 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)。
我也尝试添加以下内容,但也没有用:
@ExtendWith(SpringExtension.class)
我错过了什么?
谢谢。
确保您的整个测试都在使用 JUnit 5 相关注释。也许您的 @Test
注释仍在使用 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);
}
}
我使用的是springboot 2.2.2.RELEASE.
我正在尝试使用 junit5 添加测试,这是我在 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"
这是我的测试 class:
//@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT, classes = UserServiceApplication.class)
@ActiveProfiles("it")
public class UserControllerIntegrationTest {
@Autowired
private TestRestTemplate testRestTemplate;
}
问题是 restRestTemplate 为空。它工作的唯一方法是当我使用:
@RunWith(SpringRunner.class)
但是,据我了解,这是为了支持 junit4,对吗?
我正在使用未弃用的 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)。
我也尝试添加以下内容,但也没有用:
@ExtendWith(SpringExtension.class)
我错过了什么?
谢谢。
确保您的整个测试都在使用 JUnit 5 相关注释。也许您的 @Test
注释仍在使用 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);
}
}