MockBean in Spring Boot with Cucumber results in NullPointerException

I am testing with Spring Boot (3.3.0) and Cucumber (7.18.0). I have a repository which I want to mock and I have included the necessary (I think) configuration for Spring and Cucumber but I get
“java.lang.NullPointerException: Cannot invoke “com.example.sampleproject.repository.ArtistRepository.findById(Object)” because “this.artistRepository” is null”.

Here is my step def class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@SpringBootTest
@ContextConfiguration(classes = {SampleProjectApplication.class, CucumberSpringConfiguration.class})
public class FindArtistByIdStepDefs {
@MockBean
private ArtistRepository artistRepository;
private DataLoaderService dataLoaderService;
private List<ArtistEntity> mockedArtists = new ArrayList<>();
private List<AlbumEntity> mockedAlbums = new ArrayList<>();
public FindArtistByIdStepDefs(DataLoaderService dataLoaderService) {
this.dataLoaderService = dataLoaderService;
}
@Given("artists exist in the DB")
public void theFollowingArtistsExistInTheDB(DataTable dataTable) {
List<Map<String, String>> map = dataTable.asMaps(String.class, String.class);
for (Map<String, String> 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));
}
</code>
<code>@SpringBootTest @ContextConfiguration(classes = {SampleProjectApplication.class, CucumberSpringConfiguration.class}) public class FindArtistByIdStepDefs { @MockBean private ArtistRepository artistRepository; private DataLoaderService dataLoaderService; private List<ArtistEntity> mockedArtists = new ArrayList<>(); private List<AlbumEntity> mockedAlbums = new ArrayList<>(); public FindArtistByIdStepDefs(DataLoaderService dataLoaderService) { this.dataLoaderService = dataLoaderService; } @Given("artists exist in the DB") public void theFollowingArtistsExistInTheDB(DataTable dataTable) { List<Map<String, String>> map = dataTable.asMaps(String.class, String.class); for (Map<String, String> 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)); } </code>
@SpringBootTest
@ContextConfiguration(classes = {SampleProjectApplication.class, CucumberSpringConfiguration.class})
public class FindArtistByIdStepDefs {

    @MockBean
    private ArtistRepository artistRepository;
    private DataLoaderService dataLoaderService;
    

    private List<ArtistEntity> mockedArtists = new ArrayList<>();
    private List<AlbumEntity> mockedAlbums = new ArrayList<>();

    public FindArtistByIdStepDefs(DataLoaderService dataLoaderService) {
        this.dataLoaderService = dataLoaderService;
    }

    @Given("artists exist in the DB")
    public void theFollowingArtistsExistInTheDB(DataTable dataTable) {
        List<Map<String, String>> map = dataTable.asMaps(String.class, String.class);

        for (Map<String, String> 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));
        }

Then I have CucumberSpring config:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@CucumberContextConfiguration
@SpringBootTest(classes = SampleProjectApplication.class)
public class CucumberSpringConfiguration {
public CucumberSpringConfiguration() {
System.out.println("CucumberSpringConfiguration initialized");
}
}
</code>
<code>@CucumberContextConfiguration @SpringBootTest(classes = SampleProjectApplication.class) public class CucumberSpringConfiguration { public CucumberSpringConfiguration() { System.out.println("CucumberSpringConfiguration initialized"); } } </code>
@CucumberContextConfiguration
@SpringBootTest(classes = SampleProjectApplication.class)
public class CucumberSpringConfiguration {

    public CucumberSpringConfiguration() {
        System.out.println("CucumberSpringConfiguration initialized");
    }
}

Test Runner:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Suite
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.sampleproject.steps, com.example.sampleproject.config")
public class TestRunner {
}
</code>
<code>@Suite @SelectClasspathResource("features") @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.sampleproject.steps, com.example.sampleproject.config") public class TestRunner { } </code>
@Suite
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.sampleproject.steps, com.example.sampleproject.config")
public class TestRunner {
}

When I add @CucumberContextConfiguration on the FindArtistByIdStepDefs class and remove the other it works. But I want this configuration to be more centralized since I have another step def class and plan to have more.

What might be causing @MockBean to result in a NullPointerException when using a centralized configuration? How can I ensure that the MockBean is properly injected while keeping a centralized configuration?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật