Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert project to Gradle+Kotlin #2

Merged
merged 19 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ This repository contains examples of basic unit tests written in Kotlin. In spec

`MVP` - Model-View-Presenter contract



# Gradle, Kotlin & Groovy

This Gradle project is by default configured with [Gradle's Kotlin DSL](https://github.com/gradle/kotlin-dsl)

The equivalent Groovy build are still available for you copy&pasting pleasures, they have been renamed as `build.gradle.groovy`

You can switch between Groovy and Kotlin by running the script `$ ./toggle-kotlin-groovy.sh`



## Junit4
[https://junit.org/junit4/](https://junit.org/junit4/)
### Attach to project
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions application/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
plugins {
application
kotlin("jvm")
}

dependencies {
compile(Libs.kotlin_stdlib_jdk8)
testCompile(Libs.junit)
}

File renamed without changes.
31 changes: 31 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile


plugins {
id("jmfayard.github.io.gradle-kotlin-dsl-libs") version "0.2.3"
`kotlin-dsl`
}


allprojects {

group = "me.rozkmin.testing"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need both mavenCentral and jcenter? jcenter is superset of maven central

jcenter()
}

tasks.withType<KotlinCompile>().configureEach {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need configureEach in this case

kotlinOptions.jvmTarget = "1.8"
}

apply(plugin = "java")
java {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather to use plugins.withType, so you could apply java plugin specific settings only if some of modules applied java plugin, and do not force to apply it

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

}

8 changes: 8 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}

Empty file added buildSrc/settings.gradle.kts
Empty file.
88 changes: 88 additions & 0 deletions buildSrc/src/main/kotlin/Libs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import kotlin.String

/**
* Generated by [gradle-kotlin-dsl-libs](https://github.com/jmfayard/gradle-kotlin-dsl-libs)
*
* Run again
* `$ ./gradlew syncLibs`
* to update this file */
object Libs {
const val jmfayard_github_io_gradle_kotlin_dsl_libs_gradle_plugin: String =
"jmfayard.github.io.gradle-kotlin-dsl-libs:jmfayard.github.io.gradle-kotlin-dsl-libs.gradle.plugin:" + Versions.jmfayard_github_io_gradle_kotlin_dsl_libs_gradle_plugin

/**
* [junit website](http://junit.org) */
const val junit: String = "junit:junit:" + Versions.junit

/**
* [junit-jupiter-api website](http://junit.org/junit5/) */
const val junit_jupiter_api: String =
"org.junit.jupiter:junit-jupiter-api:" + Versions.junit_jupiter_api

/**
* [junit-jupiter-engine website](http://junit.org/junit5/) */
const val junit_jupiter_engine: String =
"org.junit.jupiter:junit-jupiter-engine:" + Versions.junit_jupiter_engine

/**
* [junit-jupiter-params website](http://junit.org/junit5/) */
const val junit_jupiter_params: String =
"org.junit.jupiter:junit-jupiter-params:" + Versions.junit_jupiter_params

/**
* [kotlin-gradle-plugin website](https://kotlinlang.org/) */
const val kotlin_gradle_plugin: String =
"org.jetbrains.kotlin:kotlin-gradle-plugin:" + Versions.kotlin_gradle_plugin

/**
* [kotlin-reflect website](https://kotlinlang.org/) */
const val kotlin_reflect: String =
"org.jetbrains.kotlin:kotlin-reflect:" + Versions.kotlin_reflect

/**
* [kotlin-sam-with-receiver website](https://kotlinlang.org/) */
const val kotlin_sam_with_receiver: String =
"org.jetbrains.kotlin:kotlin-sam-with-receiver:" + Versions.kotlin_sam_with_receiver

/**
* [kotlin-scripting-compiler-embeddable website](https://kotlinlang.org/) */
const val kotlin_scripting_compiler_embeddable: String =
"org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:" + Versions.kotlin_scripting_compiler_embeddable

/**
* [kotlin-stdlib-jdk8 website](https://kotlinlang.org/) */
const val kotlin_stdlib_jdk8: String =
"org.jetbrains.kotlin:kotlin-stdlib-jdk8:" + Versions.kotlin_stdlib_jdk8
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use string templates?


/**
* [kotlintest-runner-junit5 website](http://www.github.com/kotlintest/kotlintest) */
const val kotlintest_runner_junit5: String =
"io.kotlintest:kotlintest-runner-junit5:" + Versions.kotlintest_runner_junit5

/**
* [mockito-core website](https://github.com/mockito/mockito) */
const val mockito_core: String = "org.mockito:mockito-core:" + Versions.mockito_core

/**
* [mockito-kotlin website](https://github.com/nhaarman/mockito-kotlin) */
const val mockito_kotlin: String =
"com.nhaarman.mockitokotlin2:mockito-kotlin:" + Versions.mockito_kotlin

/**
* [mockk website](http://mockk.io) */
const val mockk: String = "io.mockk:mockk:" + Versions.mockk

const val org_gradle_kotlin_kotlin_dsl_gradle_plugin: String =
"org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin:" + Versions.org_gradle_kotlin_kotlin_dsl_gradle_plugin

/**
* [spek-dsl-jvm website](https://spekframework.github.io/spek) */
const val spek_dsl_jvm: String = "org.spekframework.spek2:spek-dsl-jvm:" + Versions.spek_dsl_jvm

/**
* [spek-runner-junit5 website](https://spekframework.github.io/spek) */
const val spek_runner_junit5: String =
"org.spekframework.spek2:spek-runner-junit5:" + Versions.spek_runner_junit5

const val strikt_core: String = "io.strikt:strikt-core:" + Versions.strikt_core
}
58 changes: 58 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import kotlin.String

/**
* Generated by [gradle-kotlin-dsl-libs](https://github.com/jmfayard/gradle-kotlin-dsl-libs)
*
* Run again
* `$ ./gradlew syncLibs`
* to update this file */
object Versions {
const val jmfayard_github_io_gradle_kotlin_dsl_libs_gradle_plugin: String =
"0.2.3" // available: milestone=0.2.4

const val junit: String = "4.12" // up-to-date

const val junit_jupiter_api: String = "5.3.1" // up-to-date
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you decide to violate Kotlin style guide and do not use camelCase for constants?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I forgot to reply. I follow the Kotlin style guide by default but in this case, I decided to that it was more important to be closer to what the maven coordinate is.


const val junit_jupiter_engine: String = "5.3.1" // up-to-date

const val junit_jupiter_params: String = "5.3.1" // up-to-date

const val kotlin_gradle_plugin: String = "1.2.61" // available: milestone=1.3.0-rc-190

const val kotlin_reflect: String = "1.2.71" // available: milestone=1.3.0-rc-190

const val kotlin_sam_with_receiver: String = "1.2.61" // available: milestone=1.3.0-rc-190

const val kotlin_scripting_compiler_embeddable: String =
"1.2.61" // available: milestone=1.3.0-rc-190

const val kotlin_stdlib_jdk8: String = "1.2.71" // available: milestone=1.3.0-rc-190

const val kotlintest_runner_junit5: String = "3.1.7" // available: milestone=3.1.10

const val mockito_core: String = "2.23.0" // up-to-date

const val mockito_kotlin: String = "2.0.0-RC1" // available: milestone=2.0.0-RC3

const val mockk: String = "1.8.9" // up-to-date

const val org_gradle_kotlin_kotlin_dsl_gradle_plugin: String =
"1.0-rc-5" // available: milestone=1.0-rc-13

const val spek_dsl_jvm: String = "2.0.0-alpha.1" // available: milestone=2.0.0-alpha.2

const val spek_runner_junit5: String = "2.0.0-alpha.1" // available: milestone=2.0.0-alpha.2

const val strikt_core: String = "0.16.0" // available: milestone=0.16.2

object Gradle {
const val runningVersion: String = "4.10.2"

const val currentVersion: String = "4.10.2"

const val nightlyVersion: String = "5.1-20181022112637+0000"

const val releaseCandidate: String = ""
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
File renamed without changes.
10 changes: 10 additions & 0 deletions junit4/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
plugins {
java
kotlin("jvm")
}

dependencies {
implementation(project(":application"))
Copy link

@gildor gildor Oct 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also use approach when all our modules are items of enum, so access to modules became type safe, I probably share sample project that uses this approach

compile(Libs.kotlin_stdlib_jdk8)
testCompile(Libs.junit)
}
File renamed without changes.
23 changes: 23 additions & 0 deletions junit5/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
java
kotlin("jvm")
}

dependencies {
implementation(project(":application"))
testCompile(Libs.junit_jupiter_api)
testCompile(Libs.junit_jupiter_params)
testRuntime(Libs.junit_jupiter_engine)
}

// GROOVY: test { ... }
tasks.withType<Test>().configureEach {
Copy link

@gildor gildor Oct 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, you don't need configureEach
If you want to configure only task "test", not all tasks with this type (same is in groovy), instead you can use tasks.named("test") {} (not sure that available before Gradle 5, than you can use some other DSL to get task, such as getByName or delegates)

useJUnitPlatform()
testLogging {
// GROOVY:
// events "passed", "skipped", "failed"
events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
}
File renamed without changes.
18 changes: 18 additions & 0 deletions kotlintest/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
java
kotlin("jvm")
}

dependencies {
implementation(project(":application"))
testCompile(Libs.kotlintest_runner_junit5)
}



//test {
// useJUnitPlatform()
//}
tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
File renamed without changes.
12 changes: 12 additions & 0 deletions mockito-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
application
kotlin("jvm")
}

dependencies {
compile(Libs.kotlin_stdlib_jdk8)
implementation(project(":application"))
testCompile(Libs.mockito_core)
testCompile(Libs.mockito_kotlin)

}
File renamed without changes.
14 changes: 14 additions & 0 deletions mockito/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
application
kotlin("jvm")
}

