Java tutorial
/** Copyright (C) 2016-Present Pivotal Software, Inc. All rights reserved. This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License?); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package io.pivotal.ecosystem.service; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.web.context.WebApplicationContext; import java.io.IOException; import java.nio.charset.Charset; import static junit.framework.TestCase.assertNotNull; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; 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; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { Application.class }) @WebAppConfiguration public class HelloMVCTest { private static final String USER = "foo"; private static final String PW = "bar"; private static final User.Role ROLE = User.Role.User; private MockMvc mockMvc; private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); @Autowired private WebApplicationContext webApplicationContext; @Before public void setup() throws Exception { this.mockMvc = webAppContextSetup(webApplicationContext).build(); } @Test public void testIt() throws Exception { RequestBuilder reqBuilder = MockMvcRequestBuilders.post("/users") .contentType(MediaType.APPLICATION_JSON_UTF8).content(toJson(new User(USER, PW, ROLE))); MvcResult result = mockMvc.perform(reqBuilder).andExpect(MockMvcResultMatchers.status().isCreated()) .andReturn(); assertNotNull(result); mockMvc.perform(get("/greeting?username=foo")).andExpect(status().isOk()) .andExpect(content().contentType(contentType)); mockMvc.perform(delete("/users/foo")).andExpect(status().isOk()); } @Test public void testUser() throws IOException { User u = new User("hello", "pass", User.Role.Broker); System.out.println(toJson(u)); } private String toJson(Object o) throws IOException { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(o); } }