第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

嘗試測試具有存儲(chǔ)庫的服務(wù)的控制器(Spring Boot)的問題

嘗試測試具有存儲(chǔ)庫的服務(wù)的控制器(Spring Boot)的問題

肥皂起泡泡 2021-09-12 10:38:42
我試圖建立一個(gè)測試的ClienteController.java,它使用ClienteService.java的是使用ClienteRepository.java到Cliente.class我正在使用mvn test命令運(yùn)行測試。所以我試圖使用WebMvcTest將測試隔離到 web 層。我正在關(guān)注此示例,該示例無需堆棧上的存儲(chǔ)庫(測試 Web 層)代碼客戶端控制器.java@Controllerpublic 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;    }}客戶端服務(wù).java@Servicepublic class ClienteService {    private final ClienteRepository clienteRepository;    @Autowired    public ClienteService(ClienteRepository clienteRepository) {        this.clienteRepository = clienteRepository;    }    public List<Cliente> listarClientes(){        return clienteRepository.findAll();    }}ClienteREpository.class@Repositorypublic 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 {    }}
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

請參閱文檔:

使用此注釋將禁用完全自動(dòng)配置,而是僅應(yīng)用與 MVC 測試相關(guān)的配置(即@Controller、@ControllerAdvice、@JsonComponent、Converter/GenericConverter、Filter、WebMvcConfigurer 和 HandlerMethodArgumentResolver bean,但不應(yīng)用 @Component、@Service 或 @Repository bean )。

特別是,@Servicebean 沒有被配置。假設(shè)僅@WebMvcTest用于測試 mvc 層,其他一切都應(yīng)該被模擬。您應(yīng)該ClienteService單獨(dú)模擬您的和測試,可能使用單元測試?;蛘撸?code>@SpringBootTest如果您想配置整個(gè)應(yīng)用程序,您可以使用,或者您可以簡單地@Import使用缺少的服務(wù)。


查看完整回答
反對 回復(fù) 2021-09-12
  • 2 回答
  • 0 關(guān)注
  • 139 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號