简体   繁体   English

TestNG 拦截TestClassConstructor 模拟

[英]TestNG interceptTestClassConstructor analog

I need somehow to intercept Test class constructor to override a behavior.我需要以某种方式拦截 Test 类构造函数以覆盖行为。 In JUnit , it is done with:JUnit ,它是通过以下方式完成的:

@Override
public <T> T interceptTestClassConstructor(Invocation<T> invocation,
                                           ReflectiveInvocationContext<Constructor<T>> invocationContext,
                                           ExtensionContext extensionContext) throws Throwable {
...
}

How is this achievable in TestNG?这在 TestNG 中如何实现?

You could override the behavior using IObjectFactory .您可以使用IObjectFactory覆盖该行为。

public class CustomObjectFactory implements IObjectFactory {

    @Override
    public Object newInstance(Constructor constructor, Object... params) {
        if(constructor.getDeclaringClass().equals(YourTestCalss.class)) {
            return yourCustomObject();
        }

        try {
            return constructor.newInstance(params);
        } catch(InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // error handling...
        }
    
        return null; // or any other object or throw exception
    }
}

Now in a test class, add a method as below:现在在测试类中,添加如下方法:

@Test
public class YourTestClass {
    // ......
    // test methods
    // ......


    @ObjectFactory
    public ITestObjectFactory getObjectFactory() {
        return new CustomObjectFactory();
    }
}

Please note that even though this method is declared inside YourTestClass , this same object factory would be used for every other test class in your suite.请注意,即使此方法是在YourTestClass声明的,这个相同的对象工厂也将用于套​​件中的每个其他测试类。 Hence the if condition to check the class is required in CustomObjectFactory.newInstance .因此,在CustomObjectFactory.newInstance需要检查类的if条件。

You may also try IObjectFactory2 which has a newInstance method taking Class as the parameter.您也可以尝试IObjectFactory2 ,它有一个以Class作为参数的newInstance方法。


TestNG has an implementation for IObjectFactory -> ObjectFactoryImpl . TestNG 有IObjectFactory -> ObjectFactoryImpl You could use extends ObjectFactoryImpl instead, in which case the code within the `newInstance could be:您可以改用extends ObjectFactoryImpl ,在这种情况下,`newInstance 中的代码可以是:

if(constructor.getDeclaringClass().equals(YourTestCalss.class)) {
    return yourCustomObject();
}
return super.newInstance(constructor, params);

BUT note that, Since ObjectFactoryImpl is inside the testng.internal package, I assume that extending ObjectFactoryImpl is not recommended.但请注意,由于ObjectFactoryImpl位于testng.internal包内,因此我认为不建议扩展ObjectFactoryImpl

As discuss in mailing group , @KrishnanMahadevan fixed a bug which will allow to intercept Test class constructor in version 7.5正如在邮件组中讨论的那样,@KrishnanMahadevan 修复了一个错误,该错误将允许拦截 7.5 版中的 Test 类构造函数

public class LocalSuiteAlteringListener implements IAlterSuiteListener { @Override public void alter(List<XmlSuite> suites) { suites.forEach(each -> each.setObjectFactoryClass(MyFactory.class));

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

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