Junit 测试用例与 Mockito 和 Spring MVC 一起下降
Junit test case falling with Mockito and Spring MVC
我有一个控制器 Class,我正在尝试使用 mockito 对其进行测试。
@Controller
public class LoginController {
final static Logger logger = Logger.getLogger(LoginController.class);
private StaffService staffService;
@Autowired
public LoginController(StaffService staffService) {
this.staffService = staffService;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String showLoginPage(Model model) {
model.addAttribute("loginBean", new LoginBean());
return "login";
}}
我的测试Class如下所示:-
@RunWith(MockitoJUnitRunner.class)
public class LoginControllerTest {
private static MockMvc mockMvc;
@Mock
private static LoginController loginController;
@Before
public void setUp() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
mockMvc = MockMvcBuilders.standaloneSetup(loginController).setViewResolvers(viewResolver).build();
}
@Test
public void testShowLoginPage() throws Exception {
loginController.showLoginPage(new ExtendedModelMap());
mockMvc.perform(get("/")).andExpect(view().name("login"));
}
}
但是当我 运行 测试时 class 我收到以下错误。请帮忙!
java.lang.AssertionError: View name expected:<login> but was:<>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
at org.springframework.test.web.servlet.result.ViewResultMatchers.match(ViewResultMatchers.java:69)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:171)
at com.expensecalculator.controller.test.LoginControllerTest.testShowLoginPage(LoginControllerTest.java:38)
这可能是因为您正在模拟您正在尝试测试的控制器。你应该试试这个:
@RunWith(MockitoJUnitRunner.class)
public class LoginControllerTest {
private MockMvc mockMvc;
private static LoginController loginController;
@Mock
private StaffService staffService;
@Before
public void setUp() {
loginController = new LoginController(staffService);
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
mockMvc = MockMvcBuilders.standaloneSetup(loginController).setViewResolvers(viewResolver).build();
}
@Test
public void testShowLoginPage() throws Exception {
mockMvc.perform(get("/")).andExpect(view().name("login"));
}
}
Mockito 的 mock 将其所有方法调用替换为默认行为,因此当您的独立实现到达该方法时,它只是 return 一个空字符串。
我有一个控制器 Class,我正在尝试使用 mockito 对其进行测试。
@Controller
public class LoginController {
final static Logger logger = Logger.getLogger(LoginController.class);
private StaffService staffService;
@Autowired
public LoginController(StaffService staffService) {
this.staffService = staffService;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String showLoginPage(Model model) {
model.addAttribute("loginBean", new LoginBean());
return "login";
}}
我的测试Class如下所示:-
@RunWith(MockitoJUnitRunner.class)
public class LoginControllerTest {
private static MockMvc mockMvc;
@Mock
private static LoginController loginController;
@Before
public void setUp() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
mockMvc = MockMvcBuilders.standaloneSetup(loginController).setViewResolvers(viewResolver).build();
}
@Test
public void testShowLoginPage() throws Exception {
loginController.showLoginPage(new ExtendedModelMap());
mockMvc.perform(get("/")).andExpect(view().name("login"));
}
}
但是当我 运行 测试时 class 我收到以下错误。请帮忙!
java.lang.AssertionError: View name expected:<login> but was:<>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
at org.springframework.test.web.servlet.result.ViewResultMatchers.match(ViewResultMatchers.java:69)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:171)
at com.expensecalculator.controller.test.LoginControllerTest.testShowLoginPage(LoginControllerTest.java:38)
这可能是因为您正在模拟您正在尝试测试的控制器。你应该试试这个:
@RunWith(MockitoJUnitRunner.class)
public class LoginControllerTest {
private MockMvc mockMvc;
private static LoginController loginController;
@Mock
private StaffService staffService;
@Before
public void setUp() {
loginController = new LoginController(staffService);
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
mockMvc = MockMvcBuilders.standaloneSetup(loginController).setViewResolvers(viewResolver).build();
}
@Test
public void testShowLoginPage() throws Exception {
mockMvc.perform(get("/")).andExpect(view().name("login"));
}
}
Mockito 的 mock 将其所有方法调用替换为默认行为,因此当您的独立实现到达该方法时,它只是 return 一个空字符串。