Unit testing with Java 8 features
See original GitHub issueI 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:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yup! Removed and still works. Thanks for the heads-up. For posterity, this is the block now:
This is now included as part of the new Getting Started guide. Thanks again!