☕ Java

Code-Testen in Java: Eine ausführliche Anleitung

What is it and why? (a real-life analogy)

Imagine you wrote a program for a school diary. It should be able to add grades, calculate the average score, and show the schedule. You did everything, the program runs — hooray! But how can you be sure it works correctly? What if it calculates the average score as 5, when it should be 4? Or it records a grade for the wrong student?

In real life, when building a bridge, it is first tested for strength — a load is placed on it to see if it will hold. When baking a pie, you try a small piece to see if the dough is too salty. In programming, it's the same: we test the code, that is, we check whether the program does what we expect it to do.

Testing is like a test for your program. You ask it questions (for example, "what is 2 + 2?") and see if it gives the correct answer. If the answer is wrong, it means there is an error (a bug) in the code, and it needs to be fixed.

💡 Important: Testing does not make code perfect. It only shows that the code works correctly in tested situations. But the more tests you have, the more confident you can be in the program.

Why test if you can just run the program and look? The thing is, a program can consist of thousands of lines of code. Manually checking every button and every function is very time-consuming and boring. And if you change something in the code, you have to check everything all over again. Tests, on the other hand, can be run with a single button, and the computer will check everything in seconds.

Theory: how it works internally

In Java, testing is usually built around unit tests. The word "unit" means that we are not testing the entire program as a whole, but its small parts — individual methods or classes. It's like checking each screw in a mechanism separately, rather than assembling the entire engine at once.

Special libraries are used for writing tests in Java. The most popular one is JUnit. It provides us with ready-made tools to quickly write checks.

What does a test in Java look like? It's a regular method, but with special markers (annotations). Inside this method, we:

  1. Prepare the data — create the objects we are going to test.
  2. Perform the action — call the method we want to test.
  3. Check the result — compare what the method returned with what we expected.

If the result matches the expectation, the test is green (passed). If not, it's red (failed). It's that simple!

In Java, there are different types of tests, but we'll start with the most important one — assert. This is a command that says: "I assert that this expression is true." If it is false, the test fails.

⚡ Remember: A good test checks one thing. If a test checks both addition, multiplication, and division at the same time — when an error occurs, you won't understand what exactly broke.

Syntax in Java: all forms and variants

For testing in Java, we will use the JUnit 5 library (the newest version). Here are the main annotations and methods you will need:

Annotation / Method What it does Example
@Test Marks a method as a test method. JUnit will run this method. @Test void testSum() { ... }
assertEquals(expected, actual) Checks that two values are equal. assertEquals(5, calculator.add(2, 3));
assertTrue(condition) Checks that the condition is true. assertTrue(result > 0);
assertFalse(condition) Checks that the condition is false. assertFalse(list.isEmpty());
assertNull(object) Checks that the object is null. assertNull(user.getMiddleName());
assertNotNull(object) Checks that the object is not null. assertNotNull(user.getName());
assertThrows(exception_type, code) Checks that the code threw an exception (error). assertThrows(ArithmeticException.class, () -> calc.divide(1, 0));
@BeforeEach The method with this annotation runs BEFORE EACH test. Used for preparing data. @BeforeEach void setUp() { calc = new Calculator(); }
@AfterEach The method with this annotation runs AFTER EACH test. Used for "cleanup". @AfterEach void tearDown() { calc = null; }

To start testing, you need to add JUnit to your project. If you are using an IDE like IntelliJ IDEA or Eclipse, it will offer to do this itself. If not, you need to download the junit-platform-console-standalone.jar file and add it to the project.

Code examples —