在 @SpringBootTest 中自动装配时,WebTestClient 为 null
WebTestClient is null when autowired in a @SpringBootTest
我的 Spring 启动应用程序在 2.4.2 版本上运行。我正在尝试为用户注册设置集成测试。此测试 class 从定义了一些静态设置的 AbstractIntegrationTest
扩展而来。
开始测试时,webTestClient
为null
。我尝试添加 @AutoConfigureWebTestClient
但问题仍然存在。
你知道这里可能遗漏了什么吗?
RegistrationIT.java
public class RegistrationIT extends AbstractIntegrationTest {
@Autowired
private WebTestClient webTestClient;
@Test
public void shouldRegisterNewUser() throws JSONException {
String requestBody; // some JSON
webTestClient
.post()
.uri("/api/user")
.body(BodyInserters.fromValue(requestBody))
.exchange()
.expectStatus().isOk();
}
}
AbstractIntegrationTest.java
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("integration-test")
@Testcontainers
public abstract class AbstractIntegrationTest {
private static final String MARIADB_IMAGE_NAME = "mariadb:10.4.16";
private static final String MARIADB_DATABASE_NAME = "test_db";
private static final String MARIADB_USERNAME = "test_user";
private static final String MARIADB_PASSWORD = "test_pass";
static final MariaDBContainer mariaDBContainer = (MariaDBContainer) new MariaDBContainer(MARIADB_IMAGE_NAME)
.withDatabaseName(MARIADB_DATABASE_NAME)
.withUsername(MARIADB_USERNAME)
.withPassword(MARIADB_PASSWORD)
.withReuse(true);
static {
mariaDBContainer.start();
}
@DynamicPropertySource
static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", mariaDBContainer::getJdbcUrl);
registry.add("spring.datasource.username", mariaDBContainer::getUsername);
registry.add("spring.datasource.password", mariaDBContainer::getPassword);
}
}
调试后,我发现我使用了 JUnit 4 中的 @Test
注解,而不是 JUnit 5。更正 import
语句后,测试运行成功。
我的 Spring 启动应用程序在 2.4.2 版本上运行。我正在尝试为用户注册设置集成测试。此测试 class 从定义了一些静态设置的 AbstractIntegrationTest
扩展而来。
开始测试时,webTestClient
为null
。我尝试添加 @AutoConfigureWebTestClient
但问题仍然存在。
你知道这里可能遗漏了什么吗?
RegistrationIT.java
public class RegistrationIT extends AbstractIntegrationTest {
@Autowired
private WebTestClient webTestClient;
@Test
public void shouldRegisterNewUser() throws JSONException {
String requestBody; // some JSON
webTestClient
.post()
.uri("/api/user")
.body(BodyInserters.fromValue(requestBody))
.exchange()
.expectStatus().isOk();
}
}
AbstractIntegrationTest.java
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("integration-test")
@Testcontainers
public abstract class AbstractIntegrationTest {
private static final String MARIADB_IMAGE_NAME = "mariadb:10.4.16";
private static final String MARIADB_DATABASE_NAME = "test_db";
private static final String MARIADB_USERNAME = "test_user";
private static final String MARIADB_PASSWORD = "test_pass";
static final MariaDBContainer mariaDBContainer = (MariaDBContainer) new MariaDBContainer(MARIADB_IMAGE_NAME)
.withDatabaseName(MARIADB_DATABASE_NAME)
.withUsername(MARIADB_USERNAME)
.withPassword(MARIADB_PASSWORD)
.withReuse(true);
static {
mariaDBContainer.start();
}
@DynamicPropertySource
static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", mariaDBContainer::getJdbcUrl);
registry.add("spring.datasource.username", mariaDBContainer::getUsername);
registry.add("spring.datasource.password", mariaDBContainer::getPassword);
}
}
调试后,我发现我使用了 JUnit 4 中的 @Test
注解,而不是 JUnit 5。更正 import
语句后,测试运行成功。