gpt4 book ai didi

java - 无法使用@WebMvcTest Autowiring MockMvc

转载 作者:行者123 更新时间:2023-12-05 02:27:30 25 4
gpt4 key购买 nike

<分区>

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

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 放在 MockMvc 字段上时,Intellij 会给我 Could not autowire。未找到“MockMvc”类型的 beans 并且 mockMvc.perform 方法不起作用。我试过将 @WebMvcTest 更改为

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc

但是Intellij还是给我

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

当我将光标悬停在 mockMvc 上时

如何 Autowiring MockMvc?谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com