【发布时间】:2020-11-11 09:20:10
【问题描述】:
这就是当前对错误的描述。
说明:
com.N2O2.Nitrouz_Studioz.controller.MainController 中的字段 profileDoa 需要一个无法找到的“com.N2O2.Nitrouz_Studioz.model.profile.ProfileDoa”类型的 bean。注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
考虑在您的配置中定义一个“com.N2O2.Nitrouz_Studioz.model.profile.ProfileDoa”类型的 bean。
我有点不明白为什么会发生这个错误,因为我最近才使用 Java Spring Boot 并且仍然习惯于使用 Beans。我已经在 Test 类中自动装配了 Bean,但它仍然抛出同样的错误。
这是我的测试类以及 Controller 和 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);
}
对此的任何帮助都会有所帮助。谢谢。
【问题讨论】:
标签: java spring-boot unit-testing junit spring-test