DEV Community

xh1m
xh1m

Posted on

Sleep Better at Night: Why Unit Testing is Your Secret Weapon for Graded Units 🛡️

Let’s be frank: Most students treat Unit Testing as much as the terms and conditions of a software project - presented to be there, to be ignored, to be quickly scanned to the end in hopes that something good is to be found.

When you are studying Software Engineering or Quality Assurance courses, testing is what separates a Pass from a Distinction. Why? Anyone can write code that works as long as everything is aligned. A Lead Developer is someone who writes code that still works as long as a user decides to type their name in a field labeled “Phone Number.”

What is Unit Testing?
Unit testing is testing a small piece of code, called a unit or a function. For us Java programmers, we use JUnit to accomplish this.

Rather than running the entire application, logging in, and manually clicking through to verify that the CRUD wrapper is working (which is a slow and flaky process), you write a script that does all these tests for you in a matter of milliseconds.

Three Ways to Show Distinction 🏆

When you write the Graded Unit, instead of just showing a single test, you must show a “Test Suite” with three different cases:

1. The Happy Path (Works)
The happy path is where everything works. So, if I pass a StudentID to deleteUser(), does it return true?

What to achieve with the test:
Test if the core function works.

2. The Edge Case (What If)
What if I pass a null or empty StudentID? Does it crash with a NullPointerException, or does it handle it well?

What to achieve with the test:
Test if the code is robust.

3. The Fail Case (Wrong Type)
What if I pass a string to a function expecting an integer? Does the exception handling work well?

What to achieve with the test:
Test if the guardrails are in place.

The Java Example: Testing Your CRUD Wrapper 💻

Let’s take a look at how you would write a simple test for the Java CRUD Wrapper, a topic we discussed earlier in this post and in detail on another post.

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class DatabaseTest {

    // 1. Happy Path
    @Test
    void testConnectionSuccess() {
        DatabaseWrapper db = new DatabaseWrapper();
        assertTrue(db.connect(), "Database should connect successfully with valid credentials.");
    }

    // 2. Edge Case
    @Test
    void testEmptyQuery() {
        DatabaseWrapper db = new DatabaseWrapper();
        assertThrows(IllegalArgumentException.class, () -> {
            db.executeQuery(""); // Should not allow empty strings
        });
    }

    // 3. Fail Case
    @Test
    void testInvalidUserDelete() {
        DatabaseWrapper db = new DatabaseWrapper();
        // Testing a non-existent ID should return false, not crash
        assertFalse(db.deleteUser(-1), "Should return false for non-existent IDs.");
    }
}

Enter fullscreen mode Exit fullscreen mode

Why Tutors Lecturers Love This 🎓

If your tutor spots a test folder within your project, you've already won half the battle. This shows you're interested in Maintainability.

If you decide to refactor your code (for instance, from a local Oracle DB to a Cloud-based one), you can run your tests to ensure your logic is working correctly. This is called 'Lead Dev' thinking. This converts your documentation from a list of features to a Technical Report that shows your application is ready for production.

Top comments (0)