JUNIT / NOTES</>↗JUnit12 Jul 2026
The best way to test private logic is to test the observable behavior that depends on it, usually through the class’s public methods. Private methods are implementation details. If you test them directly, your tests become tightly coupled to how the class is written internally. That makes refactoring harder because changing a private method can […]
JUNIT / NOTES</>↗JUnit12 Jul 2026
Code that depends directly on the current date or time can be difficult to test because the result changes every time the test runs. For example, code like this is hard to test reliably: import java.time.LocalDate; public class SubscriptionService { public boolean isExpired(LocalDate expiryDate) { return expiryDate.isBefore(LocalDate.now()); } } The problem is LocalDate.now(). Today’s test […]
JUNIT / NOTES</>↗JUnit12 Jul 2026
In JUnit 5, assumptions let you run a test only when certain conditions are true. If an assumption fails, the test is skipped/aborted, not failed. They are useful when a test depends on things like: operating system environment variables external services database availability specific Java version local developer setup JUnit assumptions are available from: import […]
JUNIT / NOTES</>↗JUnit12 Jul 2026
In JUnit 5, repeated tests are written using the @RepeatedTest annotation. A repeated test runs the same test method multiple times without requiring different input values. Basic Example import org.junit.jupiter.api.RepeatedTest; import static org.junit.jupiter.api.Assertions.assertTrue; class RandomNumberTest { @RepeatedTest(5) void randomNumberShouldBeLessThanTen() { int number = (int) (Math.random() * 10); assertTrue(number >= 0 && number < 10); } […]
JUNIT / NOTES</>↗JUnit12 Jul 2026
To run only selected JUnit 5 tests by tag, mark your tests with @Tag, then configure your build tool or IDE to include only that tag. 1. Add tags to your tests import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; class PaymentServiceTest { @Test @Tag("fast") void calculatesTotal() { // test code } @Test @Tag("integration") void connectsToPaymentGateway() { // test […]
JUNIT / NOTES</>↗JUnit12 Jul 2026
In JUnit 5, you can use the @Tag annotation to group tests into categories such as: fast slow unit integration database api smoke Tags are useful when you want to run only certain groups of tests, for example only fast unit tests during development, or only integration tests in a CI pipeline. 1. Basic Example […]