Skip to content

Instantly share code, notes, and snippets.

@lfmunoz
Created September 27, 2019 15:18
Show Gist options
  • Save lfmunoz/d0f36b36d6dce84225e37fa93a823875 to your computer and use it in GitHub Desktop.
Save lfmunoz/d0f36b36d6dce84225e37fa93a823875 to your computer and use it in GitHub Desktop.
Kotlin Unit Test Template
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.AfterEach
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
/**
* Unit Test - AgentConnection
*/
class AgentConnectionUnitTest {
// unit under test
lateinit var connectHandler: OurConnectHandler
// mocks
val stompServer: StompServer = mockk(relaxed = true)
// dependencies
val testPool = newFixedThreadPoolContext( 4, "testPool")
//________________________________________________________________________________
// setUp/TearDown
//________________________________________________________________________________
@BeforeEach
fun `before each`() {
clearMocks(stompServer, writingFrameHandler, socket, authAsyncTemplate)
every { stompServer.options().supportedVersions } returns listOf("1.2")
}
@AfterEach
fun `after each`() {
}
//________________________________________________________________________________
// Tests
//________________________________________________________________________________
@Test
fun `should disconnect if credentials are missing`() {
runBlocking(testPool) {
assertThat(result).isFalse()
}
verify(exactly = 1) { socket.close()}
}
} // EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment