Skip to content

Instantly share code, notes, and snippets.

@asilichenko
Last active September 9, 2023 11:58
Show Gist options
  • Save asilichenko/d954f7f619bba9a26ac5be7f77f321ea to your computer and use it in GitHub Desktop.
Save asilichenko/d954f7f619bba9a26ac5be7f77f321ea to your computer and use it in GitHub Desktop.
JUnit5 Jupiter unit and parametrized tests example
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
/**
* build.gradle:
*
* plugins {
* // for Android project
* id 'de.mannodermaus.android-junit5' version '1.9.3.0'
* }
*
* dependencies {
* // Writing and executing Unit Tests on the JUnit Platform
* testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
* testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
*
* // JUnit5 support to run JUnit4 tests
* testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.10.0'
*
* // "Parameterized Tests"
* testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.0'
*
* // Android Tests
* androidTestImplementation 'androidx.test.ext:junit:1.1.5'
* androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
* androidTestImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
* androidTestImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.0'
* }
*/
public class ExampleUnitTest {
@Test
void testAddition() {
int result = 2 + 2;
assertEquals(4, result);
}
static Stream<Arguments> provideTestArguments() {
return Stream.of(
Arguments.of(1, 1),
Arguments.of(2, 4),
Arguments.of(3, 9)
);
}
@ParameterizedTest
@MethodSource("provideTestArguments")
void testSquare(int input, int expected) {
int result = input * input;
assertEquals(expected, result);
}
}
@asilichenko
Copy link
Author

asilichenko commented Sep 8, 2023

Sources of the JUnit5 Jupiter plugin for Android project support:

without this plugin when you run your JUnit5 tests you get error "No tests found for given includes".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment