[英]I am learning how to do the unit tests for the controllers and i don't know how to do it correctly
I'm learning how to test controllers and I still don't understand some things.我正在学习如何测试控制器,但我仍然不明白一些事情。 I've watched some tutorials and I read some pages to learn but I don't understand 100%.
我看过一些教程,也阅读了一些页面来学习,但我没有 100% 理解。 I tried to do it but I don't think it's correct.
我试图这样做,但我认为它不正确。 I'm using Spring Boot and Mock.
我正在使用 Spring 引导和模拟。
This is my controller class:这是我的 controller class:
@Autowired
private UserDetailsServiceImpl userService;
@Autowired
private ShopService shopService;
@Autowired
private GameService gameService;
@GetMapping(value = "/userProfile")
public String userProfile(final Model model, final HttpServletRequest request) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Object principal = auth.getPrincipal();
User us = null;
if (principal instanceof User) {
us = (User) principal;
}
model.addAttribute("user", us);
model.addAttribute("teamsCreated", this.userService.teamsCreated(us));
model.addAttribute("teamshejoined", this.userService.teamsHeJoined(us));
model.addAttribute("gamesheplays", this.gameService.findGamesHePlays(us.getId()));
model.addAttribute("groupgamesheplays", this.gameService.findGroupHePlays(us));
model.addAttribute("prizehewon", this.gameService.getPrizeHeWon(us));
return "user/profile";
}
This is my test class:这是我的测试 class:
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userService;
@MockBean
private ShopService shopService;
@MockBean
private UserRepository userRepository;
@MockBean
private DataUserRepository dataUserRepository;
private User user;
private DataUser dataUser;
private Team team;
private Set<User> people;
@BeforeEach
void setUp() {
user = new User();
user.setId(12L);
user.setUsername("usertest");
user.setPassword("Pass1234");
dataUser.setId(915495L);
dataUser.setMoney(1);
dataUser.setUser(user);
user.setDataUser(dataUser);
people = new HashSet<>();
people.add(user);
team = new Team();
team.setId(2L);
team.setName("team1");
team.setCreator(user);
team.setPeople(people);
team.setCreationDate(Date.from(Instant.now()));
}
@WithMockUser(value = "spring")
@Test
void testProcessCreationUser() throws Exception{
mockMvc.perform(post("/userProfile")
.with(csrf())
.param("username", "usertest")
.param("password", "Pass1234")
.param("id", "49L")
.param("dataUser", ?????? )) // the ?? is because I dont know how to add a dataUser type, I only know Strings
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("users/createClienteForm"));
}
I tried doing it seeing previous methods some people made and with what I read before in notes I took, but i don't really understand so if you could tell me if that's correct or what is wrong and I could do better and I'd appreciate it.我试着看一些人以前的方法,以及我之前在笔记中读到的内容,但我不太明白,所以如果你能告诉我这是正确的还是错误的,我可以做得更好,我会欣赏它。 Thank you so much!
太感谢了!
First of all, you're trying to mock a @GetMapping
as a post()
and it will not work.首先,您试图将
@GetMapping
模拟为post()
并且它不起作用。
Second, it seems that your resource does not need any parameter as it only gathers information about the user that is logged in your system, am I correct?其次,您的资源似乎不需要任何参数,因为它只收集有关登录您系统的用户的信息,对吗?
So, your test should look like:因此,您的测试应如下所示:
@WithMockUser(value = "spring")
@Test
void testProcessCreationUser() throws Exception{
mockMvc.perform(get("/userProfile")
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("users/createClienteForm"));
}
You should review to which page your user will be redirected at the end, as users/createClienteForm
doesn't seem to be the corrected one in your mock.您应该查看您的用户最终将被重定向到哪个页面,因为
users/createClienteForm
在您的模拟中似乎不是正确的页面。
Also, always choose a name for your test that correctly explains what your test is doing/verifying.此外,请始终为您的测试选择一个正确解释您的测试正在做什么/验证的名称。 I think
testShowUserProfile
should be fine in your case.我认为
testShowUserProfile
在你的情况下应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.