Java - JUnit Testing

JUnit Testing

Setup

// Gradle (Groovy DSL)
dependencies {
  testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
}
test { useJUnitPlatform() }

Example Test

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
class MathTest {
  @Test void adds(){ assertEquals(5, 2+3); }
}

Parameterized

import org.junit.jupiter.params.*;
import org.junit.jupiter.params.provider.*;
class ParamTest {
  @ParameterizedTest
  @ValueSource(ints = {1,2,3})
  void positive(int n){ assertTrue(n > 0); }
}
Architect note: Keep tests deterministic; isolate time/IO with fakes or abstractions.

Try it

  1. Create a simple class with a method and write a JUnit test that asserts its behavior.
  2. Add a parameterized test with 3 inputs and expected outputs.