Jersy
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.whenever
import kotlinx.coroutines.experimental.runBlocking
import org.glassfish.jersey.server.ResourceConfig
import org.glassfish.jersey.test.JerseyTest
import org.glassfish.jersey.test.TestProperties
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.junit.MockitoJUnitRunner
import java.util.logging.Level
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.QueryParam
import javax.ws.rs.core.Application
import javax.ws.rs.core.MediaType
@RunWith(MockitoJUnitRunner::class)
class ServiceItTest : JerseyTest() {
companion object {
private val endpoint: Endpoint = mock()
private const val endpointResponse = """
${json}
"""
private const val ID = "test_id"
private val testValue = listOf("USD", "EUR", "GBP")
}
public override fun configure(): Application {
set(TestProperties.RECORD_LOG_LEVEL, Level.WARNING.intValue())
return ResourceConfig().register(endpoint)
}
@Test
fun test() {
val conf: ConfigProvider = mock()
whenever(conf.getString(any())).thenReturn("http://localhost:$port")
whenever(endpoint.list(ID)).thenReturn(endpointResponse)
val service =ServiceApi(ApiFactory(conf))
val res = runBlocking { service.getById(ID) }
assertThat(res, equalTo(testValue))
}
}
@Path("/testMehod")
@Produces(MediaType.APPLICATION_JSON)
class Endpoint {
@GET
@Path("")
@Produces(MediaType.APPLICATION_JSON)
fun list(@QueryParam("id") id: String): String {
return id
}
}
testImplementation "org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-jetty:$ver.jersey"