Home > Software engineering >  Junit tests are failing after I upgrade the spring boot dependencies
Junit tests are failing after I upgrade the spring boot dependencies

Time:12-27

I have the following code that is running fine when I had spring boot dependencies 2.3.3.

spring boot 2.3.3 deps

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));
  }
}

The same test case I am running with spring boot dependencies 2.6.2 like this. The changes I have done are I am using jupiter.api.test instead of junit.test, using ExtendWith instead of RunWith, using MockitoExtension instead of MockitoJUnitRunner

I am getting nullpointer exception when converter.convert is called

spring boot 2.6.2 deps

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));
  }
}

CodePudding user response:

This is due to using the wrong maven-surefire-plugin

more at https://www.journaldev.com/21711/junit-setup-maven

junit5 tests need maven-surefire-plugin 2.22.0

  • Related