|
| 1 | +package com.baeldung.storingXYcoordinates; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import java.awt.geom.Point2D; |
| 6 | + |
| 7 | +class XYCoordinatesUnitTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + void givenAPoint_whenUsingGetter_thenPointReturnsCoordinatesCorrectly() { |
| 11 | + // Given a point with coordinates (10.0, 20.5) |
| 12 | + Point point = new Point(10.0, 20.5); |
| 13 | + |
| 14 | + // Then its getters should return the correct values |
| 15 | + assertEquals(10.0, point.getX(), "X coordinate should be 10.0"); |
| 16 | + assertEquals(20.5, point.getY(), "Y coordinate should be 20.5"); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + void givenTwoPointsWithSameCoordinates_whenComparedForEquality_thenShouldBeEqual() { |
| 21 | + Point point1 = new Point(5.1, -3.5); |
| 22 | + Point point2 = new Point(5.1, -3.5); |
| 23 | + |
| 24 | + assertEquals(point1, point2, "Points with same coordinates should be equal"); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void givenAPointRecord_whenUsingAccessorMethods_thenRecordReturnsCoordinatesCorrectly() { |
| 29 | + // Given a record with coordinates (30.5, 40.0) |
| 30 | + PointRecord point = new PointRecord(30.5, 40.0); |
| 31 | + |
| 32 | + // Then its accessor methods should return the correct values |
| 33 | + assertEquals(30.5, point.x(), "X coordinate should be 30.5"); |
| 34 | + assertEquals(40.0, point.y(), "Y coordinate should be 40.0"); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void givenTwoRecordsWithSameCoordinates_whenComparedForEquality_thenShouldBeEqual() { |
| 39 | + PointRecord point1 = new PointRecord(7.0, 8.5); |
| 40 | + PointRecord point2 = new PointRecord(7.0, 8.5); |
| 41 | + |
| 42 | + assertEquals(point1, point2, "Records with same coordinates should be equal"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void givenAnAWTPoint_whenAccessingItsFieldsAndGetters_thenReturnsCoordinatesCorrectly() { |
| 47 | + // Given an AWT Point |
| 48 | + Point2D.Double point = new Point2D.Double(10.5, 20.5); |
| 49 | + |
| 50 | + // Then its public fields should hold the correct values |
| 51 | + assertEquals(10.5, point.x); |
| 52 | + assertEquals(20.5, point.y); |
| 53 | + |
| 54 | + // And its getters should also work |
| 55 | + assertEquals(10.5, point.getX()); |
| 56 | + assertEquals(20.5, point.getY()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void givenAnAWTPointForIntegerCoordinates_whenAccessingItsFieldsAndGetters_thenReturnsCoordinatesCorrectly() { |
| 61 | + // Given an AWT Point |
| 62 | + java.awt.Point point = new java.awt.Point(50, 60); |
| 63 | + |
| 64 | + // Then its public fields should hold the correct values |
| 65 | + assertEquals(50, point.x); |
| 66 | + assertEquals(60, point.y); |
| 67 | + |
| 68 | + // And its getters should also work |
| 69 | + assertEquals(50, point.getX()); |
| 70 | + assertEquals(60, point.getY()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void givenArrayOfCoordinates_whenAccessingArrayIndices_thenReturnsCoordinatesAtCorrectIndices() { |
| 75 | + // Given an array representing coordinates (15.0, 25.0) |
| 76 | + double[] coordinates = new double[2]; |
| 77 | + coordinates[0] = 15.0; // X |
| 78 | + coordinates[1] = 25.0; // Y |
| 79 | + |
| 80 | + // Then index 0 should be X and index 1 should be Y |
| 81 | + assertEquals(15.0, coordinates[0], "Index 0 should be the X coordinate"); |
| 82 | + assertEquals(25.0, coordinates[1], "Index 1 should be the Y coordinate"); |
| 83 | + } |
| 84 | +} |
0 commit comments