Week 2 Environment Setup & Unit Testing Introduction

In this week’s lab you will:

  1. Briefly review the tutorial roadmap for the semester
  2. Use IntelliJ IDEA for Java development
  3. Learn unit testing concepts (focal method, test prefix, test oracle, coverage)
  4. Write unit tests and view coverage
  5. (If time) Download and explore Defects4J

Prerequisites

  1. Java JDK 8+ — for compiling and running Java
  2. IntelliJ IDEA — Community Edition (free) or student license; lab machines may have it pre-installed
  3. (Optional) Docker — for Defects4J if using the course image

Outline (1 hour)

Part Activity Time (guide)
1 Tutorial roadmap + IntelliJ setup ~10 min
2 Unit testing concepts and demonstration ~15 min
3 Hands-on: write tests and view coverage ~20 min
4 Defects4J intro and download (if time) ~15 min

Activity 1: Tutorial roadmap and IntelliJ (~10 min)#

Task 1.1: Tutorial roadmap#

This semester you will: use unit testing to validate code; use EvoSuite for automated test generation; use LLMs to generate tests; use SootUp to extract code knowledge; design prompts and pipelines; and practice writing up your approach (e.g. for assignments).

Task 1.2: IntelliJ setup and Hello World#

  1. Open IntelliJ IDEA (pre-installed in lab or download).
  2. New Project → Java, choose JDK 8 or higher.
  3. Create a Java class with a main method that prints “Hello, World!”
  4. Run (green play or Shift+F10) to verify.

If using your own laptop, install IntelliJ Community Edition and ensure the JDK is configured in File → Project Structure → Project.


Activity 2: Unit testing concepts and demonstration (~15 min)#

What is unit testing?#

Unit testing runs the smallest testable parts of your code (e.g. single methods) to check they behave as intended. A unit test provides inputs, calls the code, and checks outputs (test oracle).

Key ideas:

  • Focal method — the method under test
  • Test prefix — setup (e.g. create object, set inputs) so the focal method can be called
  • Test oracle — how we decide pass/fail (e.g. assertions comparing actual vs expected)

Example: Calculator#

Focal class and methods:

public class Calculator {
    public int add(int a, int b) { return a + b; }
    public int subtract(int a, int b) { return a - b; }
}

Test class (JUnit 4 style):

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CalculatorTest {
    private Calculator calculator = new Calculator();

    @Test
    public void testAdd() {
        int result = calculator.add(5, 3);   // focal method
        assertEquals(8, result);             // test oracle
    }

    @Test
    public void testSubtract() {
        int result = calculator.subtract(5, 3);
        assertEquals(2, result);
    }
}

Where is the test prefix in this example? What would you change to use JUnit 5 (@BeforeEach, Assertions.assertEquals)?

Evaluating test quality: coverage#

  • Line coverage — proportion of lines executed by tests
  • Method coverage — proportion of methods executed
  • Class coverage — proportion of classes with at least one executed line

Higher coverage often means more thorough testing, but coverage alone does not guarantee correctness.


Activity 3: Hands-on — write tests and view coverage (~20 min)#

Task 3.1: Create a test class in IntelliJ#

  1. Use the Calculator class (or a small class provided by your tutor).
  2. Right-click the class → Go toTestCreate New Test.
  3. Choose JUnit 4 (or 5), add test methods for add and subtract.
  4. Use assertEquals (or equivalent) for the oracle.
  5. Run the test class (green play next to the class or method).

Task 3.2: Run with coverage#

  1. Run with Coverage (e.g. right-click test class → Run with Coverage, or Run → More Run/Debug → Run with Coverage).
  2. Note line (and if shown, method) coverage.
  3. Add one more test (e.g. negative numbers or edge case) and run again.
  4. See how coverage or the report changes.

If a method has 100% line coverage but only one test, what might we be missing? (Consider branches and edge cases.)

Activity 4: Defects4J intro and download (~15 min)#

Defects4J provides real Java projects with known bugs and developer-written tests, often used in research and assignments.

If using the course Docker image (comp4130-env:1.0), Defects4J is pre-installed. Use Java 11 inside the container for Defects4J.

Task 4.1: Check Defects4J (Docker)#

  1. Load the Docker image (only once): docker load -i comp4130-env-1.0.tar

  2. Start the container: docker run -it --rm comp4130-env:1.0

  3. Verify the installation: defects4j info -p Lang

  4. Check out a buggy version: defects4j checkout -p Lang -v 1b -w lang_1_buggy

  5. Inspect the project structure: ls lang_1_buggy/

If you install Defects4J manually, ensure Java 11 and that framework/bin is on your PATH. See Defects4J README.

After the tutorial: explore src/test/java in the checked-out project and compare developer-written tests with what you wrote by hand.


Learning objectives#

By the end of this tutorial you should be able to:

  • Use IntelliJ for Java and run a simple program
  • Explain focal method, test prefix, and test oracle
  • Write JUnit tests and run them in IntelliJ
  • Run tests with coverage and interpret line/method coverage
  • (If completed) Checkout a Defects4J project and locate source and test directories

References

bars magnifying-glass xmark