I have a problem testing controller with injection based on Provider pattern. In my controller I have this bean injected @Autowired private Provider<GlobalSettingsService> globalSettingsServiceProvider; where GlobalSettingsService is request scope service. This is why Provider is used here. Its working fine in application itself, but globalSettingsServiceProvider becomes empty when I test this controller by code below. Other @Autowired beans work fine, but those with Provider are not. Any idea how to test this correctly?
Test code looks like this:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@ActiveProfiles({"test"})
public class StaticWebTests {
private MockMvc mockMvc;
@InjectMocks
HomePageController homeController;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
this.mockMvc=standaloneSetup(homeController, pagesController).build();
}
@Test
public void testHomepage() throws Exception {
mockMvc.perform(get("/")).andExpect(status().isOk());
}
}
Aucun commentaire:
Enregistrer un commentaire