引起:org.springframework.beans.factory.BeanCreationException:创建名称为' loginRepository'的init时出错:init方法的调用失败;

时间:2016-02-25 01:20:57

标签: spring-mvc spring-boot spring-data cucumber-jvm

我有时间让Spring上下文运行我的黄瓜测试

我只是攀爬测试所需的豆子,而不是所有的弹簧环境。

奇怪的是我的实体的登录ID属性为Long

@Configuration
@EnableAutoConfiguration         
@PropertySource("application.properties")
public class ConfigApplicationTests {

    }

@ContextConfiguration(classes = ConfigApplicationTests.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class UserStepdefs {

private User user;
private static Validator validator;
private String retorno;
private Set<ConstraintViolation<User>> constraintViolations;


@Autowired
private UserService userService;

@Before
public static void setUp(){
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    validator = factory.getValidator();
}



@Dado("^que um usuario tenha \"([^\"]*)\" e \"([^\"]*)\" e \"([^\"]*)\" e \"([^\"]*)\"$")
public void queUmUsuarioTenhaNomeSobrenomeEmailSenha(String nome, String sobrenome, String email, String senha) throws Throwable {
    Login login = new Login(email,senha);
    user = new User(nome,sobrenome,login);

}

@Quando("^eu clicar em Salvar$")
public void euClicarEmSalvar() throws Throwable {
   String message =  validateModel();
    if(!StringUtils.isEmpty(message)){
        userService.save(user);
        retorno = String.valueOf(HttpStatus.CREATED.value());
    }else{
        retorno = message;
    }

}


@Entao("^eu recebo \"([^\"]*)\"$")
public void euRecebo(String retorno) throws Throwable {
    Assert.assertEquals(this.retorno,retorno);

}


private String validateModel() {
    String message = "";
    constraintViolations = (HashSet<ConstraintViolation<User>>) validator.validate(user);

    if(constraintViolations.iterator().hasNext()){
        message = constraintViolations.iterator().next().getMessage();
    }

    return message;
}


@RunWith(Cucumber.class)
@CucumberOptions(glue = "com.br.oor.step",
            features = "classpath:feature/User.feature",
    format = "json:target/cucumber.json",
            strict = true,
    tags = { "~@Ignore" })
public class UserServiceTest {

}

public interface LoginRepository extends JpaRepository<Login,Long> {

Optional<Login> emailEquals(String email);

/*@Query(value = "select l FROM login l where l.id = ?1 and l.email =  ?2 ", nativeQuery = true)*/
Optional<Login> idNotEqualsAndEmailEquals(Long id ,String email);
}

LOGERROR

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at cucumber.runtime.java.spring.CucumberTestContextManager.getContext(SpringFactory.java:215)
at cucumber.runtime.java.spring.CucumberTestContextManager.<init>(SpringFactory.java:207)
at cucumber.runtime.java.spring.SpringFactory.start(SpringFactory.java:102)
at cucumber.runtime.java.JavaBackend.buildWorld(JavaBackend.java:123)
at cucumber.runtime.Runtime.buildBackendWorlds(Runtime.java:140)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:38)
at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.runtime.junit.ExamplesRunner.run(ExamplesRunner.java:59)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.runtime.junit.ScenarioOutlineRunner.run(ScenarioOutlineRunner.java:53)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:93)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:37)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.api.junit.Cucumber.run(Cucumber.java:98)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property not found for type Long! Traversed path: Login.id.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframewor

1 个答案:

答案 0 :(得分:0)

最初的问题是:No property not found for type Long! Traversed path: Login.id。 您的Login实体id字段看起来不是Long类型。

Spring Data Jpa有一个非常好的API。您可以将@Query替换为finder:

Optional<Login> findOneByIdAndEmail(Long id ,String email);
相关问题