'@MockBean service field is always null in a Test class with @WebMvcTest
I'm trying to improve a unit test for a service I'm developing. I read somewhere that it's ideal to use @WebMvcTest
annotation over @SpringBootTest
when testing the Web or Controller layer.
However, for some reason, a @MockBean
service field I am using in the Test class is always NULL
.
java.lang.AssertionError: Expecting actual not to be null
In the test class below, serviceIsLoaded()
method is always null when I run that single test.
SectionRESTControllerTest.java
@WebMvcTest
public class SectionRESTControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private SectionServiceImpl sectionServiceImpl;
@Test
public void serviceIsLoaded() {
assertThat(sectionServiceImpl).isNotNull();
}
}
SectionServiceImpl.java
@Service
public class SectionServiceImpl implements SectionService {
private final Logger logger = LoggerFactory.getLogger(SectionServiceImpl.class);
@Autowired
private SectionRepository sectionRepo; //saves to section table
@Autowired
private GradeLevelRepository gradeLevelRepo;
@Override
public Section createSection(Section section) {...}
@Override
public Section updateSection(Section request) throws Exception {...}
@Override
public Section getSectionById(Long id) {
return sectionRepo.findById(id).get();
}
@Override
public List<Section> getAllActiveSections() {
return sectionRepo.findSectionByIsActiveTrue();
}
@Override
public List<Section> getAllInActiveSections() {
return sectionRepo.findSectionByIsActiveFalse();
}
@Override
public List<Section> getSectionsByGradeLevelId(Long id) {
return sectionRepo.findByGradeLevelId(id);
}
@Override
public List<Section> getSectionByGradeLevelCode(String code) {
return sectionRepo.findByGradeLevelCode(code);
}
@Override
public List<Section> getSectionByGradeLevelCategory(String category) {
return sectionRepo.findByGradeLevelCategory(category);
}
}
My understanding is, with @WebMvcTest
, it does not load the entire application context with all managed beans which makes the UnitTest run faster. Unlike, @SpringBootTest
which loads everything when running the UnitTest.
SectionService.java
public interface SectionService {
Section createSection(Section section);
Section updateSection(Section section) throws Exception;
Section getSectionById(Long id);
List<Section> getAllActiveSections();
List<Section> getAllInActiveSections();
List<Section> getSectionsByGradeLevelId(Long id);
List<Section> getSectionByGradeLevelCode(String code);
List<Section> getSectionByGradeLevelCategory(String category);
}
Main class
@SpringBootApplication
public class AutoformSettingsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AutoformSettingsServiceApplication.class, args);
}
}
Do you have any thoughts or ideas why the @MockBean sectionServiceImpl is null
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|