question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing with Java 8 features

See original GitHub issue

I just wanted to share something I discovered that might help others. I wanted to write parameterized tests using Stream.of() and Arguments.of(). Those obviously require a Java 8+ build target. You can do the following:

// in root build.gradle
allprojects {
    // We need this to use certain features of JUnit5 (such as calling static methods on interfaces)
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).matching { "$it".contains("UnitTest") }.all {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8

        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
}

Obviously I’m using Kotlin here. I had an earlier iteration of this script that didn’t have the matching block, and it broke on earlier versions of Android 😃 But this seems to work just fine.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
autonomousappscommented, Apr 26, 2018

Yup! Removed and still works. Thanks for the heads-up. For posterity, this is the block now:

// in root build.gradle
allprojects {
    // We need this to use certain features of JUnit5 (such as calling static methods on interfaces)
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).matching { "$it".contains("UnitTest") }.all {
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
}
0reactions
mannodermauscommented, Jul 28, 2018

This is now included as part of the new Getting Started guide. Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito's Java 8 Features - Baeldung
1. Overview · 2. Mocking Interface With a Default Method · 3. Return Default Values for Optional and Stream · 4. Leveraging Lambda...
Read more >
Pragmatic Unit Testing in Java 8 with JUnit
Freshly updated for modern software development, Pragmatic Unit Testing in Java 8 With JUnit teaches you how to write and run easily maintained...
Read more >
Unit Testing Java 8 Lambda Expressions and Streams - Radar
Lambda expressions pose a slightly different challenge when unit testing code. Because they don't have a name, it's impossible to directly call ...
Read more >
Writing Clean Tests - Java 8 to the Rescue - Petri Kainulainen
This blog post describes how we can catch exceptions and capture method parameters without writing any boilerplate code.
Read more >
Unit Testing: Java Streams and Lambdas - DZone
Writing unit tests is one of the core parts in software development. With new features introduced after JDK 8, it has become easier...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found