From db487a20cfafc3aacfd5d916e8372646e47e1b25 Mon Sep 17 00:00:00 2001
From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de>
Date: Mon, 19 May 2014 12:49:48 +0200
Subject: [PATCH] Rewritten StatisticsControllerTest

---
 .../controller/StatisticsControllerTest.java  | 62 ++++++++-----------
 1 file changed, 26 insertions(+), 36 deletions(-)

diff --git a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java
index 63fcbcd0b..bb05926f6 100644
--- a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java
+++ b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java
@@ -1,22 +1,22 @@
 package de.thm.arsnova.controller;
 
-import static org.junit.Assert.assertEquals;
-
-import javax.inject.Inject;
+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 org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.ApplicationContext;
-import org.springframework.mock.web.MockHttpServletRequest;
-import org.springframework.mock.web.MockHttpServletResponse;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.web.servlet.HandlerAdapter;
-import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
 
 @RunWith(SpringJUnit4ClassRunner.class)
+@WebAppConfiguration
 @ContextConfiguration(locations = {
 		"file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml",
 		"file:src/main/webapp/WEB-INF/spring/spring-main.xml",
@@ -24,47 +24,37 @@ import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAda
 })
 public class StatisticsControllerTest {
 
-	@Inject
-	private ApplicationContext applicationContext;
-	private MockHttpServletRequest request;
-	private MockHttpServletResponse response;
-	private HandlerAdapter handlerAdapter;
-
 	@Autowired
 	private StatisticsController statisticsController;
 
+	private MockMvc mockMvc;
+
+	@Autowired
+	private WebApplicationContext webApplicationContext;
+
 	@Before
-	public final void setUp() {
-		this.request = new MockHttpServletRequest();
-		this.response = new MockHttpServletResponse();
-		handlerAdapter = applicationContext.getBean(AnnotationMethodHandlerAdapter.class);
+	public void setup() {
+		mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
 	}
 
 	@Test
 	public final void testShouldGetCurrentOnlineUsers() throws Exception {
-		request.setMethod("GET");
-		request.setRequestURI("/statistics/activeusercount");
-		handlerAdapter.handle(request, response, statisticsController);
-
-		assertEquals("0", response.getContentAsString());
+		mockMvc.perform(get("/statistics/activeusercount"))
+		.andExpect(status().isOk())
+		.andExpect(content().string("0"));
 	}
-	
+
 	@Test
 	public final void testShouldGetSessionCount() throws Exception {
-		request.setMethod("GET");
-		request.setRequestURI("/statistics/sessioncount");
-		handlerAdapter.handle(request, response, statisticsController);
-
-		assertEquals("3", response.getContentAsString());
+		mockMvc.perform(get("/statistics/sessioncount"))
+		.andExpect(status().isOk())
+		.andExpect(content().string("3"));
 	}
-	
+
 	@Test
 	public final void testShouldGetStatistics() throws Exception {
-		request.setMethod("GET");
-		request.setRequestURI("/statistics/");
-		handlerAdapter.handle(request, response, statisticsController);
-		
-		String expected = "{\"answers\":0,\"questions\":0,\"openSessions\":3,\"closedSessions\":0,\"activeUsers\":0}";
-		assertEquals(expected, response.getContentAsString());
+		mockMvc.perform(get("/statistics"))
+		.andExpect(status().isOk())
+		.andExpect(content().string("{\"answers\":0,\"questions\":0,\"openSessions\":3,\"closedSessions\":0,\"activeUsers\":0}"));
 	}
 }
-- 
GitLab