greg woodfill

about | github | linkedin

Currently working on iOS, Android, Web with Swift, Kotlin, and Vue.js front-end. With AWS Lambda functions in Kotlin and Ruby. For more information about me and contact info check out the about page.

posts


Kotlin + Spring Boot's ConfigurationProperties

One minor annoyance i ran into with Spring Boot + Kotlin was that I couldn’t find good documentation on getting Intellij’s autocompletion working with Kotlin classes annotated with @ConfigurationProperties.

ConfigurationProperties can be used to bind property values from application-[profile].properties or application.yml instead of using @Value. The benefits are discussed in many places around the web.

For awhile I was just using Java for my ConfigurationProperties class, adding private fields and creating getters and setters. That works, but getting them working in Kotlin is trivial if you know what to do and results in a lot less boiler plate.

The biggest issue thing is that you need to use kapt, the Kotlin Annotation Processor. You also need to tell kapt to process resources using org.springframework.boot:spring-boot-configuration-processor

build.gradle

plugins {
    apply plugin: 'kotlin-kapt'
}

dependencies{
    kapt "org.springframework.boot:spring-boot-configuration-processor"
	compileOnly "org.springframework.boot:spring-boot-configuration-processor"

}
compileJava.dependsOn(processResources)

Then you can just create a plain old Kotlin class that’s annotated with @ConfigurationProperties

@ConfigurationProperties(prefix = "myapp")
class DemoConfigurationProperties {
    var url: URL? = null
}

In order for Intellij to autocomplete you just need to run gradle to the point of processing resources. ./gradlew compileKotlin does the trick.

Example project here: kotlin-spring-demo


JUnit @Rules with Kotlin

While testing a service with WireMock, which uses a JUnit @Rule, we ran into an issue where JUnit rules are supposed to be private final fields, Kotlin does not directly support fields, and instead has properties that had field access.

The solution is to use Kotlin’s @JvmField annotation as well as marking the field as final.

@Rule
@JvmField
final val wireMockRule = WireMockRule(8181)

Kotlin JSON Assertions

I’ve just released a new project called Kotlin Json Assert on GitHub. It is a library for asserting JSON with Kotlin, that uses the style of the Kluent style assertions for JSON project. It is a thin wrapper over the excellent JSONAssert library.

Sample Usage

// strict checking is off by default
 """{"firstName": "Bob", "lastName":"Smith"}""" shouldEqualJson """{ "firstName": "Bob" }"""
"{}" shouldNotEqualJson """{"firstName":"Bob"}"""
// strict checking can be applied with `should strictly equal`
"""{"firstName": "Bob"}""" shouldStrictlyEqualJson """{ "firstName": "Bob" }"""

Groovy @ Operator

In Groovy there are times you need to bypass the implicit calls to a getter or setter when calling.

class Foo {
  String a = "bar"

  String getA() {
    "foobar"
  }
}

You can bypass this by calling object.@property e.g. foo.@a == "bar"


Speeding up git-svn

Git-svn has been an essential part of my workflow for about a year since I last worked on a pure fit project. Using it on a huge monolithic repo can be unbearably slow on the initial clone and subsequent fetches

Running

git fetch|clone --log-window-size=1000

will cause git to fetch svn commits of blocks of 1000 instead of the default 100. You can specify to your liking, though it may cause heap or timeout issues if set too large.