gpt4 book ai didi

在测试 Controller 方法时,Spring MVC 4.2.6 版似乎没有将模拟服务注入(inject) Controller

转载 作者:行者123 更新时间:2023-12-01 06:06:44 25 4
gpt4 key购买 nike

我真的搜索并遵循了为 Spring MVC Controller 创建单元测试类的步骤,但是单元测试运行时带有绿色通过标志,但框架使用原始服务类并调用数据库。我 mock 了类并使用了@InjectMocks连同 MockitoAnnotations.initMocks(this) .仍然在测试运行时, Controller 使用原始服务对象而不是模拟对象。如果有人可以在这方面帮助我,我真的很感激。

这里是 UserManager (服务等级),UserRegisterController ( Controller ),TestUserRegisterController (测试类)类与Eclipse包结构的图片

服务 :

 @Service
public class UserManager {

protected Map<String, String> getAllCertificates() {

Map<String, String> allCertificates = new HashMap<String, String>();
//call to database
return allCertificates;
}




protected User getUser(int userId) {
//create session
User user = session.get(User.class, userId);
//close session
return user;
}

}

Controller :
 @Controller
public class UserRegisterController {

@Autowired
private UserManager manager;

@InitBinder
public void initBinder(WebDataBinder binder) {
//do some work

}

@RequestMapping(value = "/user.html", method = RequestMethod.GET)
public ModelAndView getUser(@RequestParam(value="userId", defaultValue="-1") String userId) {
User user1;
user1 = this.manager.getUser(Integer.parseInt(userId));
if (user1 == null) {
user1 = new User();
}

ModelAndView view = new ModelAndView("User", "user1", user1);
view.addObject("allCertificatesMap", this.manager.getAllCertificates());
return view;
}

@ModelAttribute
public void setModelAttribute(Model model) {
model.addAttribute("PageHeader", "lable.pageHeader");

}

}

测试类:
   @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("test-spring-dispatcher-servlet.xml")
@WebAppConfiguration
public class TestUserRegisterController {

@Mock
private UserManager userManager;

@InjectMocks
private UserRegisterController userRegisterController;

@Autowired
private WebApplicationContext wac;

private MockMvc mockMvc;

@Before
public void setUp() throws Exception {
// Process mock annotations
MockitoAnnotations.initMocks(this);

User user2 = new User();
user2.setUserId(10006);
user2.setUserName("Reza");
user2.setHobby("Quadcopter");
user2.setPhone("4032376295");

when(this.userManager.getUser(10006)).thenReturn(user2);
when(this.userManager.getAllCertificates()).thenReturn(new HashMap<String, String>());

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

}

@Test
public void getUser() {
try {

this.mockMvc.perform(get("/user.html").param("userId", "10006"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/jsp/User.jsp"))
.andExpect(MockMvcResultMatchers.view().name("User"))
.andExpect(model().attributeExists("allCertificatesMap"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Package hierarchy

最佳答案

使用@RunWith(MockitoJUnitRunner.class)获取 @InjectMocks和其他注释工作

关于在测试 Controller 方法时,Spring MVC 4.2.6 版似乎没有将模拟服务注入(inject) Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37932637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com