Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions 3-0-spring-framework/3-0-2-view-resolver/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>3-0-spring-framework</artifactId>
<groupId>com.bobocode</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>3-0-2-view-resolver</artifactId>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.6</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.5</version>
<scope>test</scope>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bobocode;

public class ViewResolverApp {
public static void main(String[] args) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.bobocode.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class DispatcherServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{ResolverConfig.class};
}

@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.bobocode.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.bobocode")
public class ResolverConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bobocode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping
public class HelloJspController {
@GetMapping
public String hello() {
return "hello";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.bobocode;

import com.bobocode.config.ResolverConfig;
import com.bobocode.controller.HelloJspController;
import lombok.SneakyThrows;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;
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;

@WebMvcTest
@ContextConfiguration(classes = ResolverConfig.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class HelloJspControllerTest {

@Autowired
private MockMvc mockMvc;
private final HelloJspController helloJspController = new HelloJspController();

@Test
@Order(1)
@DisplayName("Class marked as @Controller")
void helloJspControllerClassMarkedAsController() {
Controller controller = HelloJspController.class.getAnnotation(Controller.class);
assertNotNull(controller);
}

@Test
@Order(2)
@DisplayName("Class marked as @RequestMapping")
void helloJspControllerClassMarkedAsRequestMapping() {
RequestMapping requestMapping = HelloJspController.class.getAnnotation(RequestMapping.class);
assertNotNull(requestMapping);
}

@Test
@Order(3)
@DisplayName("Method that get hello page is marker with @GetMapping")
void helloJspControllerHasMethodAnnotatedAsGetMapping() {
boolean hasAnnotation = Arrays.stream(HelloJspController.class.getMethods())
.anyMatch(method ->
method.getAnnotation(GetMapping.class) != null
);
assertTrue(hasAnnotation);
}

@Test
@Order(4)
@SneakyThrows
@DisplayName("Get method returns view name \"hello\"")
void helloJspControllerMethodReturnsHelloViewName() {
Method method = Arrays.stream(HelloJspController.class.getMethods())
.filter(m -> m.getAnnotation(GetMapping.class) != null)
.findFirst()
.orElseThrow();

var viewName = method.invoke(helloJspController);

assertEquals("hello", viewName);
}

@Test
@Order(5)
@SneakyThrows
@DisplayName("GET method is completed ✅")
void getRequestShouldReturnStatusOkAndCorrectData() {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("hello"))
.andExpect(MockMvcResultMatchers.forwardedUrl("/WEB-INF/views/hello.jsp"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.bobocode;

import com.bobocode.config.ResolverConfig;
import lombok.SneakyThrows;
import org.junit.jupiter.api.*;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.*;
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;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ViewResolverTest {

private final ResolverConfig config = new ResolverConfig();
private final ViewResolverRegistry resolverRegistry = Mockito.mock(ViewResolverRegistry.class);

@Test
@Order(1)
@DisplayName("Class is marked as @Configuration")
void classIsMarkedAsConfiguration() {
Configuration configuration = ResolverConfig.class.getAnnotation(Configuration.class);
assertNotNull(configuration);
}

@Test
@Order(2)
@DisplayName("Configuration Class has annotation @EnableWebMvc")
void classAnnotatedWithEnableWebMvc() {
EnableWebMvc enableWebMvc = ResolverConfig.class.getAnnotation(EnableWebMvc.class);
assertNotNull(enableWebMvc);
}

@Test
@Order(3)
@DisplayName("Configuration Class has annotation @ComponentScan")
void classAnnotatedWithComponentScan() {
ComponentScan componentScan = ResolverConfig.class.getAnnotation(ComponentScan.class);
assertNotNull(componentScan);
}

@Test
@Order(4)
@DisplayName("ComponentScan packages are indicated")
void ComponentScanHasIndicatedPackages() {
ComponentScan componentScan = ResolverConfig.class.getAnnotation(ComponentScan.class);
assertEquals("com.bobocode", componentScan.basePackages()[0]);
}

@Test
@Order(5)
@DisplayName("Config class implements WebMvcConfigurer interface")
void isConfigClassImplementsWebMvcConfigurerInterface() {
assertTrue(WebMvcConfigurer.class.isAssignableFrom(ResolverConfig.class));
}

@SneakyThrows
@Test
@Order(6)
@DisplayName("Config class overrides configureViewResolvers method")
void isConfigurationClassContainsViewResolverMethod() {
Method configureViewResolvers = ResolverConfig.class.getMethod("configureViewResolvers", ViewResolverRegistry.class);
assertNotNull(configureViewResolvers);
}

@Test
@Order(7)
@SneakyThrows
@DisplayName("View resolver registry has correct prefix and suffix")
void viewResolverRegistryHasCorrectPrefixAndSuffix() {
config.getClass().getMethod("configureViewResolvers", ViewResolverRegistry.class).invoke(config, resolverRegistry);
Mockito.verify(resolverRegistry).jsp("/WEB-INF/views/", ".jsp");
}
}
1 change: 1 addition & 0 deletions 3-0-spring-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<module>3-0-0-hello-spring-framework</module>
<module>3-0-1-hello-spring-mvc</module>
<module>3-1-1-dispatcher-servlet-initializer</module>
<module>3-0-2-view-resolver</module>
<module>3-2-1-account-rest-api</module>
</modules>

Expand Down