简体   繁体   English

没有符合条件的 bean,但我添加了 @Component 注释

[英]No qualifying bean, but I added @Component annotation

Could not invoke step [User clicks Register on main page] defined at 'pcz.StepDefs.LoginPageStepDefs.userClicksRegisterOnMainPage()'.无法调用在“pcz.StepDefs.LoginPageStepDefs.userClicksRegisterOnMainPage()”中定义的步骤 [用户单击主页上的注册]。 It appears there was a problem with the step definition.步骤定义似乎存在问题。 The converted arguments types were ()转换后的参数类型为 ()

Caused by: io.cucumber.core.backend.CucumberBackendException: Error creating bean with name 'pcz.StepDefs.LoginPageStepDefs': Unsatisfied dependency expressed through field 'loginPage';原因:io.cucumber.core.backend.CucumberBackendException:创建名称为“pcz.StepDefs.LoginPageStepDefs”的bean时出错:通过字段“loginPage”表示的依赖关系不满足; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'pcz.Page.LoginPage' available: expected at least 1 bean which qualifies as autowire candidate.嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“pcz.Page.LoginPage”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。 Dependency annotations: {@org.springframework.beans.factory .annotation.Autowired(required=true)}依赖注解:{@org.springframework.beans.factory .annotation.Autowired(required=true)}

Project Structure:项目结构:

在此处输入图像描述

public class LoginPageStepDefs {

@Autowired
LoginPage loginPage;


    @When("User clicks Register on main page")
    public void userClicksRegisterOnMainPage() {
        loginPage.test();

    }
}



import org.springframework.stereotype.Component;
@Component
public class LoginPage {
    public void test(){
    }
}

And Spring config:和弹簧配置:

@CucumberContextConfiguration
@ContextConfiguration(classes = SpringBootCucumberApplication.class, loader = AnnotationConfigContextLoader.class)
@SpringBootTest()
public class CucumberSpringConfiguration {
}


@SpringBootApplication()
public class SpringBootCucumberApplication {

    public static void main(String[] args) {


    }
}

Neither your SpringBootCucumberApplication nor your CucumberSpringConfiguration scan your LoginPage component because it's in a totally different package.您的SpringBootCucumberApplicationCucumberSpringConfiguration都不会扫描您的LoginPage组件,因为它位于完全不同的包中。 The default component scan only looks in the current and any descending packages, but pcz.Page isn't anywhere beneath the pcz.StepDefs or config packages.默认组件扫描仅查看当前和任何降序包,但pcz.Page不在pcz.StepDefsconfig包下方的任何位置。

You can try any of the following:您可以尝试以下任何方法:

  • Rename the config package to pcz .config包重命名为pcz That will make the default component scan of your SpringBootCucumberApplication notice your pcz.Page package.这将使SpringBootCucumberApplication的默认组件扫描通知您的pcz.Page包。

  • Move your pcz.Page package to pcz.StepDefs.Page so that the default component scan of your CucumberSpringConfiguration will notice it.将您的pcz.Page包移动到pcz.StepDefs.Page以便CucumberSpringConfiguration的默认组件扫描会注意到它。

  • Use a ComponentScan annotation on your CucumberSpringApplication , telling it which packages to scan: @ComponentScan(basePackages = {"Page"}) .CucumberSpringApplication上使用ComponentScan注释,告诉它要扫描哪些包: @ComponentScan(basePackages = {"Page"})

Side note, your package names should probably be lower-cased .旁注,你的包名应该是小写的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM