forked from SonarSource/sonarqube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRectangleTest.java
60 lines (46 loc) · 1.79 KB
/
RectangleTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class RectangleTest {
@Test
void testConstructorAndGetters() {
Rectangle rect = new Rectangle(5.0, 3.0);
assertEquals(5.0, rect.getWidth(), "Width should be 5.0");
assertEquals(3.0, rect.getHeight(), "Height should be 3.0");
assertEquals(15.0, rect.getArea(), "Area should be 15.0");
assertEquals(16.0, rect.getPerimeter(), "Perimeter should be 16.0");
}
@Test
void testSetters() {
Rectangle rect = new Rectangle();
rect.setWidth(10.0);
rect.setHeight(4.0);
// Expectations
assertEquals(10.0, rect.getWidth(), "Width should be 10.0");
assertEquals(4.0, rect.getHeight(), "Height should be 4.0");
assertEquals(40.0, rect.getArea(), "Area should be 40.0");
assertEquals(28.0, rect.getPerimeter(), "Perimeter should be 28.0");
}
@Test
void testNegativeWidth() {
Rectangle rect = new Rectangle();
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
rect.setWidth(-5.0);
});
assertEquals("Width cannot be negative.", exception.getMessage());
}
@Test
void testNegativeHeight() {
Rectangle rect = new Rectangle();
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
rect.setHeight(-3.0);
});
assertEquals("Height cannot be negative.", exception.getMessage());
}
@Test
void testNegativeValuesInConstructor() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
new Rectangle(-5.0, -3.0);
});
assertEquals("Width and height cannot be negative.", exception.getMessage());
}
}