Skip to content

Commit

Permalink
Improve test code style
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Oct 31, 2024
1 parent f8d72e5 commit 3b8e2bc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ 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
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(
Expand All @@ -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<UserRes>()

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(
Expand All @@ -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<UserRes>()

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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Graduating-Server/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ oauth2:
audience: https://appleid.apple.com
private-key: wow
grant-type: authorization_code
public-data:
kosaf-service-key: wow

0 comments on commit 3b8e2bc

Please sign in to comment.