Reputation: 430
I'm following the spring guide to create a hello world soap ws. The link below :
https://spring.io/guides/gs/producing-web-service/
I successfully make it work. When i run this command line :
curl --header "content-type: text/xml" -d @src/test/resources/request.xml http://localhost:8080/ws/coutries.wsdl
I get this response.
<SOAP-ENV:Header/><SOAP-ENV:Body><ns2:getCountryResponse xmlns:ns2="http://spring.io/guides/gs-producing-web-service"><ns2:country><ns2:name>Spain</ns2:name><ns2:population>46704314</ns2:population><ns2:capital>Madrid</ns2:capital><ns2:currency>EUR</ns2:currency></ns2:country></ns2:getCountryResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
Now i'm trying to create a junit test for this service (the controller layer) but it doesn't work.
Here is my unit test :
@RunWith(SpringRunner.class)
@WebMvcTest(CountryEndpoint.class)
@ContextConfiguration(classes = {CountryRepository.class, WebServiceConfig.class})
public class CountryEndpointTest {
private final String URI = "http://localhost:8080/ws/countries.wsdl";
@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception {
mockMvc.perform(
get(URI)
.accept(MediaType.TEXT_XML)
.contentType(MediaType.TEXT_XML)
.content(request)
)
.andDo(print())
.andExpect(status().isOk());
}
static String request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
" xmlns:gs=\"http://spring.io/guides/gs-producing-web-service\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <gs:getCountryRequest>\n" +
" <gs:name>Spain</gs:name>\n" +
" </gs:getCountryRequest>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
}
here's the error :
MockHttpServletResponse:
Status = 404
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :404
I changed the log level to debug and i found this :
2020-01-27 18:04:11.880 INFO 32723 --- [ main] c.s.t.e.s.endpoint.CountryEndpointTest : Started CountryEndpointTest in 1.295 seconds (JVM running for 1.686)
2020-01-27 18:04:11.925 DEBUG 32723 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /ws/countries.wsdl
2020-01-27 18:04:11.929 DEBUG 32723 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/ws/countries.wsdl]
2020-01-27 18:04:11.930 DEBUG 32723 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/ws/countries.wsdl] are [/**]
2020-01-27 18:04:11.930 DEBUG 32723 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/ws/countries.wsdl] are {}
2020-01-27 18:04:11.931 DEBUG 32723 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/ws/countries.wsdl] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@c7a977f]]] and 1 interceptor
I tried another solution (below) but it doesn't work either.
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {WebServiceConfig.class, CountryRepository.class})
public class CountryEndpointTest {
private final String URI = "http://localhost:8080/ws/countries.wsdl";
private MockMvc mockMvc;
@Autowired
CountryRepository countryRepository;
@Before
public void setup() {
this.mockMvc = standaloneSetup(new CountryEndpoint(countryRepository)).build();
}
Upvotes: 3
Views: 20920
Reputation: 131
Spring doc says : https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/html/boot-features-testing.html
By default, @SpringBootTest will not start a server.
You need to define
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
to RUN the server.
I tried with mockserver, but I can't access to the endpoint (even with WebEnvironment.DEFINED_PORT)
So I did it as follow :
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class FacturationEndpointTest {
@Autowired
private WebTestClient webClient;
@Test
public void testWSDL() throws Exception {
this.webClient.get().uri("/test_service/services.wsdl")
.exchange().expectStatus().isOk();
}
You need to add the following dependency in your pom.xml if you want to use WebTestClient like me :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
Upvotes: 5
Reputation: 11
if you are using a spring ws framework to implements your endpoints, please see spring-ws-test. you will find a MockWebServiceClient that mocks a client and tests your endpoint. I propose you to see this example : https://memorynotfound.com/spring-ws-server-side-integration-testing/
this works only for spring web service and not for CXF web services.
Upvotes: 1
Reputation: 902
Please change the GET
method to POST
.
mockMvc.perform(
postURI) // <-- This line!!!
.accept(MediaType.TEXT_XML)
.contentType(MediaType.TEXT_XML)
.content(request)
Upvotes: 0