百里香测试案例弹簧靴

时间:2018-06-07 12:40:59

标签: spring spring-mvc spring-boot thymeleaf

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ShopControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private UserService userService;


    @Test
    public void showRegistrationForm() throws Exception {
        String uuid = UUID.randomUUID().toString();
        when(userService.showRegistrationForm(uuid)).thenReturn(new UserRegistration());
        this.mockMvc.perform(get("/user/registration/"+uuid))
                .andExpect(status().isOk()).andExpect(view()
                .name("registrationForm"))
                .andExpect(model().attributeExists("data"))
                .andExpect(model().attributeExists("reqDto"))
                .andExpect(model().attributeExists(uuid));
    }

}

我写了这个单元测试来测试百里叶模板。我无法运行它,因为生成的UUID在数据库中不存在。

这是控制器:

@RequestMapping(value = "/registration/{uuid}")
public String showRegistrationForm(@PathVariable String uuid, Model model) {
    try {
        UserRegistration UserRegistration = this.userService.showRegistrationForm(uuid);
        model.addAttribute("user", true);
        model.addAttribute("uuid", uuid);
        model.addAttribute("reqDto", new RequestDto());
    } catch (UrlExpiredException e) {
        System.out.println("UrlExpiredException");
        model.addAttribute("exception", e.getMessage());
    } catch (UrlNotFoundException e) {
        System.out.println("UrlNotFoundException");
        model.addAttribute("exception", e.getMessage());
    }
    return "registrationForm";
}

这是在pom.xml

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>

我错过了什么?

0 个答案:

没有答案