简体   繁体   English

[qaf]当我使用testng xml运行Z1441F19754335CA6ZEA8测试案例时如何与spring集成

[英][qaf]how to integrate with spring when I use testng xml to run cucumber test case

I use qaf with testng to run cucumber test case.我使用 qaf 和 testng 来运行 cucumber 测试用例。 Now I want use spring to autowired UserRepository in test step.现在我想在测试步骤中使用 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>

Below is the feature file:以下是功能文件:

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

Below is the JPA config:下面是 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();
    }

}

Below is the userRepository下面是用户存储库

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

Below is the test step:下面是测试步骤:

@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());
    }

}

If I use testng xml to run cucumber test case like above, the UserRepository can't @Autowired successfully.如果我像上面那样使用 testng xml 运行 cucumber 测试用例,则 UserRepository 无法 @Autowired 成功。

how to solve it so that the UserRepository can be @Autowired?如何解决它以便 UserRepository 可以@Autowired?

To invoke any TestStep QAF requires object of that class or method needs to be a static.要调用任何 TestStep QAF,需要 object 的 class 或方法需要是 static。 By default QAF creates a new object using non args constructor.默认情况下,QAF 使用非 args 构造函数创建一个新的 object。

But in QAF you can set your CustomObjectFactory by using below method.但在 QAF 中,您可以使用以下方法设置您的 CustomObjectFactory。 You can set your object factory in your TestNG Listeners.您可以在 TestNG 监听器中设置您的 object 工厂。

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;
    }

}

Here you can have your implementation to create object of that class.在这里,您可以实现创建 class 的 object。 Hope this helps.希望这可以帮助。

EDIT : If you want to use any third party Object factory you can use it.编辑:如果您想使用任何第三方 Object 工厂,您可以使用它。 Foe exmaple, below is using basic Guice implementation.敌人的例子,下面是使用基本的 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;
    }
}

Make sure that you have other configuration related to underlying object factory.确保您具有与底层 object 工厂相关的其他配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Cucumber 在 Spring 中指定不与 TestNG 运行器并行运行的测试用例 - Specify test case not to run in parallel with TestNG runner in Spring using Cucumber 如何将Spring整合到黄瓜中 - How to integrate Spring into Cucumber 我可以在黄瓜测试中使用spring来自动控制控制器吗? - Can I use spring to autowire controller in cucumber test? 如何在Spring测试侦听器中获取TestNG ITestResult? - How can I get TestNG ITestResult in Spring test listener? 如何模拟Spring SecurityContext,以便可以与TestNG一起使用? - How to mock Spring SecurityContext so I can use it with TestNG? 黄瓜测试用例一起运行时会失败,但是当我单独运行它们时它们会通过 - Cucumber test cases fail when run together, but they pass when I run them individually Spring:如果无法扩展AbstractTestNGSpringContextTests,如何在Testng测试类中获取spring applicationContext - Spring: How to get spring applicationContext in a Testng test class, if I can NOT extend AbstractTestNGSpringContextTests 是TestNG还是Spring Test框架? - TestNG or Spring Test framework? 如何在 TestNG 中使用 Spring WithMockUser 注释 - How to use Spring WithMockUser annotation with TestNG 如何在测试工厂类中使用弹簧自动装配 - How to use spring autowiring in a testng factory class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM