spring存储库集成测试

a7qyws3x  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(277)

我有一个带有spring框架的java项目。我有4.3.12(不是Spring Boot!)。我的存储库是接口,从springbase存储库接口扩展而来。
我用内存存储库实现了许多单元测试,但是在数据库(postgreslq)中,我有一些真正的数据库函数,我想测试它们。我不能用jpa查询实现这些。
现在我想测试那些数据库函数,所以我想做集成测试。但是,当应用程序上下文初始化时,我无法加载存储库接口,并且无法接收
原因:org.springframework.beans.factory.nosuchbeandefinitionexception:没有类型为“com.company.model.userrepository”的合格bean可用:至少需要1个符合autowire候选的bean。依赖项注解:{@org.springframework.beans.factory.annotation.autowired(required=true)}异常。
测试等级

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { ApplicationContext.class })
public class UserControllerIntegrationTest {
  @Autowired
  private UserRepository userRepository;

  .. test methods
}

applicationcontext.class是项目中真正的@configuration类。当我执行测试时,它会加载数据源、entitymanagerfactory、transactionmanager等。

package com.company.config;

import com.company.service.email.EmailService;
import com.zaxxer.hikari.HikariDataSource;
import java.util.Properties;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.flywaydb.core.Flyway;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@PropertySources({
  @PropertySource("classpath:application.properties"),
  @PropertySource("file:${CCTNG_HOME}/application.properties")
})
@ComponentScan({"com.company"})
@EnableWebMvc
@EnableTransactionManagement
public class ApplicationContext {

  private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
  private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
  private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
  private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
  private static final String PROPERTY_NAME_DATABASE_MIGRATE_USERNAME = "db.migrate.username";
  private static final String PROPERTY_NAME_DATABASE_MIGRATE_PASSWORD = "db.migrate.password";

  private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
  private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
  private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY = "hibernate.ejb.naming_strategy";
  private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
  private static final String PROPERTY_NAME_HIBERNATE_DDL_AUTO = "hibernate.hbm2ddl.auto";
//  private static final String PROPERTY_NAME_HIBERNATE_IMPORT_FILES = "hibernate.hbm2ddl.import_files";

  private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";

  private static final String PROPERTY_NAME_LDAP_URL = "ldap.url";
  private static final String PROPERTY_NAME_LDAP_USERNAME = "ldap.principal";
  private static final String PROPERTY_NAME_LDAP_PASSWORD = "ldap.password";

  private static final String PROPERTY_NAME_EMAIL_USERNAME = "email.username";
  private static final String PROPERTY_NAME_EMAIL_APPPASSWORD = "email.apppassword";

  @Resource
  private Environment environment;

  @Bean(initMethod = "migrate")
  public Flyway flyway() {
    Flyway flyway = new Flyway();
//    flyway.setBaselineOnMigrate(true);
//    flyway.setDataSource(dataSource());
    flyway.setDataSource(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL), environment.getRequiredProperty(PROPERTY_NAME_DATABASE_MIGRATE_USERNAME), 
            environment.getRequiredProperty(PROPERTY_NAME_DATABASE_MIGRATE_PASSWORD), new String[]{});
    return flyway;
  }

//  @Value("classpath:schema-postgresql.sql")
//  private org.springframework.core.io.Resource schemaScript;
  @Bean
  public DataSource dataSource() {
    HikariDataSource dataSource = new HikariDataSource();

    dataSource.setDriverClassName(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
    dataSource.setJdbcUrl(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
    dataSource.setUsername(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
    dataSource.setPassword(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

    return dataSource;
  }

  @Bean
  public PlatformTransactionManager transactionManager() throws ClassNotFoundException {
    JpaTransactionManager transactionManager = new JpaTransactionManager();

    transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());

    return transactionManager;
  }

  @Bean(name = "entityManagerFactory")
  @DependsOn("flyway")
  public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() throws ClassNotFoundException {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
    entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);

    Properties jpaProterties = new Properties();
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_DDL_AUTO, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DDL_AUTO));
//    jpaProterties.put(PROPERTY_NAME_HIBERNATE_IMPORT_FILES, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_IMPORT_FILES));

//    jpaProterties.put("spring.jpa.properties.hibernate.jdbc.batch_size", "25");
//    jpaProterties.put("spring.jpa.properties.hibernate.order_inserts", "true");
//    jpaProterties.put("spring.jpa.properties.hibernate.order_updates", "true");
//    jpaProterties.put("spring.jpa.properties.hibernate.generate_statistics", "true");
    entityManagerFactoryBean.setJpaProperties(jpaProterties);

    return entityManagerFactoryBean;
  }

  @Bean
  public LdapContextSource ldapContextSource() {
    LdapContextSource contextSource = new LdapContextSource();

    contextSource.setUrl(environment.getRequiredProperty(PROPERTY_NAME_LDAP_URL));
    contextSource.setUserDn(environment.getRequiredProperty(PROPERTY_NAME_LDAP_USERNAME));
    contextSource.setPassword(environment.getRequiredProperty(PROPERTY_NAME_LDAP_PASSWORD));

    return contextSource;
  }

  @Bean
  public LdapTemplate ldapTemplate() {
    return new LdapTemplate(ldapContextSource());
  }

  @Bean(name = "emailService")
  public EmailService email() {
    Properties props = new Properties();
    props.put("mail.smtp.host", "mailrelay.xxx.com");
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.auth", "false");
    props.put("mail.smtp.connectiontimeout", "10000");

    EmailService email = new EmailService(
            environment.getRequiredProperty(PROPERTY_NAME_EMAIL_USERNAME),
            environment.getRequiredProperty(PROPERTY_NAME_EMAIL_APPPASSWORD),
//            environment.getRequiredProperty(PROPERTY_NAME_EMAIL_APPROOT),
            props);

    return email;

  }
}

但由于某些原因,存储库没有启动。
谢谢你的提示!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题