当我使用testng xml运行黄瓜测试用例时,如何DI?

问题描述 投票:0回答:1

我使用qaf和testng来运行cucumber测试用例,现在我想在测试步骤中使用spring来自动连接UserRepository。

<suite name="Web Demo Suite" verbose="0" parallel="tests" thread-count="100">
    <listeners>
        <listener class-name="com.quantum.listeners.QuantumReportiumListener" />
    </listeners>
    <test name="Web Test" enabled="true" thread-count="10">
        <groups>
            <run>
                <include name="@testH2Spring"/>
            </run>
        </groups>
        <classes>
            <class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
        </classes>
    </test>
</suite>

下面是功能文件。

  @testH2Spring
  Scenario: H2 Spring test
    When I control with DB by Spring

下面是JPA配置。

@Configuration
@EnableJpaRepositories(basePackages = "com.quantum.repository")
public class H2DBConfig {

    @Bean
    public ComboPooledDataSource datasource() throws PropertyVetoException {

        String path = System.getProperty("user.dir");
        System.out.println(path);

        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("org.h2.Driver");
        dataSource.setJdbcUrl("jdbc:h2:file:" + path + "/src/main/resources/data/test");
        dataSource.setUser("sa");
        dataSource.setPassword("");
        dataSource.setInitialPoolSize(5);
        dataSource.setMaxPoolSize(10);
        return dataSource;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.H2);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(true);
        adapter.setDatabasePlatform("org.hibernate.dialect.H2Dialect");

        return adapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter){

        LocalContainerEntityManagerFactoryBean  entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactoryBean.setPackagesToScan("com.quantum.entity");
        return entityManagerFactoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        return new JpaTransactionManager(emf);
    }

    @Bean
    public BeanPostProcessor persistenceTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}

下面是UserRepository

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {}

下面是测试步骤。

@QAFTestStepProvider
@ContextConfiguration(classes = H2DBConfig.class)
public class WebTestSteps extends AbstractTestNGSpringContextTests {

    private UserRepository userRepository;

    @Autowired
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @When("^I open browser to web page$")
    public void iOpenBrowserToWebPage() {

        User user = userRepository.getOne(1);
        System.out.println(user.toString());
    }

}

用testng xml运行黄瓜测试用例如上,UserRepository不能成功@Autowired,如何解决让UserRepository能@Autowired?

如何解决让UserRepository可以@Autowired?

spring cucumber testng qaf
1个回答
1
投票

要调用任何TestStep,QAF要求该类的对象或方法必须是静态的。默认情况下,QAF使用非args构造函数创建一个新对象。

但是在QAF中,你可以通过使用下面的方法设置你的CustomObjectFactory。你可以在TestNG Listeners中设置你的对象工厂。

ObjectFactory.INSTANCE.setFactory(new CustomObjectFactory());
import com.qmetry.qaf.automation.step.ObjectFactory;

public class CustomObjectFactory implements ObjectFactory {

    @Override
    public <T> T getObject(Class<T> cls) throws Exception {
        // your implementation
        return object;
    }

}

在这里你可以有你的实现来创建该类的对象。希望这对你有帮助。

编辑: 如果你想使用任何第三方的对象工厂,你可以使用它。例如,下面是使用基本的Guice实现。

/**
 * @author chirag.jayswal
 *
 */
public class GuiceObjectFactory extends DefaultObjectFactory {//implements ObjectFactory {
    private static final Injector injector = Guice.createInjector(new GuiceModule());
    public <T> T getObject(Class<T> cls) throws Exception {
        T obj = injector.getInstance(cls);
        return obj;
    }
}

确保你有其他与底层对象工厂相关的配置。

© www.soinside.com 2019 - 2024. All rights reserved.