Closed dhina26 closed 8 years ago
Pasting below source code for reference
import org.mockserver.client.server.MockServerClient; import org.mockserver.integration.ClientAndProxy; import org.mockserver.integration.ClientAndServer; import static org.mockserver.integration.ClientAndProxy.startClientAndProxy; import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; import static org.mockserver.model.HttpForward.forward; import static org.mockserver.model.Header.header; import static org.mockserver.model.HttpResponse.notFoundResponse; import static org.mockserver.matchers.Times.exactly; import static org.mockserver.model.HttpForward.Scheme.HTTP;
import org.springframework.http.MediaType; import org.springframework.core.env.Environment; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.web.context.WebApplicationContext; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import static java.util.concurrent.TimeUnit.SECONDS; import com.google.common.net.HttpHeaders;
public class MockServerMain { private static ClientAndProxy proxy; private static ClientAndServer mockServer; private static WebApplicationContext webApplicationContext; private static MockMvc mockMvc;
public static void startProxy() {
System.out.println("Starting Proxy");
proxy = startClientAndProxy(1090);
}
public static void stopProxy() {
proxy.stop();
}
public static void startMockServer() {
System.out.println("Starting MockSever");
mockServer = startClientAndServer(1080);
proxy.reset();
}
public static void stopMockServer() {
mockServer.stop();
}
public static void main(String[] args) throws Exception {
mockMvc = webAppContextSetup(webApplicationContext).build();
startProxy();
startMockServer();
String resBody = "[" + System.getProperty("line.separator") +
" {" + System.getProperty("line.separator") +
" \"id\": \"1234\"," + System.getProperty("line.separator") +
" \"title\": \"Mock Sever Response \"," + System.getProperty("line.separator") +
" \"username\": \"Sam\"," + System.getProperty("line.separator") +
" \"phone\": \"12345789\"," + System.getProperty("line.separator") +
" \"location\": \"Bangalore\"" + System.getProperty("line.separator") +
" }" + System.getProperty("line.separator") +
"]" ;
try {
System.out.println("---Start of testMockServer block ----");
mockServer
.when(
request()
.withMethod("GET")
.withPath("/UserCredential/rest/users"),
exactly(1)
)
.respond(
response()
.withStatusCode(200)
.withBody(resBody)
);
// when
MvcResult response = mockMvc.perform(get("/UserCredential/rest/users")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json; charset=utf-8"))
.andReturn();
Thread.sleep(60000);
System.out.println("---End of testMockServer block----");
}catch (Exception ie) {
System.out.println(ie);
}finally {
stopProxy();
stopMockServer();
System.out.println("Stopping MockSever & Proxy");
}
}
}
It looks like the issue you are having is related to you use of the Spring Framework with the following code: mockMvc = webAppContextSetup(webApplicationContext).build();
This is not related to MockServer, as a guess this looks to be an issue with the parameter webApplicationContext
not being initialised.
I am going to close this issue as it is not related to MockServer.
Following below steps resolved WebApplicationContext issue
--------------test-servlet-context.xml----------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.ourownjava.spring*" />
<mvc:annotation-driven />
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
While exploring mock server for our automated test infrastructure, we hit into below error while executing code given below
Error: APPLICATION_JSON cannot be resolved or is not a field
MvcResult response = mockMvc.perform(get("/UserCredential/rest/users") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json; charset=utf-8")) .andReturn();
Passing 'MediaType.ALL' for .accept instead of 'MediaType.APPLICATION_JSON' allowed compilation to go through without any errors whereas during runtime i am hitting into below error
Exception in thread "main" java.lang.IllegalArgumentException: WebApplicationContext is required at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder.(DefaultMockMvcBuilder.java:66)
at org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup(MockMvcBuilders.java:46)
at checkMockServerMain.main(checkMockServerMain.java:81)
Any help on this regard is very much appreciated