Having upgraded an existing SB app from Java 11, Spring 5, SB 2 to Java 17, Spring 6, SB 3 i have an issue i simply cant figure out with the tests. We have a controller which contains a bunch of @ModelAttrbite annotations such as :
@ModelAttribute("cdnBucketPath")
public Message<String> getCDNBucketPath() {
return new GenericMessage<>(CDN_BUCKET_PATH.getValue());
}
We then have a test class configured like this :
@RunWith(SpringRunner.class)
@ContextConfiguration
@WebAppConfiguration
@EnableWebSecurity
@SpringBootTest(classes = TestConfig.class)
public class LoginControllerTest extends AbstractControllerTest {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(document(
"{method-name}/{step}/",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())))
.build();
}
...
}
We have a series of tests such as :
@Test
@WithStringUserPrincipal
@WithMockUser
public void getSession_UserAuthPresent() throws Exception {
MvcResult result = mvc.perform(get(LOGIN_ENDPOINT)
.with(admin())
.with(csrf()))
.andExpect(status().is3xxRedirection())
.andReturn();
ModelAndView modelAndView = result.getModelAndView();
assertEquals("redirect:http://localhost:7012/", modelAndView.getViewName());
Message<String> loginUrl = (Message<String>) modelAndView.getModel().get("loginUrl");
assertEquals(MOCK_IDP_LOGIN_URL, loginUrl.getPayload());
}
What we're seeing since upgrading is that the Model in the ModelAndView is not getting populated with the various @ModelAttribute values in the controller above.
I know the test is issuing the controller request just fine so, as far as i can see from docs, the request should trigger the addition of the model attributes. Is there something extra i need to do (or do differently) having upgraded?
Thanks very much
Martin