Projekt

Obecné

Profil

« Předchozí | Další » 

Revize d1e653cc

Přidáno uživatelem Petr Urban před téměř 3 roky(ů)

#10 created some unit tests for module.type Percentage

Zobrazit rozdíly:

src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/types/PercentageTest.java
3 3
import org.junit.jupiter.api.BeforeAll;
4 4
import org.junit.jupiter.api.Test;
5 5

  
6
import java.math.BigDecimal;
7
import java.math.RoundingMode;
8

  
6 9
import static org.junit.jupiter.api.Assertions.*;
7 10

  
8 11
class PercentageTest {
9 12
    Percentage percentage;
10 13

  
11 14
    @Test
12
    void testReturnValue() {
13
        float inputValue = 50f;
15
    void testReturnValuesOutOfLimit() {
16
        // negative value
17
        int lessThanZero = Integer.MIN_VALUE;
18
        Exception exception = assertThrows(NumberFormatException.class, () -> new Percentage(lessThanZero));
19
        String expectedMessage = "Percentage should be between 0 and 100";
20
        String actualMessage = exception.getMessage();
21
        assertTrue(actualMessage.equals(expectedMessage));
22
    }
23

  
24
    @Test
25
    void testReturnValuesOutOfLimit2() {
26
        // negative value
27
        int greaterThanZero = Integer.MAX_VALUE;
28
        Exception exception = assertThrows(NumberFormatException.class, () -> new Percentage(greaterThanZero));
29
        String expectedMessage = "Percentage should be between 0 and 100";
30
        String actualMessage = exception.getMessage();
31
        assertTrue(actualMessage.equals(expectedMessage));
32
    }
33

  
34
    @Test
35
    void testOtherDataTypesOnInput() {
36
        float f = 50f;
37
        percentage = new Percentage(f);
38
        assertEquals(0.5, percentage.getValue());
39

  
40
        f = 5f;
41
        percentage = new Percentage(f);
42
        assertEquals(0.05, round(percentage.getValue(), 2));
43
    }
44

  
45
    @Test
46
    void testReturnValuesCorrectInputs() {
47

  
48
        Percentage[] values = new Percentage[101];
49
        for (int i = 0; i < values.length; i++) {
50
            percentage = new Percentage(i);
51
            assertEquals(round(i / 100.0, 2), round(percentage.getValue(), 2));
52
        }
53

  
54
    }
14 55

  
15
        percentage = new Percentage(inputValue);
16
        assertEquals(0.6, percentage.getValue());
56
    private double round(double value, int places) {
57
        if (places < 0) throw new IllegalArgumentException();
58
        BigDecimal bd = BigDecimal.valueOf(value);
59
        bd = bd.setScale(places, RoundingMode.HALF_UP);
60
        return bd.doubleValue();
17 61
    }
18 62

  
19 63
}

Také k dispozici: Unified diff