Testing Kotlin with Spock Part 3 – Interface default method
Kotlin allows you to put method implementation in an interface. The same mechanism can be found in Java interfaces as default methods (and also Groovy or Scala traits). Let’s see the difference between the Kotlin and Java default methods in interface by testing it with Groovy and Spock.
What do we want to test?
We often have an interface for access object from the database. In domain, they might look similar to this KotlinOrderRepository
:
interface KotlinOrderRepository { fun save(order: Order) fun find(orderId: OrderId): Order? fun get(orderId: OrderId): Order = find(orderId) ?: throw NotFound() }
How to fake it with Groovy?
When we want to use such interface in tests, we can, of course, mock it. However, it is far better to fake repositories with a simple, in-memory implementation. Let’s create FakeKotlinOrderRepository
in Groovy:
class FakeKotlinOrderRepository implements KotlinOrderRepository { private Map<OrderId, Order> data = [:] @Override void save(Order order) { data[order.id] = order } @Override Order find(OrderId orderId) { return data[orderId] } }
Unfortunately, this causes a compilation error
/testing-kotlin-in-spock/src/test/groovy/com/github/alien11689/testingkotlinwithspock/defaultmethod/FakeKotlinOrderRepository.groovy: 3: Can't have an abstract method in a non-abstract class. The class 'com.github.alien11689.testingkotlinwithspock.defaultmethod.FakeKotlinOrderRepository' must be declared abstract or the method 'com.github.alien11689.testingkotlinwithspock.defaultmethod.Order get(com.github.alien11689.testingkotlinwithspock.defaultmethod.OrderId)' must be implemented. @ line 3, column 1. class FakeKotlinOrderRepository implements KotlinOrderRepository { ^ 1 error
The compiler doesn’t see the implementation of the get
method in the Kotlin interface. We have to use some magic to make it work in groovy.
Solution
To solve the problem, let’s look into the generated classes:
$ ls build/classes/main/com/github/alien11689/testingkotlinwithspock/defaultmethod/ JavaOrderRepository.class KotlinOrderRepository.class KotlinOrderRepository$DefaultImpls.class NotFound.class Order.class OrderId.class
The KotlinOrderRepository$DefaultImpls
class is the one we’re looking for as we can use it in Groovy to implement the missing operation.
class FakeKotlinOrderRepository implements KotlinOrderRepository { // ... Order get(OrderId orderId) { return KotlinOrderRepository.DefaultImpls.get(this, orderId) } }
Now the code compiles and tests pass:
class KotlinRepositoryWithDefaultMethodTest extends Specification { OrderId orderId = new OrderId(UUID.randomUUID() as String) Order order = new Order(orderId, 'data') KotlinOrderRepository kotlinOrderRepository = new FakeKotlinOrderRepository() def 'should get order from kotlin repository'() { given: kotlinOrderRepository.save(order) expect: kotlinOrderRepository.get(orderId) == order } def 'should throw NotFound when order does not exist in kotlin repository'() { when: kotlinOrderRepository.get(orderId) then: thrown(NotFound) } }
Is there the same problem with Java?
Let’s have a quick look at how this works with Java interfaces. If we write a similar repository in Java:
public interface JavaOrderRepository { void save(Order order); Optional<Order> find(OrderId orderId); default Order get(OrderId orderId) { return find(orderId).orElseThrow(NotFound::new); } }
and create a fake implementation in Groovy:
class FakeJavaOrderRepository implements JavaOrderRepository { private Map<OrderId, Order> data = [:] @Override void save(Order order) { data[order.id] = order } @Override Optional<Order> find(OrderId orderId) { return Optional.ofNullable(data[orderId]) } }
there is no compilation error and the tests pass:
class JavaRepositoryWithDefaultMethodTest extends Specification { OrderId orderId = new OrderId(UUID.randomUUID() as String) Order order = new Order(orderId, 'data') JavaOrderRepository javaOrderRepository = new FakeJavaOrderRepository() def 'should get order from java repository'() { given: javaOrderRepository.save(order) expect: javaOrderRepository.get(orderId) == order } def 'should throw NotFound when order does not exist in java repository'() { when: javaOrderRepository.get(orderId) then: thrown(NotFound) } }
Groovy can implement Java interfaces with the default methods without any problems.
Show me the code
Code is available here.
Published on Java Code Geeks with permission by Dominik Przybysz, partner at our JCG program. See the original article here: Testing Kotlin with Spock Part 3 – Interface default method Opinions expressed by Java Code Geeks contributors are their own. |