簡體   English   中英

在 Spring 測試 package 中找不到 Bean 類型

[英]Bean Type not being found in in Spring Test package

因此,這是當前對錯誤的描述給我的信息。

描述:

Field profileDoa in com.N2O2.Nitrouz_Studioz.controller.MainController required a bean of type 'com.N2O2.Nitrouz_Studioz.model.profile.ProfileDoa' that could not be found. 注入點有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)

行動:

考慮在您的配置中定義“com.N2O2.Nitrouz_Studioz.model.profile.ProfileDoa”類型的 bean。

我有點不知道為什么會發生這個錯誤,因為我最近只使用 Java Spring Boot 並且仍然習慣於使用 Beans。 我已經在測試 class 中自動連接了 Bean,但它仍然拋出相同的錯誤。

這是我在測試 class 和 Controller 和 ProfileDoa class 中的內容。

@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);
}

對此的任何幫助都會有所幫助。 謝謝。

使用@WebMvcTest ,您可以為您的 web 層編寫測試。 Spring 測試將為您創建一個上下文,其中包含測試您的應用程序這一部分所需的所有 bean:例如使用 @ @RestController @Controller @ControllerAdvice ControllerAdvice、過濾器等注釋的類。

所有其他 bean 都不會為您創建,因為它們不在web 層的 scope 中。 在您的情況下,這是您的MainController注入的任何其他 bean。

你現在基本上有兩個選擇:

  1. 模擬ProfileDoa
  2. 使用ProfileDoa的真實 bean。 這將需要您的數據庫和更多設置。

對於選項一,您可以如下調整測試:

@WebMvcTest(MainController.class)
public class MainControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ProfileDoa profileDoa;

    private boolean loggedOut = true;
    private boolean loggedIn = false;

    @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);
    }
}

由於我在MainController中看不到與profileDoa的任何交互,因此也無需准備任何模擬方法響應。 但是,如果您確實在某處調用了例如profileDao.findByEmail("mail@duke.io") ,則可以使用 Mockito 來准備結果:

ProfileEntity databaseResult = new ProfileEntitiy();
when(profileDao.findByEmail("THIS_HAS_TO_MATCH_YOUR_MAIL")).thenReturn(databaseResult);

對於選項二,您可以結合使用@SpringBootTest@AutoconfigureMockMvc來加載整個 Spring 上下文(所有 bean)並使用MockMvc

@SpringBootTest
@AutoconfigureMockMvc
public class MainControllerTest {

  // no mocking required as _real_ beans are used
}

在這里,您可能想使用例如Testcontainers來為您的測試啟動一個數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM