"java.lang.NullPointerException: невозможно вызвать "com.example.sampleproject.repository.ArtistRepository .findById(Object)", потому что "this.artistRepository" имеет значение null".
Вот мой класс определения шага:
Код: Выделить всё
@SpringBootTest
@ContextConfiguration(classes = {SampleProjectApplication.class, CucumberSpringConfiguration.class})
public class FindArtistByIdStepDefs {
@MockBean
private ArtistRepository artistRepository;
private DataLoaderService dataLoaderService;
private List mockedArtists = new ArrayList();
private List mockedAlbums = new ArrayList();
public FindArtistByIdStepDefs(DataLoaderService dataLoaderService) {
this.dataLoaderService = dataLoaderService;
}
@Given("artists exist in the DB")
public void theFollowingArtistsExistInTheDB(DataTable dataTable) {
List map = dataTable.asMaps(String.class, String.class);
for (Map row : map) {
ArtistEntity artist = new ArtistEntity();
String name = row.get("name");
String description = row.get("description");
long id = Long.parseLong(row.get("id"));
artist.setArtist(name);
artist.setDescription(description);
artist.setId(id);
mockedArtists.add(artist);
Mockito.when(artistRepository.findById(artist.getId())).thenReturn(Optional.of(artist));
}
Код: Выделить всё
@CucumberContextConfiguration
@SpringBootTest(classes = SampleProjectApplication.class)
public class CucumberSpringConfiguration {
public CucumberSpringConfiguration() {
System.out.println("CucumberSpringConfiguration initialized");
}
}
Код: Выделить всё
@Suite
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.sampleproject.steps, com.example.sampleproject.config")
public class TestRunner {
}
Что может привести к тому, что @MockBean приведет к возникновению исключения NullPointerException при использовании централизованной конфигурации? Как я могу гарантировать, что MockBean будет правильно внедрен, сохраняя при этом централизованную конфигурацию?
Подробнее здесь: https://stackoverflow.com/questions/786 ... rexception