JUNIT / CATATAN</>↗JUnit13 Jul 2026
Utility classes are usually tested like any other unit: test their public behavior, especially calculations, transformations, validation rules, edge cases, and error handling. The main difference is that utility classes often have static methods, so your tests usually call the method directly instead of creating an object. 1. Test Useful Behavior, Not the Fact That […]
JUNIT / CATATAN</>↗JUnit12 Jul 2026
In a real Java project, unit tests should be organized so they are easy to find, run, understand, and maintain. A good structure follows the same shape as your production code and keeps tests focused on behavior. Most Java projects use this standard layout: project-root ├── src │ ├── main │ │ └── java │ […]
JUNIT / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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); } […]