Section 9.12.3 of the documentation describes that a test can be started with Live Server.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MockMvcGraphQlTests {
@Autowired
private WebGraphQlTester graphQlTester;
}
But the autoconfiguration of WebGraphQlTesterAutoConfiguration
is then not active and no WebGraphQlTester
is created. I get the error message that the WebGraphQlTester
bean cannot be found. That only works if the annotation @AutoConfigureWebGraphQlTester
is set.
It would be nice if this worked like it did with REST.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MockMvcRestTests {
@Autowired
private TestRestTemplate testRestTemplate;
}
I don't understand why the @Autoconfiguration ..
has to be added there.
However, the documentation may not be correct like this?
Comment From: bclozel
@Thinkenterprise I've moved this issue to the Spring Boot tracker since the auto-configuration now lives in Spring Boot. I've identified the problem and I'll commit a fix soon. In the meantime, adding @AutoConfigureWebGraphQlTester
is a good workaround for this problem.
Comment From: Thinkenterprise
Thanks @bclozel. Just a little hint, but I think you already know that. I think the problem is the dependency MockMvcAutoConfiguration.class
of the auto-configuration. This dependency is not longer needed if the test should be run on a real server not on a mock, right?
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ WebClient.class, WebTestClient.class, WebGraphQlTester.class })
@AutoConfigureAfter({ WebTestClientAutoConfiguration.class, MockMvcAutoConfiguration.class })
public class WebGraphQlTesterAutoConfiguration {
@Bean
@ConditionalOnBean(WebTestClient.class)
@ConditionalOnMissingBean
public WebGraphQlTester webTestClientGraphQlTester(WebTestClient webTestClient, GraphQlProperties properties) {
WebTestClient mutatedWebTestClient = webTestClient.mutate().baseUrl(properties.getPath()).build();
return WebGraphQlTester.create(mutatedWebTestClient);
}
}
Comment From: bclozel
The issue is not related to this auto-configuration, but rather a problem with the way the WebGraphQlTester
bean was contributed to the application context.
In the case of @SpringBootTest
(that triggers the entire application context of your application), we're using a different mechanism, not an auto-configuration class. You can see that in the commit listed above. This should be fixed already in the latest SNAPSHOT.
Comment From: Thinkenterprise
After the changes, the tests only work if instead of a spring-boot-starter-web
a spring-boot-starter-webflux
is defined. This is necessary because the WebGraphQlTester
uses the WebTestClient
. But what if I don't want to use Weblux. The documentation describes that both variants reactive and non reactive are supported, right? A thought or a guess from me .... I think for the non reactive case you can use the TestRestTemplate
an need 2 Implementation of the WebGraphQLTester
??
Comment From: bclozel
Indeed, you'll need spring-webflux
as a test dependency (not necessarily the entire starter). This is called out in the reference documentation and generated for you with start.spring.io.
But we should consider whether we want to make that dependency more explicit. We'll discuss that as a team.
Comment From: Thinkenterprise
I followed the documentation and added the dependencies.
<dependency>
<groupId>org.springframework.graphql</groupId>
<artifactId>spring-graphql-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<scope>test</scope>
</dependency>
However, the same mistake comes up. It only works when I add the webflux starter instead of spring webflux
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
I have made my project available here. Maybe I'm doing something wrong and just don't see it, sorry!!
Comment From: bclozel
You're right, while spring-webflux
is enough for WebEnvironment.MOCK
, we need an actual HTTP client for WebEnvironment.RANDOM_PORT
tests. I'm reopening this issue to amend the documentation.