无法使用 @WebMvcTest 自动装配 MockMvc [关闭]

2024-04-13

我正在尝试在 Spring Boot 应用程序中测试我的 REST 控制器。这是我的测试课

import com.binar.kelompok3.secondhand.repository.OffersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

@WebMvcTest(HistoryController.class)
public class HistoryControllerTest {

    @MockBean
    private OffersRepository offersRepository;

    @Autowired
    private MockMvc mockMvc;    

    @Test
    void signIn() throws Exception{
        mockMvc.perform(get())
    }
}

当我把@Autowired在 Intellij 给我的 MockMvc 字段上Could not autowire. No beans of 'MockMvc' type foundmockMvc.perform方法不起作用。 我尝试过改变@WebMvcTest to

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc

但Intellij仍然给了我

Could not autowire. No beans of 'MockMvc' type found

当我将光标悬停在mockMvc

如何自动装配 MockMvc?谢谢


我只需要将 Intellij 更新到 20022.1.3

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用 @WebMvcTest 自动装配 MockMvc [关闭] 的相关文章

随机推荐