From bcd9ae221522fa779ea1fd1ff1ab5b561ca0895f Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Thu, 24 Nov 2016 03:35:01 +0700 Subject: simple spring controller's tests --- juick-spring-www/build.gradle | 3 +- .../test/java/com/juick/www/NotFoundPageTest.java | 64 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 juick-spring-www/src/test/java/com/juick/www/NotFoundPageTest.java diff --git a/juick-spring-www/build.gradle b/juick-spring-www/build.gradle index 9ba9bebf..2b3c27f4 100644 --- a/juick-spring-www/build.gradle +++ b/juick-spring-www/build.gradle @@ -35,8 +35,9 @@ dependencies { compile "org.springframework.security:spring-security-web:${springSecurityVersion}" compile "org.springframework.security:spring-security-config:${springSecurityVersion}" - testCompile 'junit:junit:4.12' + testCompile "junit:junit:4.12" testCompile "org.mockito:mockito-core:1.+" + testCompile "org.springframework:spring-test:${springFrameworkVersion}" testRuntime "org.thymeleaf:thymeleaf-testing:3.0.1.RELEASE" diff --git a/juick-spring-www/src/test/java/com/juick/www/NotFoundPageTest.java b/juick-spring-www/src/test/java/com/juick/www/NotFoundPageTest.java new file mode 100644 index 00000000..974cda32 --- /dev/null +++ b/juick-spring-www/src/test/java/com/juick/www/NotFoundPageTest.java @@ -0,0 +1,64 @@ +package com.juick.www; + +import com.juick.configuration.DataConfiguration; +import com.juick.www.configuration.WwwServletConfiguration; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.ContextHierarchy; +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.setup.MockMvcBuilders; +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.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +/** + * Created by aalexeev on 11/24/16. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextHierarchy({ + @ContextConfiguration(classes = { + com.juick.www.configuration.WebAppConfiguration.class, + DataConfiguration.class}), + @ContextConfiguration(classes = {WwwServletConfiguration.class}), +}) +public class NotFoundPageTest { + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + } + + @Test + public void postNotFoundTest() throws Exception { + mockMvc.perform(get("/-11")) + .andExpect(status().isOk()) + .andExpect(view().name("postNotFound")); + + mockMvc.perform(get("/-1")) + .andExpect(status().isOk()) + .andExpect(view().name("postNotFound")); + } + + @Test + public void userNotFoundTest() throws Exception { + mockMvc.perform(get("/-0")) + .andExpect(status().isOk()) + .andExpect(view().name("userNotFound")); + + mockMvc.perform(get("/00000000000000000000000000")) + .andExpect(status().isOk()) + .andExpect(view().name("userNotFound")); + } +} -- cgit v1.2.3