Created
January 3, 2020 09:03
-
-
Save kristiannissen/ecd14d9c5d9bb900085c3d40978661d9 to your computer and use it in GitHub Desktop.
grocerymonkey test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package mobi.grocerymonkey.groceryapp; | |
| import static com.google.common.truth.Truth.assertThat; | |
| import static org.mockito.Mockito.when; | |
| import static org.mockito.Mockito.mock; | |
| import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | |
| import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; | |
| import com.google.cloud.datastore.DatastoreOptions; | |
| import com.google.cloud.datastore.DatastoreOptions; | |
| import com.googlecode.objectify.ObjectifyFactory; | |
| import com.googlecode.objectify.ObjectifyService; | |
| import com.googlecode.objectify.Objectify; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.junit.runners.JUnit4; | |
| import org.mockito.Mock; | |
| import org.mockito.MockitoAnnotations; | |
| import org.json.JSONObject; | |
| import java.io.PrintWriter; | |
| import java.io.StringWriter; | |
| import java.io.BufferedReader; | |
| import java.io.StringReader; | |
| import java.io.Reader; | |
| import java.io.Closeable; | |
| import java.io.IOException; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import javax.servlet.ServletContext; | |
| import javax.servlet.ServletContextListener; | |
| import javax.servlet.ServletContextEvent; | |
| import static mobi.grocerymonkey.groceryapp.service.OfyService.ofy; | |
| /** | |
| * | |
| */ | |
| @RunWith(JUnit4.class) | |
| public class GroceryServletTest { | |
| private static final String MOCK_URL = "/grocery"; | |
| // Set up a helper so that the ApiProxy returns a valid environment for local testing. | |
| private final LocalServiceTestHelper helper = | |
| new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig() | |
| .setDefaultHighRepJobPolicyUnappliedJobPercentage(100)); | |
| private Closeable closeable; | |
| @Mock | |
| private HttpServletRequest mockRequest; | |
| @Mock | |
| private HttpServletResponse mockResponse; | |
| private ServletContextListener contextListener; | |
| private ServletContext context; | |
| private StringWriter responseWriter; | |
| private GroceryServlet servletUnderTest; | |
| @Before | |
| public void setUp() throws Exception { | |
| MockitoAnnotations.initMocks(this); | |
| helper.setUp(); | |
| ObjectifyService.init(); | |
| // contextListener = new GroceryContextListener(); | |
| // context = mock(ServletContext.class); | |
| // Set up some fake HTTP requests | |
| when(mockRequest.getRequestURI()).thenReturn(MOCK_URL); | |
| JSONObject grocery = new JSONObject(); | |
| grocery.put("name", "Beer"); | |
| Reader inputString = new StringReader(grocery.toString()); | |
| BufferedReader reader = new BufferedReader(inputString); | |
| when(mockRequest.getReader()).thenReturn(reader); | |
| // when(mockRequest.getServletContext()).thenReturn(context); | |
| // Set up a fake HTTP response. | |
| responseWriter = new StringWriter(); | |
| when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); | |
| servletUnderTest = new GroceryServlet(); | |
| } | |
| @After | |
| public void tearDown() throws Exception { | |
| helper.tearDown(); | |
| } | |
| @Test | |
| public void doGetWritesResponse() throws Exception { | |
| servletUnderTest.doGet(mockRequest, mockResponse); | |
| // We expect our hello world response. | |
| assertThat(responseWriter.toString()) | |
| .contains("Hello Kitty"); | |
| } | |
| @Test | |
| public void doPostWritesResponse() throws Exception { | |
| // contextListener.contextInitialized(new ServletContextEvent(context)); | |
| JSONObject reqObj = new JSONObject(); | |
| reqObj.put("name", "Beer"); | |
| reqObj.put("quantity", 5); | |
| StringReader reader = new StringReader(reqObj.toString()); | |
| when(mockRequest.getReader()).thenReturn(new BufferedReader(new StringReader(reqObj.toString()))); | |
| servletUnderTest.doPost(mockRequest, mockResponse); | |
| // We expect our hello world response. | |
| assertThat(responseWriter.toString()) | |
| .contains(reqObj.getString("name")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment