在Spring测试包中找不到java Bean类型
下面是当前对错误的描述
描述:
在com中输入字段profileDoa。N2O2。Nitrouz_Studioz。控制器。MainController需要“com”类型的bean。N2O2。Nitrouz_Studioz。模型轮廓找不到的配置文件。注入点具有以下注释: -@org。springframework。豆。工厂注释。自动连线(必需=真)
动作:
考虑定义“com”类型的bean。N2O2。Nitrouz_Studioz。模型轮廓在你的配置中配置DOA
我有点不明白为什么会发生这个错误,因为我最近才开始使用Java Spring Boot,并且仍然习惯于使用bean。我已经在测试类中自动连接了Bean,但它仍然抛出相同的错误
以下是我在测试类、控制器和ProfileDoa类中的内容
@WebMvcTest(MainController.class)
@ContextConfiguration(classes = NitrouzStudiozApplication.class)
public class MainControllerTest {
@Autowired
private MockMvc mockMvc;
ProfileEntity profileEntity;
@Autowired
private ProfileDoa profileDoa;
private MainController mainController;
@Mock
private Model model;
private boolean loggedOut = true;
private boolean loggedIn = false;
@BeforeEach
public void intializeController(){
mainController = new MainController();
}
@Test
@DisplayName("Navigating to Website Correctly Displays Index page")
public void loadsIndexPage() throws Exception {
RequestBuilder request = MockMvcRequestBuilders.get("/");
MvcResult result = mockMvc.perform(request)
.andExpect(model().attribute("loggedOut", loggedOut))
.andExpect(model().attribute("loggedIn", loggedIn))
.andExpect(model().attribute("profileEntity", "Not logged In"))
.andReturn();
Assertions.assertEquals("index", result);
}
}
@Controller
public class MainController {
private boolean loggedOut = true;
private boolean loggedIn = false;
private ProfileEntity profileEntity;
@Autowired
private ProfileDoa profileDoa;
@RequestMapping("/")
public String home_page(Model model) {
model.addAttribute("loggedOut", loggedOut);
model.addAttribute("loggedIn", loggedIn);
model.addAttribute("profileEntity", "Not logged In");
return "index";
}
@RequestMapping("/about")
public String about_page(Model model){
model.addAttribute("loggedOut", loggedOut);
model.addAttribute("loggedIn", loggedIn);
model.addAttribute("profileEntity", "Not logged In");
return "about";
}
@RequestMapping("/signup")
public String sign_up(){
return "signup";
}
@GetMapping("/signUpForm")
public String signUpForm(Model model, ProfileEntity profileEntity){
boolean checked = false;
model.addAttribute("profileEntity", profileEntity);
model.addAttribute("join", checked);
return "signUpForm";
}
@RequestMapping("/signUpFormError")
public String signUpFormError(Model model,
@ModelAttribute("error") boolean error,
@ModelAttribute("message") String message,
ProfileEntity profileEntity){
boolean checked = false;
model.addAttribute("join", checked);
model.addAttribute("error", error);
model.addAttribute("message", message);
model.addAttribute("profileEntity", profileEntity);
return "signUpForm";
}
@RequestMapping("/ForgotPasswordPage")
public String forgotPasswordPage(){
return "forgotPassword";
}
@GetMapping("/Forgot_Password")
public String ForgotPasswordResponse(){
return "forgotPassword";
}
}
@Transactional
public interface ProfileDoa extends JpaRepository<ProfileEntity, Long> {
public ProfileEntity findByEmail(String email);
}
这方面的任何帮助都会有所帮助。谢谢
# 1 楼答案
使用
@WebMvcTest
可以为web层编写测试。Spring Test将为您创建一个测试应用程序这个部分所需的所有bean的上下文:例如,用@Controller
、@RestController
、@ControllerAdvice
、过滤器等注释的类所有其他bean都不是为您创建的,因为它们不在web层的范围内。在你的例子中,这是你的
MainController
注入的任何其他bean现在基本上有两种选择:
ProfileDoa
ProfileDoa
bean。这需要一个数据库和更多的设置李>对于选项1,您可以按如下方式调整测试:
因为我在
MainController
中没有看到与profileDoa
的任何交互,所以也没有必要准备任何模拟方法响应。但是,如果您确实在某处调用了例如profileDao.findByEmail("mail@duke.io")
,您可以使用Mockito来准备结果:对于选项二,您可以使用
@SpringBootTest
和@AutoconfigureMockMvc
的组合来加载整个Spring上下文(所有bean),并利用MockMvc
:在这里,您可能希望使用例如Testcontainers来为测试启动一个数据库