我试图建立一个测试的ClienteController.java,它使用ClienteService.java的是使用ClienteRepository.java到Cliente.class
我正在使用mvn test命令运行测试。
所以我试图使用WebMvcTest将测试隔离到 web 层。我正在关注此示例,该示例无需堆栈上的存储库(测试 Web 层)
代码
客户端控制器.java
@Controller
public class ClientesController {
private final String view = "clientes";
@Autowired
ClienteService clienteService;
@GetMapping("/clientes")
public ModelAndView clientes() {
ModelAndView mv = new ModelAndView(view);
mv.addObject("listar", true);
mv.addObject("clientes", this.clienteService.listarClientes());
return mv;
}
}
客户端服务.java
@Service
public class ClienteService {
private final ClienteRepository clienteRepository;
@Autowired
public ClienteService(ClienteRepository clienteRepository) {
this.clienteRepository = clienteRepository;
}
public List<Cliente> listarClientes(){
return clienteRepository.findAll();
}
}
ClienteREpository.class
@Repository
public interface ClienteRepository extends JpaRepository<Cliente, Integer> {
@Query("SELECT c FROM Cliente c WHERE c.id = ?1")
Cliente findOne(Integer clienteId);
}
客户端控制器测试.java
@RunWith(SpringRunner.class)
@WebMvcTest(ClientesController.class)
public class ClientesControllerTest {
@Autowired
private MockMvc mockMvc;
@Mock
private ClienteRepository clienteRepository;
@Test
public void contexLoads() throws Exception {
}
}
慕码人2483693
相关分类