Skip to content

Commit fbb2213

Browse files
authored
fix(be): mock redis and object mapper for controller tests (#116)
* fix(be):edit id type * fix(be): mock redis and object mapper for controller tests
1 parent de4cb70 commit fbb2213

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/main/kotlin/com/back/koreaTravelGuide/domain/userChat/chatmessage/entity/ChatMessage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import java.time.ZonedDateTime
1717
)
1818
data class ChatMessage(
1919
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
20-
val id: Long? = null,
20+
var id: Long? = null,
2121
@Column(name = "room_id", nullable = false)
2222
val roomId: Long,
2323
@Column(name = "sender_id", nullable = false)

src/main/kotlin/com/back/koreaTravelGuide/domain/userChat/chatroom/entity/ChatRoom.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import java.time.ZonedDateTime
1717
)
1818
data class ChatRoom(
1919
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
20-
val id: Long? = null,
20+
var id: Long? = null,
2121
@Column(nullable = false)
2222
val title: String,
2323
@Column(name = "guide_id", nullable = false)
Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
package com.back.koreaTravelGuide.config
22

3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
5+
import com.fasterxml.jackson.module.kotlin.KotlinModule
36
import org.mockito.Mockito
7+
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
48
import org.springframework.boot.test.context.TestConfiguration
59
import org.springframework.context.annotation.Bean
610
import org.springframework.context.annotation.Primary
711
import org.springframework.data.redis.connection.RedisConnectionFactory
12+
import org.springframework.data.redis.core.RedisTemplate
13+
import org.springframework.data.redis.core.ValueOperations
814

915
@TestConfiguration
1016
class TestConfig {
1117
@Bean
1218
@Primary
13-
fun redisConnectionFactory(): RedisConnectionFactory = Mockito.mock(RedisConnectionFactory::class.java)
19+
fun testRedisConnectionFactory(): RedisConnectionFactory = Mockito.mock(RedisConnectionFactory::class.java)
20+
21+
@Bean
22+
@Primary
23+
fun testRedisTemplate(): RedisTemplate<String, String> {
24+
@Suppress("UNCHECKED_CAST")
25+
val template = Mockito.mock(RedisTemplate::class.java) as RedisTemplate<String, String>
26+
27+
@Suppress("UNCHECKED_CAST")
28+
val valueOps = Mockito.mock(ValueOperations::class.java) as ValueOperations<String, String>
29+
30+
Mockito.`when`(template.opsForValue()).thenReturn(valueOps)
31+
Mockito.`when`(valueOps.get(Mockito.anyString())).thenReturn(null)
32+
33+
return template
34+
}
35+
36+
@Bean
37+
fun javaTimeModuleCustomizer(): Jackson2ObjectMapperBuilderCustomizer {
38+
return Jackson2ObjectMapperBuilderCustomizer { builder ->
39+
builder.modulesToInstall(JavaTimeModule())
40+
}
41+
}
42+
43+
@Bean
44+
@Primary
45+
fun testObjectMapper(): ObjectMapper {
46+
return ObjectMapper().apply {
47+
registerModule(JavaTimeModule())
48+
registerModule(KotlinModule.Builder().build())
49+
}
50+
}
1451
}

0 commit comments

Comments
 (0)