Skip to content

Instantly share code, notes, and snippets.

@kiuchikeisuke
Created May 15, 2018 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiuchikeisuke/38700e45484214298b6043e16baf80f9 to your computer and use it in GitHub Desktop.
Save kiuchikeisuke/38700e45484214298b6043e16baf80f9 to your computer and use it in GitHub Desktop.
AndroidのテストをSpek+Mockitoで書こう ref: https://qiita.com/k_keisuke/items/815ced486e8cdff8670d
object SampleTest : Spek({
describe("describe1") {
describe(" describe2") {
describe(" describe3") {
on("input x=5,y=7") {
it("should be 12") {
val expected = 12
Assert.Equals(expected,Calc.sum(5,7))
}
}
}
}
}
})
buildscript {
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
}
}
allprojects {
repositories {
maven { url "http://dl.bintray.com/jetbrains/spek" }
}
@RunWith(JUnitPlatform::class)
object ConstraintTest : Spek({
val executionThreads: ExecutionThreads = object : ExecutionThreads {
override fun io(): Scheduler = Schedulers.trampoline()
override fun ui(): Scheduler = Schedulers.trampoline()
}
val prefix: Prefix = Mockito.mock(Prefix::class.java)
val emptyPrefix = EmptyPrefix()
context("PrefixEnable Test") {
given("setting true") {
val settingDataSource: SettingDataSource = Mockito.mock(SettingDataSource::class.java)
Mockito.`when`(settingDataSource.getPrefixEnable()).then { Observable.just(true) }
val prefixEnable = PrefixEnable(settingDataSource, executionThreads)
on("validate") {
val testObs = TestObserver<PrefixEnable.Response>()
prefixEnable.execute(PrefixEnable.Request(prefix)).subscribe(testObs)
it("prefix should be not empty") {
testObs.assertNoErrors()
testObs.assertValue { it.prefix == prefix }
}
}
}
given("setting false") {
val settingDataSource: SettingDataSource = Mockito.mock(SettingDataSource::class.java)
Mockito.`when`(settingDataSource.getPrefixEnable()).then { Observable.just(false) }
val prefixEnable = PrefixEnable(settingDataSource, executionThreads)
on("validate") {
val testObs = TestObserver<PrefixEnable.Response>()
prefixEnable.execute(PrefixEnable.Request(prefix)).subscribe(testObs)
it("prefix should be empty") {
testObs.assertNoErrors()
testObs.assertValue { it.prefix == EmptyPrefix() }
}
}
}
}
}
class SimpleTest : Spek({
describe("a calculator") {
val calculator = SampleCalculator()
it("should return the result of adding the first number to the second number") {
val sum = calculator.sum(2, 4)
assertEquals(6, sum)
}
it("should return the result of subtracting the second number from the first number") {
val subtract = calculator.subtract(4, 2)
assertEquals(2, subtract)
}
}
})
on("decodeToUTF8PhoneNumber: %2B (+)") {
it("should be tel:12344445555+") {
val expected = TelephoneNumber("tel:12344445555+")
assertEquals(expected, TelephoneNumber.decodeToUTF8TelephoneNumber("tel:12344445555%2B"))
}
}
class PhoneNumber(val number: String) {
init {
if (number.startsWith(TelephoneNumber.SCHEME)) {
throw IllegalArgumentException("PhoneNumber does not start with 'tel:'")
} else if (number.isEmpty()) {
throw IllegalArgumentException("number must not be Empty")
} else if ((number.contains(Regex(".*[^0-9#¥+¥*() -]+.*")))) {
throw IllegalArgumentException("contains illegal char. number = $number")
}
}
}
class PrefixEnable @Inject constructor(
private val settingDataSource: SettingDataSource,
executionThreads: ExecutionThreads)
: IoUseCase<PrefixEnable.Request, PrefixEnable.Response, Throwable>(executionThreads) {
override fun execute(requestValue: Request): Observable<Response> {
return settingDataSource.getPrefixEnable().map {
if (it) {
Response(requestValue.prefix)
} else {
Response(Prefix.generateEmptyPrefix())
}
}
}
data class Request(val prefix: Prefix) : UseCase.RequestValue
data class Response(val prefix: Prefix) : UseCase.ResponseValue
}
@RunWith(JUnitPlatform::class)
object SampleTest : Spek({
val calc = Calc()
describe("Calc Test") {
beforeGroup { /* describe内の事前処理 */ }
afterGroup { /* describe内の事後処理 */}
context(" sum method") {
beforeGroup { /* context内の事前処理 */ }
afterGroup { /* context内の事後処理 */}
given(" condition x>0, y>0") {
beforeGroup { /* given内の事前処理 */ }
afterGroup { /* given内の事後処理 */}
beforeEachTest { /* 各々のitの事前処理 */}
afterEachTest { /* 各々のitの事前処理 */ }
on("input x=5,y=7") {
it("should be 12") {
val expected = 12
Assert.Equals(expected,calc.sum(5,7))
}
}
}
}
}
})
data class TelephoneNumber(val number: String) {
companion object {
fun decodeToUTF8TelephoneNumber(number: String) = TelephoneNumber(URLDecoder.decode(number, "UTF-8"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment