Skip to content

Commit 58f790b

Browse files
BAEL-9429: Resolving JUnit Constructor Error (#18935)
1 parent 0ff1726 commit 58f790b

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.resolvingjunitconstructorerror;
2+
3+
public class ResolvingJUnitConstructorError {
4+
5+
public int square(int a) {
6+
return a * a;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.resolvingjunitconstructorerror;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
import static org.junit.Assert.assertEquals;
9+
10+
@RunWith(Parameterized.class)
11+
public class ResolvingJUnitConstructorErrorJUnit4UnitTest {
12+
13+
private final int input;
14+
private final ResolvingJUnitConstructorError service = new ResolvingJUnitConstructorError();
15+
16+
public ResolvingJUnitConstructorErrorJUnit4UnitTest(int input) {
17+
this.input = input;
18+
}
19+
20+
@Parameterized.Parameters
21+
public static Collection<Object[]> data() {
22+
return Arrays.asList(new Object[][]{{2}, {3}, {4}});
23+
}
24+
25+
@Test
26+
public void givenNumber_whenSquare_thenReturnsCorrectResult() {
27+
assertEquals(input * input, service.square(input));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.resolvingjunitconstructorerror;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.ValueSource;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class ResolvingJUnitConstructorErrorJUnit5UnitTest {
8+
9+
private final ResolvingJUnitConstructorError service = new ResolvingJUnitConstructorError();
10+
11+
@ParameterizedTest
12+
@ValueSource(ints = {2, 3, 4})
13+
void givenNumber_whenSquare_thenReturnsCorrectResult(int input) {
14+
assertEquals(input * input, service.square(input));
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.resolvingjunitconstructorerror;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class ResolvingJUnitConstructorErrorNoConstructorJUnit4UnitTest {
8+
9+
private ResolvingJUnitConstructorError service;
10+
private int input;
11+
12+
@Before
13+
public void setUp() {
14+
service = new ResolvingJUnitConstructorError();
15+
input = 2;
16+
}
17+
18+
@Test
19+
public void givenNumber_whenSquare_thenReturnsCorrectResult() {
20+
assertEquals(input * input, service.square(input));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.resolvingjunitconstructorerror;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class ResolvingJUnitConstructorErrorNoConstructorJUnit5UnitTest {
8+
9+
private ResolvingJUnitConstructorError service;
10+
private int input;
11+
12+
@BeforeEach
13+
void setUp() {
14+
service = new ResolvingJUnitConstructorError();
15+
input = 2;
16+
}
17+
18+
@Test
19+
void givenNumber_whenSquare_thenReturnsCorrectResult() {
20+
assertEquals(input * input, service.square(input));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.resolvingjunitconstructorerror;
2+
3+
import org.junit.Ignore;
4+
import org.junit.Test;
5+
import static org.junit.Assert.assertEquals;
6+
7+
@Ignore("Demonstrates JUnit constructor error")
8+
public class ResolvingJUnitConstructorErrorReproduceUnitTest {
9+
10+
private int input;
11+
12+
public ResolvingJUnitConstructorErrorReproduceUnitTest(int input) {
13+
this.input = input;
14+
}
15+
16+
@Test
17+
public void givenNumber_whenSquare_thenReturnsCorrectResult() {
18+
ResolvingJUnitConstructorError service = new ResolvingJUnitConstructorError();
19+
assertEquals(input * input, service.square(input));
20+
}
21+
}

0 commit comments

Comments
 (0)