升级 spring 引导依赖项后 Junit 测试失败
Junit tests are failing after I upgrade the spring boot dependencies
当我有 spring 引导依赖项 2.3.3.
时,我有以下代码 运行 没问题
class Details {
String pin;
State state;
}
class DetailsDto {
String pin;
StateDto stateDto;
}
class State {
}
class StateDto {
}
@Named
class TestClass {
private final StateConverter converter;
@Inject
public TestClass(StateConverter converter) {
this.converter= converter;
}
public DetailsDto getDetails(Details details) {
DetailsDto dto = new DetailsDto();
dto.setPin(details.getPin());
dto.setState(converter.convert(details.getState());
}
}
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class TestClassTest {
@Mock
private StateConverter converter;
@InjectMocks
private TestClass testClass;
@Test
public final void testSomething() {
final String pin = "12345";
final State state = mock(State.class);
Details details = mock(Details.class);
given(details.getPin()).willReturn(pin);
given(details.getState()).willReturn(state);
StateDto stateDto = mock(StateDto.class);
given(converter.convert(state)).willReturn(stateDto);
DetailsDto deatilsDto = testClass.getDetails(details);
assertThat(deatilsDto.getPin(), equalTo(pin));
verify(converter).convert(state);
assertThat(deatilsDto.getState(), equalTo(stateDto));
}
}
同样的测试用例我是运行spring boot dependencies 2.6.2 是这样的。我所做的改变是
我使用 jupiter.api.test 而不是 junit.test,使用 ExtendWith 而不是 RunWith,使用 MockitoExtension 而不是 MockitoJUnitRunner
调用 converter.convert 时出现空指针异常
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class TestClassTest {
@Mock
private StateConverter converter;
@InjectMocks
private TestClass testClass;
@Test
public final void testSomething() {
final String pin = "12345";
final State state = mock(State.class);
Details details = mock(Details.class);
given(details.getPin()).willReturn(pin);
given(details.getState()).willReturn(state);
StateDto stateDto = mock(StateDto.class);
given(converter.convert(state)).willReturn(stateDto);
DetailsDto deatilsDto = testClass.getDetails(details);
assertThat(deatilsDto.getPin(), equalTo(pin));
verify(converter).convert(state);
assertThat(deatilsDto.getState(), equalTo(stateDto));
}
}
这是因为使用了错误的maven-surefire-plugin版本
更多信息在 https://www.journaldev.com/21711/junit-setup-maven
junit5 测试需要 maven-surefire-plugin 2.22.0
当我有 spring 引导依赖项 2.3.3.
时,我有以下代码 运行 没问题class Details {
String pin;
State state;
}
class DetailsDto {
String pin;
StateDto stateDto;
}
class State {
}
class StateDto {
}
@Named
class TestClass {
private final StateConverter converter;
@Inject
public TestClass(StateConverter converter) {
this.converter= converter;
}
public DetailsDto getDetails(Details details) {
DetailsDto dto = new DetailsDto();
dto.setPin(details.getPin());
dto.setState(converter.convert(details.getState());
}
}
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class TestClassTest {
@Mock
private StateConverter converter;
@InjectMocks
private TestClass testClass;
@Test
public final void testSomething() {
final String pin = "12345";
final State state = mock(State.class);
Details details = mock(Details.class);
given(details.getPin()).willReturn(pin);
given(details.getState()).willReturn(state);
StateDto stateDto = mock(StateDto.class);
given(converter.convert(state)).willReturn(stateDto);
DetailsDto deatilsDto = testClass.getDetails(details);
assertThat(deatilsDto.getPin(), equalTo(pin));
verify(converter).convert(state);
assertThat(deatilsDto.getState(), equalTo(stateDto));
}
}
同样的测试用例我是运行spring boot dependencies 2.6.2 是这样的。我所做的改变是 我使用 jupiter.api.test 而不是 junit.test,使用 ExtendWith 而不是 RunWith,使用 MockitoExtension 而不是 MockitoJUnitRunner
调用 converter.convert 时出现空指针异常
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class TestClassTest {
@Mock
private StateConverter converter;
@InjectMocks
private TestClass testClass;
@Test
public final void testSomething() {
final String pin = "12345";
final State state = mock(State.class);
Details details = mock(Details.class);
given(details.getPin()).willReturn(pin);
given(details.getState()).willReturn(state);
StateDto stateDto = mock(StateDto.class);
given(converter.convert(state)).willReturn(stateDto);
DetailsDto deatilsDto = testClass.getDetails(details);
assertThat(deatilsDto.getPin(), equalTo(pin));
verify(converter).convert(state);
assertThat(deatilsDto.getState(), equalTo(stateDto));
}
}
这是因为使用了错误的maven-surefire-plugin版本
更多信息在 https://www.journaldev.com/21711/junit-setup-maven
junit5 测试需要 maven-surefire-plugin 2.22.0