dependencies {
compile(Libs.kotlin_stdlib_jdk8)
implementation(project(":application"))
testCompile(Libs.junit)
testCompile(Libs.mockito_core)
testCompile(Libs.junit)
}


File renamed without changes.
11 changes: 11 additions & 0 deletions mockk/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
application
kotlin("jvm")
}

dependencies {
compile(Libs.kotlin_stdlib_jdk8)
implementation(project(":application"))
testCompile(Libs.junit)
testCompile(Libs.mockk)
}
File renamed without changes.
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rootProject.name = "KotlinUnitTesting"
include (":junit4",":junit5",":kotlintest",":spek",":application", ":mockito", ":mockito-kotlin", "mockk", "strikt")

File renamed without changes.
34 changes: 34 additions & 0 deletions spek/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
java
kotlin("jvm")
}

dependencies {
implementation(project(":application"))
testImplementation(Libs.spek_dsl_jvm) {
exclude("org.jetbrains.kotlin")
}
testRuntimeOnly(Libs.spek_runner_junit5) {
exclude("org.junit.platform")
exclude("org.jetbrains.kotlin")
}

testCompile(Libs.junit_jupiter_api)


// spek requires kotlin-reflect, can be omitted if already in the classpath
testRuntimeOnly(Libs.kotlin_reflect)
}

repositories {
maven {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can replace it with maven(("https://dl.bintray.com/spekframework/spek-dev")

setUrl("https://dl.bintray.com/spekframework/spek-dev")
}
}


tasks.withType<Test>().configureEach {
this.useJUnitPlatform {
includeEngines("spek2")
}
}
File renamed without changes.
24 changes: 24 additions & 0 deletions strikt/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import org.gradle.api.tasks.testing.logging.TestLogEvent.*

plugins {
application
kotlin("jvm")
}

dependencies {
compile(Libs.kotlin_stdlib_jdk8)
implementation(project(":application"))

testCompile(Libs.junit_jupiter_api)
testCompile(Libs.junit_jupiter_params)
testRuntime(Libs.junit_jupiter_engine)
testImplementation(Libs.strikt_core)

}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
testLogging {
events = setOf(PASSED, SKIPPED, FAILED)
}
}
Loading