From 6f7a0eca8a2480d8f39f6f31832b123e5ee257b7 Mon Sep 17 00:00:00 2001 From: samuelnjoki29 Date: Tue, 11 Nov 2025 16:04:57 +0300 Subject: [PATCH] BAEL-9429: Resolving JUnit Constructor Error --- .../ResolvingJUnitConstructorError.java | 8 +++++ ...ngJUnitConstructorErrorJUnit4UnitTest.java | 29 +++++++++++++++++++ ...ngJUnitConstructorErrorJUnit5UnitTest.java | 16 ++++++++++ ...uctorErrorNoConstructorJUnit4UnitTest.java | 22 ++++++++++++++ ...uctorErrorNoConstructorJUnit5UnitTest.java | 22 ++++++++++++++ ...UnitConstructorErrorReproduceUnitTest.java | 21 ++++++++++++++ 6 files changed, 118 insertions(+) create mode 100644 testing-modules/junit-5/src/main/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorError.java create mode 100644 testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorJUnit4UnitTest.java create mode 100644 testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorJUnit5UnitTest.java create mode 100644 testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorNoConstructorJUnit4UnitTest.java create mode 100644 testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorNoConstructorJUnit5UnitTest.java create mode 100644 testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorReproduceUnitTest.java diff --git a/testing-modules/junit-5/src/main/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorError.java b/testing-modules/junit-5/src/main/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorError.java new file mode 100644 index 000000000000..6aa2bb5a7567 --- /dev/null +++ b/testing-modules/junit-5/src/main/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorError.java @@ -0,0 +1,8 @@ +package com.baeldung.resolvingjunitconstructorerror; + +public class ResolvingJUnitConstructorError { + + public int square(int a) { + return a * a; + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorJUnit4UnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorJUnit4UnitTest.java new file mode 100644 index 000000000000..c41ad7ad89ba --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorJUnit4UnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.resolvingjunitconstructorerror; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import java.util.Arrays; +import java.util.Collection; +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class ResolvingJUnitConstructorErrorJUnit4UnitTest { + + private final int input; + private final ResolvingJUnitConstructorError service = new ResolvingJUnitConstructorError(); + + public ResolvingJUnitConstructorErrorJUnit4UnitTest(int input) { + this.input = input; + } + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{{2}, {3}, {4}}); + } + + @Test + public void givenNumber_whenSquare_thenReturnsCorrectResult() { + assertEquals(input * input, service.square(input)); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorJUnit5UnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorJUnit5UnitTest.java new file mode 100644 index 000000000000..adab3a87bbad --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorJUnit5UnitTest.java @@ -0,0 +1,16 @@ +package com.baeldung.resolvingjunitconstructorerror; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ResolvingJUnitConstructorErrorJUnit5UnitTest { + + private final ResolvingJUnitConstructorError service = new ResolvingJUnitConstructorError(); + + @ParameterizedTest + @ValueSource(ints = {2, 3, 4}) + void givenNumber_whenSquare_thenReturnsCorrectResult(int input) { + assertEquals(input * input, service.square(input)); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorNoConstructorJUnit4UnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorNoConstructorJUnit4UnitTest.java new file mode 100644 index 000000000000..a1bd2f5aa56c --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorNoConstructorJUnit4UnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.resolvingjunitconstructorerror; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class ResolvingJUnitConstructorErrorNoConstructorJUnit4UnitTest { + + private ResolvingJUnitConstructorError service; + private int input; + + @Before + public void setUp() { + service = new ResolvingJUnitConstructorError(); + input = 2; + } + + @Test + public void givenNumber_whenSquare_thenReturnsCorrectResult() { + assertEquals(input * input, service.square(input)); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorNoConstructorJUnit5UnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorNoConstructorJUnit5UnitTest.java new file mode 100644 index 000000000000..9a17cc885593 --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorNoConstructorJUnit5UnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.resolvingjunitconstructorerror; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ResolvingJUnitConstructorErrorNoConstructorJUnit5UnitTest { + + private ResolvingJUnitConstructorError service; + private int input; + + @BeforeEach + void setUp() { + service = new ResolvingJUnitConstructorError(); + input = 2; + } + + @Test + void givenNumber_whenSquare_thenReturnsCorrectResult() { + assertEquals(input * input, service.square(input)); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorReproduceUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorReproduceUnitTest.java new file mode 100644 index 000000000000..7ff13848805b --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/resolvingjunitconstructorerror/ResolvingJUnitConstructorErrorReproduceUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.resolvingjunitconstructorerror; + +import org.junit.Ignore; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +@Ignore("Demonstrates JUnit constructor error") +public class ResolvingJUnitConstructorErrorReproduceUnitTest { + + private int input; + + public ResolvingJUnitConstructorErrorReproduceUnitTest(int input) { + this.input = input; + } + + @Test + public void givenNumber_whenSquare_thenReturnsCorrectResult() { + ResolvingJUnitConstructorError service = new ResolvingJUnitConstructorError(); + assertEquals(input * input, service.square(input)); + } +}