HomeControllerTest.java
package edu.global.ex.controller;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@SpringBootTest
@AutoConfigureMockMvc
class HomeControllerTest {
@Autowired
private MockMvc mockMvc;
// GetMapping(/)
@Test
void testHome() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isOk());
}
}
// GetMapping(/)
@Test
void testHome() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
MockHttpServletRequest:
HTTP Method = GET
Request URI = /
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = edu.global.ex.controller.HomeController
Method = edu.global.ex.controller.HomeController#home()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = home
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Language:"en", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
Content type = null
Body =
Forwarded URL = /WEB-INF/views/home.jsp
Redirected URL = null
Cookies = []
// GetMapping(/user/userHome)
@Test
void testUserHome() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/userHome"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
MockHttpServletRequest:
HTTP Method = GET
Request URI = /user/userHome
Parameters = {}
Headers = []
Body = null
Session Attrs = {SPRING_SECURITY_SAVED_REQUEST=DefaultSavedRequest [http://localhost/user/userHome]}
Handler:
Type = null
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 302
Error message = null
Headers = [X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY", Location:"http://localhost/login"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = http://localhost/login
Cookies = []
// GetMapping(/user/userHome)
@Test
@WithMockUser(username = "user", password = "user", roles = "USER")
void testUserHome() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/userHome")).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
MockHttpServletRequest:
HTTP Method = GET
Request URI = /user/userHome
Parameters = {}
Headers = []
Body = null
Session Attrs = {SPRING_SECURITY_CONTEXT=SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=org.springframework.security.core.userdetails.User [Username=user, Password=[PROTECTED], Enabled=true, AccountNonExpired=true, credentialsNonExpired=true, AccountNonLocked=true, Granted Authorities=[ROLE_USER]], Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[ROLE_USER]]]}
Handler:
Type = edu.global.ex.controller.HomeController
Method = edu.global.ex.controller.HomeController#user_home()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = user/userHome
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Language:"en", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
Content type = null
Body =
Forwarded URL = /WEB-INF/views/user/userHome.jsp
Redirected URL = null
Cookies = []
// GetMapping(/admin/adminHome)
@Test
@WithMockUser(username = "admin", password = "admin", roles = "ADMIN")
void testAdminHome() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/admin/adminHome")).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
MockHttpServletRequest:
HTTP Method = GET
Request URI = /admin/adminHome
Parameters = {}
Headers = []
Body = null
Session Attrs = {SPRING_SECURITY_CONTEXT=SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=org.springframework.security.core.userdetails.User [Username=admin, Password=[PROTECTED], Enabled=true, AccountNonExpired=true, credentialsNonExpired=true, AccountNonLocked=true, Granted Authorities=[ROLE_ADMIN]], Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[ROLE_ADMIN]]]}
Handler:
Type = edu.global.ex.controller.HomeController
Method = edu.global.ex.controller.HomeController#admin_home()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = admin/adminHome
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Language:"en", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
Content type = null
Body =
Forwarded URL = /WEB-INF/views/admin/adminHome.jsp
Redirected URL = null
Cookies = []
나머지 내용들은 조금 더 진행한 이후에 진행 예정
package edu.global.ex.db;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@SpringBootTest
class DataSourceTest {
@Autowired
private DataSource datasource;
@Disabled
@Test
void testDataSource() {
System.out.println(datasource);
assertNotNull(datasource);
}
@Test
void testConnetion() throws Exception {
System.out.println("DS : " + datasource);
try (Connection conn = datasource.getConnection()) {
System.out.println("Connection : " + conn);
assertThat(conn).isInstanceOf(Connection.class);
assertEquals(100, getLong(conn, "select 100 from dual"));
// assertTrue(0 < getLong(conn, "select count(*) from User"));
} catch (Exception e) {
e.printStackTrace();
}
}
private long getLong(Connection conn, String sql) {
long result = 0;
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
result = rs.getLong(1);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}