k-sera

A Kotlin Wrapper for JMock

License

License

GroupId

GroupId

com.oneeyedmen
ArtifactId

ArtifactId

k-sera
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

k-sera
A Kotlin Wrapper for JMock
Project URL

Project URL

https://github.com/dmcg/k-sera
Source Code Management

Source Code Management

https://github.com/dmcg/k-sera

Download k-sera

How to add to project

<!-- https://jarcasting.com/artifacts/com.oneeyedmen/k-sera/ -->
<dependency>
    <groupId>com.oneeyedmen</groupId>
    <artifactId>k-sera</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.oneeyedmen/k-sera/
implementation 'com.oneeyedmen:k-sera:1.0.0'
// https://jarcasting.com/artifacts/com.oneeyedmen/k-sera/
implementation ("com.oneeyedmen:k-sera:1.0.0")
'com.oneeyedmen:k-sera:jar:1.0.0'
<dependency org="com.oneeyedmen" name="k-sera" rev="1.0.0">
  <artifact name="k-sera" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.oneeyedmen', module='k-sera', version='1.0.0')
)
libraryDependencies += "com.oneeyedmen" % "k-sera" % "1.0.0"
[com.oneeyedmen/k-sera "1.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.0.5
com.natpryce : hamkrest jar 1.2.3.0
org.jmock : jmock jar 2.8.2

test (2)

Group / Artifact Type Version
org.jmock : jmock-junit4 jar 2.8.2
org.jetbrains.kotlin : kotlin-test jar 1.0.5

Project Modules

There are no modules declared in this project.

K-Sera

A JMock wrapper for Kotlin.

KSeraExampleTests shows how to write a test.

class KSeraExampleTests {

    // Create a mockery using the usual JMock rule
    @Rule @JvmField val mockery = JUnitRuleMockery()

    // and a mock from the mockery
    val charSequence = mockery.mock<CharSequence>()

    @Test fun `expect a call to a mock via the mockery`() {
        mockery.expecting {
            oneOf(charSequence).length
        }
        // in this case JMock returns a default value
        assertEquals(0, charSequence.length)
    }

    @Test fun `you can specify a (type-checked) return value for a call`() {
        mockery.expecting {
            oneOf(charSequence).length.which will returnValue(42)
        }
        assertEquals(42, charSequence.length)

        // oneOf(charSequence).length.which will returnValue("banana") doesn't compile
    }

    @Test fun `simulate failures with throwException`() {
        mockery.expecting {
            oneOf(charSequence).length.which will throwException(RuntimeException("deliberate"))
        }
        assertFails {
            charSequence.length
        }
    }

    @Test fun `special operators work well`() {
        mockery.expecting {
            allowing(charSequence)[0].which will returnValue('*')
        }
        assertEquals('*', charSequence.get(0))
    }

    @Test fun `match parameters with Hamkrest`() {
        mockery.expecting {
            allowing(charSequence)[with(lessThan(0))].which will returnValue('-')
            allowing(charSequence)[with(anything)].which will returnValue('+')
        }
        assertEquals('-', charSequence.get(-1))
        assertEquals('+', charSequence.get(99))
    }

    @Test fun `which-will can take a block and access the call invocation`() {
        mockery.expecting {
            allowing(charSequence)[with(anything)].which will { invocation ->
                (invocation.getParameter(0) as Int).toChar()
            }
        }
        assertEquals(' ', charSequence[32])
        assertEquals('A', charSequence[65])
    }

    @Test fun `you can mock functions too`() {
        val f = mockery.mock<(Any?) -> String>()
        mockery.expecting {
            allowing(f).invoke(with(anything)).which will returnValue("banana")
        }
        assertEquals("banana", f("ignored"))
    }

    // Shall we play a game?

    val controlPanel = Panel(
        mockery.mock<Key>("key1"),
        mockery.mock<Key>("key2"),
        mockery.mock<Missile>())


    @Test fun `block syntax for expecting-during-verify`() =
        mockery {
            expecting {
                allowing(controlPanel.key1).isTurned.which will returnValue(true)
                allowing(controlPanel.key2).isTurned.which will returnValue(true)
            }
            during {
                controlPanel.pressButton()
            }
            verify {
                oneOf(controlPanel.missile).launch()
            }
        }

    @Test fun `given-that allows property references`() =
        mockery {
            given {
                that(controlPanel.key1, Key::isTurned).isProperty withValue (true)
                that(controlPanel.key2, Key::isTurned).isProperty withValue (false)
            }
            during {
                controlPanel.pressButton()
            }
            verify {
                never(controlPanel.missile).launch()
            }
        }

}

k-sera is available at Maven central.

Versions

Version
1.0.0