TestNG interceptTestClassConstructor analog

62 views
Skip to first unread message

Mitia Alexandrov

unread,
Nov 17, 2021, 7:33:02 PM11/17/21
to testng-users
Hello,
I need somehow to intercept Test class constructor to override a behavior. In JUnit it is done with:

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

How is this achievable in TestNG?

Thank you!

Best regards,
Dmitry

⇜Krishnan Mahadevan⇝

unread,
Nov 18, 2021, 11:26:55 PM11/18/21
to testng-users
Dmitry,

You could leverage an ObjectFactory wherein you can control how the objects are being created.

1. Implement org.testng.IObjectFactory
2. Provide a reference to this object factory in your suite xml file via the attribute "object-factory".

This would let you gain access to the constructor and also the parameters that are being passed to it.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/3521e362-c2b1-4b12-816f-982e3be63d5en%40googlegroups.com.

Mitia Alexandrov

unread,
Nov 19, 2021, 4:04:49 AM11/19/21
to testng...@googlegroups.com
Thank you very much!

I'm now creating a Suite listener, so is there a way to include this object Factory as a "default" for all tests?

Best regards,
Dmitry

пт, 19 нояб. 2021 г. в 07:26, ⇜Krishnan Mahadevan⇝ <krishnan.ma...@gmail.com>:
You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/CoFEnZTWDJM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/CANikZLkQUERSOk8AqZd8k15Y1ekPMXgxgwc8AsUxeSA4NTRCkQ%40mail.gmail.com.

⇜Krishnan Mahadevan⇝

unread,
Nov 19, 2021, 4:14:59 AM11/19/21
to testng-users
Yes you can do that via iSuite.getXmlSuite().setObjectFactory()

Here iSuite is of type ISuite which you get access to in your onStart of your ISuiteListener


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Mitia Alexandrov

unread,
Nov 19, 2021, 4:20:54 AM11/19/21
to testng...@googlegroups.com
Thank you!..But.. did not work for me...

newIstance never called...

@Override
public Object newInstance(Constructor constructor, Object... params) {
System.out.println(">>> "+constructor.getDeclaringClass());
if (container == null) {
...
}
return ...;
}

@Override
public void onStart(ISuite suite) {
System.
out.println("Starting Suite");
suite.getXmlSuite().setObjectFactory(this);
}

пт, 19 нояб. 2021 г. в 12:15, ⇜Krishnan Mahadevan⇝ <krishnan.ma...@gmail.com>:

⇜Krishnan Mahadevan⇝

unread,
Nov 19, 2021, 4:29:31 AM11/19/21
to testng-users
Ok. Can you please try the same thing with an implementation of IAlterSuiteListener which you can wire in as a mandatory listener via the Service Provider Interface support ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Mitia Alexandrov

unread,
Nov 19, 2021, 4:53:47 AM11/19/21
to testng...@googlegroups.com
Looks like we are almost there!!

but.. this did not help:

@Override
public void alter(List<XmlSuite> suites) {
suites.forEach(e-> e.setObjectFactory(this));
}

This method is called, but still no "newInstance is called"

пт, 19 нояб. 2021 г. в 12:29, ⇜Krishnan Mahadevan⇝ <krishnan.ma...@gmail.com>:

⇜Krishnan Mahadevan⇝

unread,
Nov 19, 2021, 5:22:52 AM11/19/21
to testng-users
Looks like we have a problem.

What you are noticing is because of a broken functionality.
This was kind of fixed/patched in the upcoming TestNG version (7.5.0 which is yet to be released).
But even there, the same test fails with a NullPointerException. I have logged a bug for this https://github.com/cbeust/testng/issues/2676 and will work on fixing it.

Until 7.5.0 is released, you would be able to get this working only via the object-factory attribute in the suite file.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Mitia Alexandrov

unread,
Nov 19, 2021, 5:32:23 AM11/19/21
to testng...@googlegroups.com
Oh, cool!! This is very kind of you!

Thank you very much!

пт, 19 нояб. 2021 г. в 13:22, ⇜Krishnan Mahadevan⇝ <krishnan.ma...@gmail.com>:

Krishnan Mahadevan

unread,
Nov 21, 2021, 10:59:08 PM11/21/21
to testng...@googlegroups.com

I have raised a PR to fix the bug in question and its being reviewed. So this fix would definitely be included in 7.5.0 (whenever it gets released).

Mitia Alexandrov

unread,
Nov 22, 2021, 1:11:01 AM11/22/21
to testng...@googlegroups.com
Great!! Thank you!

пн, 22 нояб. 2021 г. в 06:59, Krishnan Mahadevan <krishnan.ma...@gmail.com>:

Mitia Alexandrov

unread,
Jan 5, 2022, 4:12:01 AM1/5/22
to testng...@googlegroups.com
Hello,
Happy New Year!

When is the 7.5.0 release planned? We are really depending on the bug resolved in this thread....

Best regards,
Dmitry

пн, 22 нояб. 2021 г. в 08:10, Mitia Alexandrov <mitiaal...@gmail.com>:

Krishnan Mahadevan

unread,
Jan 6, 2022, 11:00:43 PM1/6/22
to testng...@googlegroups.com

Dmitry,

 

TestNG 7.5 has been released. I just now shared the release notes on this group

Mitia Alexandrov

unread,
Jan 10, 2022, 3:38:21 AM1/10/22
to testng...@googlegroups.com
Great!!! When is it expected to be available in maven central?

пт, 7 янв. 2022 г. в 06:08, Krishnan Mahadevan <krishnan.ma...@gmail.com>:

⇜Krishnan Mahadevan⇝

unread,
Jan 10, 2022, 3:41:22 AM1/10/22
to testng-users
It is already available in Maven Central



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Mitia Alexandrov

unread,
Jan 10, 2022, 3:50:35 AM1/10/22
to testng...@googlegroups.com
YAY!! Awesome!!! Thank you!

пн, 10 янв. 2022 г. в 10:41, ⇜Krishnan Mahadevan⇝ <krishnan.ma...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages