【问题标题】:Running tests with Spring Boot使用 Spring Boot 运行测试
【发布时间】:2016-12-22 19:58:14
【问题描述】:

所以我正在尝试测试我编写的 Spring boot MVC 应用程序:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PetClinicApplication.class)
@WebAppConfiguration
public class OwnerControllerTests {
    @Mock
    private OwnerService ownerService;

    @InjectMocks
    private OwnerController ownerController;

    private MockMvc mockMvc;

    public void setup(){
        MockitoAnnotations.initMocks(this);

        mockMvc = MockMvcBuilders.standaloneSetup(ownerController).build();
    }

    @Test
    public void testOwnerList() throws Exception{
        List<Owner> owners = new ArrayList<>();
        owners.add(new Owner());
        owners.add(new Owner());

        when(ownerService.getAll()).thenReturn((List<Owner>) owners);

        mockMvc.perform(get("/ownerList"))
            .andExpect(status().isOk())
            .andExpect(view().name("ownerList"))
            .andExpect(model().attribute("ownerList", List.class));

    }

}

我已经开始排队了

when(ownerService.getAll()).thenReturn((List<Owner>) owners);

在调试器模式下 ownerService=null 这是 OwnerService.class

@Transactional
public Collection<Owner> getAll() {
    return ownerDao.getAll();
}

这个方法应该返回 Owner.class 对象的列表

所有者控制器sn-p

@Controller
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @RequestMapping("/addOwner")
    public String addOwner(Model model) {
        model.addAttribute("Owner", new Owner());
        return "addOwner";
    }

    @RequestMapping(value = "addOwner.do", method = RequestMethod.POST)
    public String addOwnerDo(@Valid @ModelAttribute(value = "Owner") Owner owner, BindingResult result) {

        if (result.hasErrors())
            return "addOwner";
        ObjectBinder.bind(owner);
        ownerService.add(owner);
        return "redirect:addOwner";
    }

    @RequestMapping("/ownerList")
    public String ownerList(Model model) {
        model.addAttribute("ownerList", ownerService.getAll());
        return "ownerList";
    }

    @RequestMapping("/ownerList/{id}")
    public String ownerDetails(@PathVariable(value = "id") int id, Model model) {
        Owner owner = ownerService.get(id);
        model.addAttribute("owner", owner);
        return "ownerDetail";
    }

    // to refactor
    @RequestMapping(value = "/ownerList/{id}.do")
    public String ownerDetailsDo(@ModelAttribute(value = "owner") Owner owner, BindingResult result,
            @RequestParam(value = "action") String action, Model model) {


        switch (action) {
        case "update":
            ObjectBinder.bind(owner);
            ownerService.update(owner);
            return "ownerDetail";
        case "delete":
            ownerService.remove(owner.getId());
            model.addAttribute("ownerList", ownerService.getAll());
            return "ownerList";
        }
        model.addAttribute("owner", owner);
        return "ownerDetail";
    }



}

【问题讨论】:

  • 向我们展示 OwnerController
  • 请注意,您现在应该支持 mockito JUnit 规则,而不是调用 MockitoAnnotations.initMocks(this);,它会对 mockito 模拟执行更多检查。

标签: java spring junit spring-boot mockito


【解决方案1】:

您忘记使用 @Before 注释您的设置方法,这样 mockito 就不会创建和注入模拟,试试这个:

@Before
public void setup(){
   ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-10
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2017-02-20
    • 2020-07-28
    相关资源
    最近更新 更多