Week 2 Environment Setup & Unit Testing Introduction
In this week’s lab you will:
- Briefly review the tutorial roadmap for the semester
- Use IntelliJ IDEA for Java development
- Learn unit testing concepts (focal method, test prefix, test oracle, coverage)
- Write unit tests and view coverage
- (If time) Download and explore Defects4J
Prerequisites
- Java JDK 8+ — for compiling and running Java
- IntelliJ IDEA — Community Edition (free) or student license; lab machines may have it pre-installed
- (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#
- Open IntelliJ IDEA (pre-installed in lab or download).
- New Project → Java, choose JDK 8 or higher.
- Create a Java class with a
mainmethod that prints “Hello, World!” - 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#
- Use the Calculator class (or a small class provided by your tutor).
- Right-click the class → Go to → Test → Create New Test.
- Choose JUnit 4 (or 5), add test methods for
addandsubtract. - Use assertEquals (or equivalent) for the oracle.
- Run the test class (green play next to the class or method).
Task 3.2: Run with coverage#
- Run with Coverage (e.g. right-click test class → Run with Coverage, or Run → More Run/Debug → Run with Coverage).
- Note line (and if shown, method) coverage.
- Add one more test (e.g. negative numbers or edge case) and run again.
- 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)#
-
Load the Docker image (only once):
docker load -i comp4130-env-1.0.tar -
Start the container:
docker run -it --rm comp4130-env:1.0 -
Verify the installation:
defects4j info -p Lang -
Check out a buggy version:
defects4j checkout -p Lang -v 1b -w lang_1_buggy -
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