我正在尝试对返回String的Spring Controller服务进行单元测试。我想断言期望的URL名称是正确的,但我的测试始终返回空URL。我为此使用SpringRunner,@ WebMvcTest和MockMvc。
@Slf4j
@Controller
public class CompanyInfoController {
private CompanyService companyService;
@Autowired
public CompanyInfoController(final CompanyService companyService) {
this.companyService = companyService;
}
@PreAuthorize("@someService.hasRole('" + Constants.MY_ROLE + "')")
@RequestMapping(value = "/companyInfo", method = RequestMethod.GET)
public String getCompanyInfo(final HttpServletRequest request, final
Model model) throws Exception {
log.debug("Getting Company Info");
final CompanyInfoDTO companyInformation = loadCompanyInfo(request);
model.addAttribute("companyInformation", companyInformation);
return "companyInfo";
}
private CompanyInfoDTO loadCompanyInfo(final HttpServletRequest request) throws Exception {
final Account someAccount= (Account)request.getAttribute("someAccount");
if (null != someAccount) {
final CompanyInfoDTO companyInformation = companyService.getCompanyInfo(someAccount);
return companyInformation;
} else {
throw new Exception("Unable to retrieve this account details.");
}
}
}
控制器测试-
@RunWith(SpringRunner.class)
@WebMvcTest(CompanyInfoController.class)
@Import(SecurityConfiguration.class)
public class CompanyInfoControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CompanyService companyService;
@MockBean(name = "someService")
private SomeService someAuthorizationService;
@Before
public void setUp(){
Mockito.when(someAuthorizationService.hasRole(Constants.MY_ROLE))
.thenReturn(true);
}
@Test
@WithMockUser
public void canInvokeGetCompanyInfoServiceSuccessfully() throws Exception{
CompanyInfoDTO dto = new CompanyInfoDTO();
final Account mockAccount = new Account();
mockAccount.setId("12334");
Mockito.when(companyService.getCompanyInfo(Mockito.any(Account.class)))
.thenReturn(dto);
RequestBuilder request = get("/companyInfo")
.requestAttr("someAccount", mockAccount);
mockMvc.perform(get("/companyInfo"))
.andExpect(status().isOk());
}
@Test
@WithMockUser
public void forwardsToCompanyInfoPageSuccessfully() throws Exception{
CompanyInfoDTO dto = new CompanyInfoDTO();
final Account mockAccount = new Account();
mockAccount.setId("12334");
Mockito.when(companyService.getCompanyInfo(Mockito.any(Account.class)))
.thenReturn(dto);
RequestBuilder request = get("/companyInfo")
.requestAttr("someAccount", mockAccount);
mockMvc.perform(request)
.andExpect(redirectedUrl("companyInfo"));
}
}
测试不断失败,并显示以下错误- java.lang.AssertionError:重定向的URL 预期:companyInfo 实际:null
不确定我缺少什么! 我没有看到打印控制器内部的调试语句。但是我的控制器并没有被嘲笑。我的安全模拟是正确的吗?任何帮助将不胜感激。
更新-这是我在日志中看到的-
MockHttpServletRequest:
HTTP Method = GET
Request URI = /companyInfo
Parameters = {}
Headers = []
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = org.cmt.controller.CompanyInfoController
Method = public java.lang.String org.cmt.controller.CompanyInfoController.getCompanyInfo(javax.servlet.http.HttpServletRequest,org.springframework.ui.Model) throws java.lang.Exception
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", X-Frame-Options:"SAMEORIGIN"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: No ModelAndView found
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:36)
我看到Http响应代码200。我假设它可以进行身份验证。但是为什么我的模型和视图为空?