PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   11-09-2021

Test Case

p10 ! A test case provides an example of expected behavior.
 
/**
 * Test Case - AssertEquals Example
 * 
 * AAA (Arrange, Act, Assert)
 */

import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

public class App {

    @Test public void mytest() { // Look Here
        
        // Arange
        Squares squares = new Squares();
        squares.add(3); // 9
        squares.add(5); // 25

        // Act
        int actual = squares.average();  // 9 + 25 = 34
        int expected = 17;

        // Assert
        assertEquals(actual, expected); // passed
    }
}

class Squares {

    private List<Integer> squares = new ArrayList<>();

    public void add(int x) {
        squares.add(x * x); 
    }

    public int average() { 
        int total = squares.stream().mapToInt(Integer::intValue).sum();
        return total / squares.size(); 
    }
}
... 26 lines
 
A test case example using lambdas expresions & functional interface
 
/**
 * Test Case - AssertEquals Lambdas Example
 * 
 * AAA (Arrange, Act, Assert)
 */

import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

public class App {
    
    @Test public void mytest() {

        Squares squares = new Squares();
        squares.add(() -> 3 * 3); // Look Here
        squares.add(() -> 5 * 5);

        assertEquals(squares.average(), 17); // passed
    }
}

class Squares {

    private List<Square> squares = new ArrayList<>(); // Look Here

    public void add(Square s) { 
        squares.add(s); 
    }

    public int average() {
        int total = squares.stream().mapToInt(Square::get).sum();
        return total / squares.size();
    }
}

@FunctionalInterface interface Square { int get(); } // Look Here
... 22 lines
 
A test case example using generic types.
 
/**
 * Test Cases - with Generic type params
 */

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

import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

public class App {

    @Test public void integers() {
        
        MyList<Integer> numbers = new MyList<>();
        numbers.add(3);
        numbers.add(5);

        assertEquals(numbers.sum(), 8); // passed
    }

    @Test public void doubles() {
        
        MyList<Double> numbers = new MyList<>();
        numbers.add(3.0);
        numbers.add(5.0);

        assertEquals(numbers.sum(), 8);        // passed
        assertNotEquals(numbers.sum(), "8.0"); // passed
    }
}

class MyList<T extends Number> {

    private List<T> items = new ArrayList<>();

    public void add(T t) {
        items.add(t); 
    }

    public int sum() { 
        return items.stream()
                    .mapToInt(Number::intValue)
                    .sum();
    }
}
... 28 lines
 

TEST SUITE

A TestSuite can group multiple files with test classes.
 
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class) 
@SuiteClasses({A.class, B.class})
public class App {
    
    @BeforeClass static public void before() {
        System.setProperty("resource_loaded", "1");
        System.out.println("Db connect");
    }

    @AfterClass static public void after() {
        System.out.println("Db close");
    }
}

/**
 
    Db connect
    Db connect in A - skipped
    test A
    Db connect in B
    test B
    Db close

    
    ------------------------------------

    import org.junit.BeforeClass;
    import org.junit.Test;
    public class A {

        @BeforeClass static public void before() {
            if(System.getProperty("resource_loaded") == "1") {
                System.out.println("Db connect in A - skipped");
                return;
            }
            System.out.println("Db connect in A");
        }

        @Test public void test() {
            System.out.println("test A");
        }
    }

    ------------------------------------

    import org.junit.Test;
    public class B {
        @Test public void test() {
            System.out.println("test B");
        }
    }

*/
... 40 lines
 

Questions    
@Before