Spring-data test set



  • I can't build a test run. All tests are connected to a work base (in which development is carried out). If rollback is included, the test data are not left after the test, but if the test falls, everything he kept is in the work base. How do you make the test run on another base?

    Workbase

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages = {"ru.make.alex.web"})
    @ComponentScan("ru.make.alex.web.config")
    @PropertySource(value = {"classpath:application.properties"})
    public class HibernateConfig
    {
        @Autowired
        private Environment environment;
    
    @Bean
    public LocalSessionFactoryBean sessionFactory()
    {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[]
                {"ru.web.model"});
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }
    
    @Bean
    public DataSource dataSource()
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment
                .getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource
                .setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource
                .setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }
    
    @Bean
    public EntityManagerFactory entityManagerFactory()
    {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
    
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("ru.web");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();
    
        return factory.getObject();
    }
    
    private Properties hibernateProperties()
    {
        Properties properties = new Properties();
        properties.put("hibernate.dialect",
                environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.hbm2ddl.auto",
                environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.show_sql",
                environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql",
                environment.getRequiredProperty("hibernate.format_sql"));
        return properties;
    }
    
    @Bean
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory)
    {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }
    

    }

    Test base configuration

    @Configuration
    @EnableTransactionManagement
    @ComponentScan(basePackages = {"ru.web.test.config"}, excludeFilters =
    {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Configuration.class)})
    public class HibernateTestConfig
    {
    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory()
    {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[]
                {"ru.web.model"});
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }
    
    @Bean
    public DataSource dataSource()
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(
                "jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8");
        dataSource.setUsername("user");
        dataSource.setPassword("user");
        return dataSource;
    }
    
    @Bean
    public EntityManagerFactory entityManagerFactory()
    {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
    
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("ru.web.test");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();
    
        return factory.getObject();
    }
    
    private Properties hibernateProperties()
    {
        Properties properties = new Properties();
        properties.put("hibernate.dialect",
                "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.hbm2ddl.auto", "create-drop");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "false");
        properties.put("ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
        return properties;
    }
    
    @Bean
    public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory)
    {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }
    

    }

    The test.

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes =
    {HibernateTestConfig.class, TestConfig.class})
    @WebAppConfiguration
    @TransactionConfiguration(defaultRollback = false)
    @Transactional
    public class LooksTest
    {
    @Autowired
    private LookFilmService lookFilmService;

    @Test
    public void createTest()
    {
        String название = "Название Фильма";
        LookFilm фильм = new LookFilm();
        фильм.setНазвание(название);
        lookFilmService.сохранитьФильм(фильм);
        List<LookFilm> фильмы = lookFilmService.getФильмы();
        Assert.assertEquals(1, фильмы.size());
        LookFilm lookFilm = фильмы.get(0);
        Assert.assertEquals(название, lookFilm.getНазвание());
    
        String время = "15:22";
        lookFilmService.отметитьВремяФильма(lookFilm.getId(), время);
        фильмы = lookFilmService.getФильмы();
        Assert.assertEquals(1, фильмы.size());
        lookFilm = фильмы.get(0);
        Assert.assertEquals(время, lookFilm.getВремя());
    
        lookFilmService.отметитьПросмотрФильм(lookFilm.getId());
        фильмы = lookFilmService.getФильмы();
        Assert.assertEquals(1, фильмы.size());
        lookFilm = фильмы.get(0);
        Assert.assertTrue(lookFilm.isПросмотрен());
    }
    

    }

    TestConfig

    @Configuration
    @ComponentScan(basePackages = {"ru.web"})
    public class TestConfig
    {
    @Bean
    public IndicatorsService getIndicatorsService()
    {
    return new IndicatorsServiceImpl();
    }

    @Bean
    public HomeKubService getHomeKubService()
    {
        return new HomeKubServiceImpl();
    }
    

    }

    Everything in CRUD. I don't understand why he's a test configuration that ignores.



  • In fact, it's all earned itself, the only thing that's changed is that.

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages = {"ru.web.repository"})
    @ComponentScan(basePackages = {"ru.web.test.config"}, excludeFilters =
            {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Configuration.class)})
    public class HibernateTestConfig
    {
        @Autowired
        private Environment environment;
    
    @Bean
    public LocalSessionFactoryBean sessionFactory()
    {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[]
                {"ru.web.model"});
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }
    
    @Bean
    public DataSource dataSource()
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(
                "jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8");
        dataSource.setUsername("user");
        dataSource.setPassword("user");
        return dataSource;
    }
    
    @Bean
    public EntityManagerFactory entityManagerFactory()
    {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
    
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("ru.web");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();
    
        return factory.getObject();
    }
    
    private Properties hibernateProperties()
    {
        Properties properties = new Properties();
        properties.put("hibernate.dialect",
                "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "false");
        properties.put("ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
        return properties;
    }
    
    @Bean
    public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory)
    {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }
    

    }

    And the TestConfig added

    @Configuration
    @ComponentScan(basePackages = {"ru.make.alex.web.test"})
    public class TestConfig
    {
    @Bean
    public LookFilmService getLookFilmService()
    {
    return new LookFilmServiceImpl();
    }

    @Bean
    public LookSeriesService getLookSeriesService()
    {
        return new LookSeriesServiceImpl();
    }
    
    @Bean
    public IndicatorsService getIndicatorsService()
    {
        return new IndicatorsServiceImpl();
    }
    
    @Bean
    public HomeKubService getHomeKubService()
    {
        return new HomeKubServiceImpl();
    }
    

    }

    Now all the tests are on the test base.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2