Lucas León
02/15/2022, 5:23 AM@SpringBootApplication
class TodoConsumerApplication {
@Bean
fun httpClient() = HttpClient {
install(JsonFeature)
}
}
fun main(args: Array<String>) {
runApplication<TodoConsumerApplication>(*args)
}
@Service
class TodoConsumerService(
private val httpClient: HttpClient
) {
suspend fun getItemsTwoTimes() {
val result1 = httpClient.get<String>("<http://localhost:8080/items>")
val result2 = httpClient.get<String>("<http://localhost:8080/items>")
println("Result 1 = $result1 and Result 2 = $result2")
}
}
@ExtendWith(SpringExtension::class)
@SpringBootTest
internal class TodoConsumerServiceTest {
@MockkBean
private lateinit var httpClient: HttpClient
@Autowired
private lateinit var todoConsumerService: TodoConsumerService
@Test
fun getItemsTwoTimes() = runBlocking {
}
}
I want to mock two different responses in the service using MockEngineAleksei Tirman [JB]
02/15/2022, 8:00 AMHttpClient
object in TodoConsumerServiceTest
?Mykola Gurov
02/15/2022, 9:41 AMMykola Gurov
02/15/2022, 9:45 AM@MockBean
that you seem to aim for - this creates a new Spring context for every test. Spring contexts aren't very cheap to start, 10 secs to 30 secs is a norm. Might quickly become an issue with more than a handful of tests for your CI duration. Just saying.Aleksei Tirman [JB]
02/15/2022, 9:57 AMHttpClient
object, that will use the MockEngine
, in the TodoConsumerService
for your test, then you could mock responses.Lucas León
02/16/2022, 2:07 AM