Return Method Tests

Each example assumes the students file name is Example#.java replacing # with the actual example number. You can call the methods based on the student filename of your choice.

Example 1 - Add Five

Students write an int method named addFive(int number) that takes an integer as a parameter and returns an int equal to 5 plus the parameter value.

Grading Tests:

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

public class CodingRoomsUnitTests {
    @Test
    public void testDefaultCase() {
        // You may rename this method to better suit the purpose of your test case
        // Your test case logic here
        assertEquals(10, Example1.addFive(5));
        assertEquals(5, Example1.addFive(0));
        assertEquals(11, Example1.addFive(6));
    }
}

Sample Solution:

Example 2 - Average

Students write a method that takes 5 int values as parameters and returns the average value of the 5 ints as a double. The method must be named average() and it must have 5 int parameters. The method must return a double.

Example: Calling average(1, 5, 7, 4, 10) would return 5.4.

Note: Assertions with doubles must have delta value (tolerance when comparing values).

Grading Tests:

Sample Solution:

Example 3 - Repeats

Students write a method that takes a String parameter. If the String has a double letter (i.e. contains the same letter twice in a row) then it should return true. Otherwise, it should return false.

This method must be named hasRepeat(String str) and have a String parameter. This method must return a boolean.

Grading Tests:

Sample Solution:

Last updated

Was this helpful?