简体   繁体   English

如何在 java 中创建模板 class 文件

[英]How to create a template class file in java

I am creating a test automation framework using selenium & TestNG.我正在使用 selenium 和 TestNG 创建一个测试自动化框架。 This Framework will be use by all of the other team member.该框架将被所有其他团队成员使用。 Want to specify a class template for all the team member so that basic structure of the test class will same for all and reduce the effort for writing the same structure for all test.想要为所有团队成员指定一个 class 模板,以便测试 class 的基本结构对所有人都相同,并减少为所有测试编写相同结构的工作量。

Whenever any member create any class in a particular package the class will be created with some predefined code like below每当任何成员在特定 package 中创建任何 class 时,将使用一些预定义的代码创建 class,如下所示

package com.xxx.yyy.testmodule.dummytest;
import org.testng.annotations.Test;
import com.xxx.yyy.lib.zzz.CommonLib;

public class Test3 extends CommonUtilCommonLibities{
    @Test(description="", groups= {""})
    public void testTest3() {

        //Read Test Data Here

        //Test Logic

        //Test Verification

    }
}

Use an abstract class:使用抽象的 class:

package com.xxx.yyy.testmodule.dummytest;
import org.testng.annotations.Test;
import com.xxx.yyy.lib.zzz.CommonLib;

public abstract class Test3 extends CommonUtilCommonLibities {

    @Test(description="", groups= {""})
    public void testTest3() {

        //Read Test Data Here
        Data data = readTestData();

        //Test Logic
        test(data);

        //Test Verification
        testAssertion(data);

    }

    abstract Data readTestData();
    abstract void test(Data data);
    abstract void testAssertion(data);
}

Of course this only works if all your data beans extend some base bean which is Data in my example.当然,这仅在您的所有数据 bean 都扩展了一些基本 bean 时才有效,在我的示例中是Data

If you use Intellij IDEA, you can define a test class template, including your methods.如果您使用 Intellij IDEA,您可以定义一个测试 class 模板,包括您的方法。 There's a similar question with the instructions.指令中有一个类似的问题

You may also want to make use of a Template Method pattern:您可能还想使用模板方法模式:

public abstract class GenericTest<T> {

    @Test
    public void doTest() {
        T data = loadTestData();
        this.runTestLogic(data);
        this.runAssertions(data);
        // something else...
    }

    protected abstract T loadTestData();

    protected abstract void runTestLogic(T result);

    protected abstract void runAssertions(T result);

}

Your concrete test classes will extend this generic class and implement the actual testing and data reading logic:您的具体测试类将扩展此通用 class 并实现实际测试和数据读取逻辑:

public class ConcreteTest extends GenericTest<Integer> {

    @Override
    protected Integer loadTestData() {
        return 42;
    }

    @Override
    protected void runTestLogic(Integer result) {
        System.out.println(result);
    }

    @Override
    protected void runAssertions(Integer result) {
        assertEquals(0, result % 2);
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM