Skip to content

Commit 5a5e499

Browse files
author
Ivan Virchenko
committed
add temporal tests need refactor
1 parent 374e8db commit 5a5e499

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

3-0-spring-framework/3-0-2-view-resolver/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,46 @@
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>3-0-2-view-resolver</artifactId>
13+
<packaging>war</packaging>
1314

15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>11</maven.compiler.source>
18+
<maven.compiler.target>11</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework</groupId>
24+
<artifactId>spring-webmvc</artifactId>
25+
<version>5.3.6</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>javax.servlet</groupId>
30+
<artifactId>javax.servlet-api</artifactId>
31+
<version>4.0.1</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
35+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<version>2.4.5</version>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-war-plugin</artifactId>
51+
<version>3.3.0</version>
52+
</plugin>
53+
</plugins>
54+
</build>
1455
</project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.bobocode.viewresolver;
2+
3+
public class ViewResolverApp {
4+
public static void main(String[] args) {
5+
}
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bobocode.viewresolver.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class DispatcherServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
@Override
7+
protected Class<?>[] getRootConfigClasses() {
8+
return new Class[0];
9+
}
10+
11+
@Override
12+
protected Class<?>[] getServletConfigClasses() {
13+
return new Class[]{ResolverConfig.class};
14+
}
15+
16+
@Override
17+
protected String[] getServletMappings() {
18+
return new String[]{"/"};
19+
}
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.bobocode.viewresolver.config;
2+
3+
public class ResolverConfig {
4+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.bobocode.viewresolver;
2+
3+
import com.bobocode.viewresolver.config.ResolverConfig;
4+
import lombok.SneakyThrows;
5+
import org.junit.jupiter.api.*;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.test.context.ContextConfiguration;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
13+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
14+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
15+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
16+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
17+
18+
import java.lang.reflect.Method;
19+
20+
import static org.junit.jupiter.api.Assertions.*;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
23+
24+
@WebMvcTest
25+
@ContextConfiguration(classes = {ResolverConfig.class})
26+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
27+
public class ViewResolverTest {
28+
@Autowired
29+
private MockMvc mockMvc;
30+
31+
@Test
32+
@Order(1)
33+
@DisplayName("Class is marked as @Configuration")
34+
void classIsMarkedAsConfiguration() {
35+
Configuration configuration = ResolverConfig.class.getAnnotation(Configuration.class);
36+
assertNotNull(configuration);
37+
}
38+
39+
@Test
40+
@Order(2)
41+
@DisplayName("Configuration Class marked as @EnableWebMvc")
42+
void classAnnotatedWithEnableWebMvc() {
43+
EnableWebMvc enableWebMvc = ResolverConfig.class.getAnnotation(EnableWebMvc.class);
44+
assertNotNull(enableWebMvc);
45+
}
46+
47+
@Test
48+
@Order(3)
49+
@DisplayName("Configuration Class marked as @ComponentScan")
50+
void classAnnotatedWithComponentScan() {
51+
ComponentScan componentScan = ResolverConfig.class.getAnnotation(ComponentScan.class);
52+
assertNotNull(componentScan);
53+
}
54+
55+
@Test
56+
@Order(4)
57+
@DisplayName("ComponentScan packages are indicated")
58+
void ComponentScanHasIndicatedPackages() {
59+
ComponentScan componentScan = ResolverConfig.class.getAnnotation(ComponentScan.class);
60+
assertEquals("com.bobocode.resolver", componentScan.basePackages()[0]);
61+
}
62+
63+
@Test
64+
@Order(5)
65+
@DisplayName("Is config class implements WebMvcConfigurer interface")
66+
void isConfigClassImplementsWebMvcConfigurerInterface() {
67+
assertTrue(WebMvcConfigurer.class.isAssignableFrom(ResolverConfig.class));
68+
}
69+
70+
@SneakyThrows
71+
@Test
72+
@Order(6)
73+
@DisplayName("Is config class overrides configureViewResolvers method")
74+
void isConfigurationClassContainsViewResolverMethod() {
75+
Method configureViewResolvers = ResolverConfig.class.getMethod("configureViewResolvers", ViewResolverRegistry.class);
76+
assertNotNull(configureViewResolvers);
77+
}
78+
79+
@Test
80+
@Order(7)
81+
@SneakyThrows
82+
void getRequestReturnOkStatus() {
83+
mockMvc.perform(get("/"))
84+
.andExpect(status().isOk());
85+
}
86+
}

0 commit comments

Comments
 (0)