i want to test the spring jpa but get Unsatisfied dependency expressed through field 'personRepository';
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String code;
private int flag;
private String source;
//getters and setters ...
}
@Repository
public interface PersonRepository extends JpaRepository<Person, AtomicInteger> {
Person findByName(String name);
Person findByCodeAndName();
@Query("from Person p where p.name = :name")
Person findUser(@Param("name") String name);
}
and my ConfigClass:need i costumized the @EnableJpaRepositories?
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@EnableJpaRepositories(
basePackages = {"learn.jpaRepository"},
basePackageClasses = {PersonRepository.class})
@EntityScan(basePackages = "learn.model")
@ComponentScan(basePackages = "learn")
@SpringBootApplication
public class LearnConfig {
}
my application.properties in main/resources
spring.datasource.url = jdbc:mysql://localhost:3306/learnspring
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
and my test class: every Class this Test need is in pom.xml
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = LearnConfig.class)
public class Main {
@Autowired
PersonRepository personRepository;
@Test
public void testFunctionConfig() {
Person p = personRepository.findByName("clz");
System.out.println(p.getName());
}
}
i just get the error below:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException
Spring can't load PersonRepository because there are missing parameters in findByCodeAndName
query method, change it to:
@Repository
public interface PersonRepository extends JpaRepository<Person, AtomicInteger> {
...
Person findByCodeAndName(String code, String name);
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.