vendredi 22 juillet 2016

@ModelAttribute does not have access to @Injected services during unit-test

I have a PageController with injected services and would like to create a ModelAttribute with an annotated function.

public abstract class BasePageController {

@Inject
private MyService myService;

@ModelAttribute("myAttribute")
private String addMyAttributeToModel() {
  myService.myFunction();
}

@ModelAttribute
public void postHandle(Map<String, Object> model, HttpServletRequest request) throws Exception {
  //addMyAttributeToModel();
}

....

This works perfectly when running the application. But for some reason, the injected services is always null, wenn I run my unit-tests. If I call it through the postHandle()-method, everything is fine again during test as well.

My test class looks like this:

@ActiveProfiles(profiles = SPRING_PROFILE_INTEGRATIONTEST, resolver =  SysPropsFirstActiveProfilesResolver.class)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
@SharedDriver(type = SharedDriver.SharedType.ONCE)
public class IntegrationTest extends FluentTest {
...
}

Does anyone have an idea? Thanks.

Aucun commentaire:

Enregistrer un commentaire