Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java (Java)

Code before:
         *    0  1
         * 0  0  10
         * 1  10  0
         */
        List<BoruvkaAlgorithm.Edge> result2 = BoruvkaAlgorithm.boruvkaMST(graph2);
        // Expected result:
        // Edges: 1
        // Total weight: 10
        assertEquals(1, result2.size());
        assertEquals(10, result2.stream().mapToInt(edge -> edge.weight).sum());
    }

    @Test
    void testCompleteGraphK4() {
        // Test case 3
        int V3 = 4;

        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();
        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 7));
        edges.add(new BoruvkaAlgorithm.Edge(0, 2, 2));
        edges.add(new BoruvkaAlgorithm.Edge(0, 3, 5));
        edges.add(new BoruvkaAlgorithm.Edge(1, 2, 3));
        edges.add(new BoruvkaAlgorithm.Edge(1, 3, 4));
        edges.add(new BoruvkaAlgorithm.Edge(2, 3, 1));

        Graph graph3 = new Graph(V3, edges);

        /**
         * Adjacency matrix
         *    0  1  2  3
         * 0  0  7  2  5
         * 1  7  0  3  4
         * 2  2  3  0  1
         * 3  5  4  1  0
         */

        List<BoruvkaAlgorithm.Edge> result3 = BoruvkaAlgorithm.boruvkaMST(graph3);
        // Expected result:
        // Edges: 3
        // Total weight: 6
        assertEquals(3, result3.size());
        assertEquals(6, result3.stream().mapToInt(edge -> edge.weight).sum());
    }

    @Test
    void testNegativeVertices() {
        Exception exception1 = assertThrows(IllegalArgumentException.class, () -> new Graph(-1, null));
        String expectedMessage = "Number of vertices must be positive";
        String actualMessage = exception1.getMessage();

        assertTrue(actualMessage.contains(expectedMessage));

Review comment (suggestion):
```suggestion
        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();
        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 7));
        edges.add(new BoruvkaAlgorithm.Edge(0, 2, 2));
        edges.add(new BoruvkaAlgorithm.Edge(0, 3, 5));
        edges.add(new BoruvkaAlgorithm.Edge(1, 2, 3));
        edges.add(new BoruvkaAlgorithm.Edge(1, 3, 4));
        edges.add(new BoruvkaAlgorithm.Edge(2, 3, 1));

        Graph graph3 = new Graph(4, edges);
```

Code after:
    void testCompleteGraphK4() {
        List<BoruvkaAlgorithm.Edge> edges = new ArrayList<>();
        edges.add(new BoruvkaAlgorithm.Edge(0, 1, 7));
        edges.add(new BoruvkaAlgorithm.Edge(0, 2, 2));
        edges.add(new BoruvkaAlgorithm.Edge(0, 3, 5));
        edges.add(new BoruvkaAlgorithm.Edge(1, 2, 3));
        edges.add(new BoruvkaAlgorithm.Edge(1, 3, 4));
        edges.add(new BoruvkaAlgorithm.Edge(2, 3, 1));

        final var graph = new Graph(4, edges);

        /**
         * Adjacency matrix
         *    0  1  2  3
         * 0  0  7  2  5
         * 1  7  0  3  4
         * 2  2  3  0  1
         * 3  5  4  1  0
         */
        final var result = BoruvkaAlgorithm.boruvkaMST(graph);
        assertEquals(3, result.size());
        assertEquals(6, computeTotalWeight(result));
    }

    @Test
    void testNegativeVertices() {
        Exception exception1 = assertThrows(IllegalArgumentException.class, () -> new Graph(-1, null));
        String expectedMessage = "Number of vertices must be positive";
        String actualMessage = exception1.getMessage();

        assertTrue(actualMessage.contains(expectedMessage));
    }

    @Test
    void testEdgesNull() {
        Exception exception = assertThrows(IllegalArgumentException.class, () -> new Graph(0, null));
        String expectedMessage = "Edges list must not be null or empty";
        String actualMessage = exception.getMessage();

        assertTrue(actualMessage.contains(expectedMessage));
    }

    @Test
    void testEdgesEmpty() {
        Exception exception = assertThrows(IllegalArgumentException.class, () -> new Graph(0, new ArrayList<>()));
        String expectedMessage = "Edges list must not be null or empty";
        String actualMessage = exception.getMessage();

        assertTrue(actualMessage.contains(expectedMessage));
    }