这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.resolvingjunitconstructorerror;

public class ResolvingJUnitConstructorError {

public int square(int a) {
return a * a;
}
}
Original file line number Diff line number Diff line change
@@ -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<Object[]> data() {
return Arrays.asList(new Object[][]{{2}, {3}, {4}});
}

@Test
public void givenNumber_whenSquare_thenReturnsCorrectResult() {
assertEquals(input * input, service.square(input));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}