Я хочу использовать WebTestClient в своих тестах. работает так:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class ControllerTest {
@Autowired
private WebTestClient webTestClient;
@Test
public void webtestClient () {
assertNotNull(webTestClient);
}
}
Но теперь я хочу внедрить WebTestClient во вспомогательный класс:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class ControllerTest {
@Autowired
private Helper helper;
@Test
public void webtestClient () {
helper.check();
}
}
@Component
public class Helper {
@Autowired
private WebTestClient webTestClient;
public void check() {
assertNotNull(webTestClient);
}
}
Тоже работает. Но Intellij показывает ошибку:
Could not autowire. No beans of 'WebTestClient' type found. more... (Strg+F1)
Новая информация: тест отлично работает в Intellij, но не при работе с maven.
Вот тестовый проект с проблемой: https://github.com/kicktipp/демо
Как я могу использовать WebTestClient в своем вспомогательном классе?
Спасибо за ваш комментарий. Я создал здесь задачу: youtrack.jetbrains.com/issue/IDEA-205789




Вы должны собрать webTestClient, прежде чем использовать его в тестах
Используйте ниже, это будет работать
@Autowired
ApplicationContext context;
@Autowired
WebTestClient webTestClient;
@Before
public void setup() throws Exception {
this.webTestClient = WebTestClient.bindToApplicationContext(this.context).build();
}
Для чего это стоит - я смог исправить это, просто явно указав аннотацию AutoConfigureWebTestClient:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class MyTestClass {
}
Похоже на ошибку. Сообщите о проблеме на YouTrack: youtrack.jetbrains.com/issues/ИДЕЯ