From 3b8e2bc70e8d700280b2166e2e59d8ff6314f9fb Mon Sep 17 00:00:00 2001 From: hhhello Date: Thu, 31 Oct 2024 16:07:25 +0900 Subject: [PATCH] Improve test code style --- .../api/AuthControllerTest.kt | 10 +-- .../api/UserControllerTest.kt | 63 ++++++++----------- .../graduatingserver/entity/UserEntityTest.kt | 1 - .../src/test/resources/application-test.yml | 2 + 4 files changed, 35 insertions(+), 41 deletions(-) diff --git a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/AuthControllerTest.kt b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/AuthControllerTest.kt index c254127..b81db8b 100644 --- a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/AuthControllerTest.kt +++ b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/AuthControllerTest.kt @@ -12,12 +12,14 @@ import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.MediaType import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post import org.springframework.test.web.servlet.result.MockMvcResultMatchers +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import kotlin.test.assertNotNull import kotlin.test.junit5.JUnit5Asserter.fail @TestAnnotation class AuthControllerTest { - @Autowired lateinit var mvc: MockMvc @@ -37,15 +39,15 @@ class AuthControllerTest { fun `refresh test`( @Autowired userRepository: UserRepository, ) { - val token = TestUtil.token ?: fail("token is null") + val token = assertNotNull(TestUtil.token) mvc.perform( - MockMvcRequestBuilders.post("/auth/refresh") + post("/auth/refresh") .contentType(MediaType.APPLICATION_JSON) .content( RefreshReq( refreshToken = token.refreshToken ).toJson() ) - ).andExpect(MockMvcResultMatchers.status().isOk) + ).andExpect(status().isOk) } } diff --git a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/UserControllerTest.kt b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/UserControllerTest.kt index ab55364..57e3f3c 100644 --- a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/UserControllerTest.kt +++ b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/UserControllerTest.kt @@ -2,11 +2,9 @@ package com.bestswlkh0310.graduating.graduatingserver.api import com.bestswlkh0310.graduating.graduatingserver.TestAnnotation import com.bestswlkh0310.graduating.graduatingserver.api.user.dto.EditUserReq -import com.bestswlkh0310.graduating.graduatingserver.api.user.dto.UserRes import com.bestswlkh0310.graduating.graduatingserver.core.user.UserRepository import com.bestswlkh0310.graduating.graduatingserver.infra.token.JwtClient import com.bestswlkh0310.graduating.graduatingserver.util.TestUtil -import com.bestswlkh0310.graduating.graduatingserver.util.fromJson import com.bestswlkh0310.graduating.graduatingserver.util.toJson import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test @@ -14,20 +12,19 @@ import org.junit.jupiter.api.fail import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.MediaType import org.springframework.test.web.servlet.MockMvc -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders -import org.springframework.test.web.servlet.result.MockMvcResultMatchers +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import java.util.* -import kotlin.test.assertEquals -import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull @TestAnnotation class UserControllerTest { - @Autowired lateinit var mvc: MockMvc companion object { - @BeforeAll @JvmStatic fun beforeAll( @@ -40,31 +37,29 @@ class UserControllerTest { @Test fun `get me test`() { - val token = TestUtil.token ?: fail("token is null") - val user = TestUtil.user ?: fail("user is null") - - val res = mvc.perform( - MockMvcRequestBuilders.get("/user/me") + val token = assertNotNull(TestUtil.token) + val user = assertNotNull(TestUtil.user) + + mvc.perform( + get("/user/me") .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer ${token.accessToken}") - ).andExpect(MockMvcResultMatchers.status().isOk) - .andReturn().response.contentAsString - .fromJson() - - assertEquals(user.email, res.email) - assertEquals(user.nickname, res.nickname) + ) + .andExpect(status().isOk) + .andExpect(jsonPath("email").value(user.email)) + .andExpect(jsonPath("nickname").value(user.nickname)) } - + @Test fun `edit user and get me test`() { - val token = TestUtil.token ?: fail("token is null") - val user = TestUtil.user ?: fail("user is null") - + val token = assertNotNull(TestUtil.token) + val user = assertNotNull(TestUtil.user) + val editNickname = UUID.randomUUID().toString() - + // edit user mvc.perform( - MockMvcRequestBuilders.patch("/user") + patch("/user") .contentType(MediaType.APPLICATION_JSON) .content( EditUserReq( @@ -74,19 +69,15 @@ class UserControllerTest { ).toJson() ) .header("Authorization", "Bearer ${token.accessToken}") - ).andExpect(MockMvcResultMatchers.status().isOk) - + ).andExpect(status().isOk) + // get me - val res = mvc.perform( - MockMvcRequestBuilders.get("/user/me") + mvc.perform( + get("/user/me") .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer ${token.accessToken}") - ).andExpect(MockMvcResultMatchers.status().isOk) - .andReturn().response.contentAsString - .fromJson() - - assertEquals(user.email, res.email) - assertEquals(editNickname, res.nickname) - assertNotEquals(user.nickname, res.nickname) + ).andExpect(status().isOk) + .andExpect(jsonPath("email").value(user.email)) + .andExpect(jsonPath("nickname").value(editNickname)) } } \ No newline at end of file diff --git a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/entity/UserEntityTest.kt b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/entity/UserEntityTest.kt index f0dd55b..19cdaaa 100644 --- a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/entity/UserEntityTest.kt +++ b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/entity/UserEntityTest.kt @@ -5,7 +5,6 @@ import com.bestswlkh0310.graduating.graduatingserver.core.user.UserEntity import com.bestswlkh0310.graduating.graduatingserver.core.user.UserRole import com.bestswlkh0310.graduating.graduatingserver.core.user.UserState import org.junit.jupiter.api.Test -import java.time.LocalDate import kotlin.test.assertEquals import kotlin.test.assertNull diff --git a/Graduating-Server/src/test/resources/application-test.yml b/Graduating-Server/src/test/resources/application-test.yml index e0c8c90..ec49f1c 100644 --- a/Graduating-Server/src/test/resources/application-test.yml +++ b/Graduating-Server/src/test/resources/application-test.yml @@ -35,3 +35,5 @@ oauth2: audience: https://appleid.apple.com private-key: wow grant-type: authorization_code +public-data: + kosaf-service-key: wow \ No newline at end of file