Can we skip instantiation of TestNg Factory method?

22 views
Skip to first unread message

Java Enthusiast

unread,
Oct 24, 2022, 1:18:31 AM10/24/22
to testng-users
I have following class structure:
--------------------------------------------------------------------------------------------------
public class A { 

@Factory 
public Object[] tst_createFactoryTests() {
}
}

public class B extends A { 
public static final int NUM = 10;
}

public class C extends A { 
public static final int NUM = 20;
}

public class D extends A { 
public static final int NUM = -30;
}
--------------------------------------------------------------------------------------------------

  • So basically class A is a super class. And class B, C & D extend class A.
  • I am writing a factory method in class A which should create a test for each of the class which extends it only if the value of NUM is positive for the class invoking the superclass. 
  • So every time when class A is invoked by one of the subclasses (B or C), the factory method in class A should create tests for those test classes (One for class B and one for class C). 
  • And in case when class A is invoked by class C, since the NUM value is negative, the factory method should skip creation of test for class C.
  • While doing so, I am getting following exception:
    • An error occurred while instantiating class A. Check to make sure it can be instantiated. Root cause: The Factory method tst_createFactoryTests() should have produced at-least one instance.
  • So, my question is do we have a way by which we can skip instantiation of a factory method for a few cases based on a condition? 
  • Please let me know if the question is not clear or if I need to add some more information about it.
Thanks in advance!!

⇜Krishnan Mahadevan⇝

unread,
Oct 24, 2022, 1:28:35 AM10/24/22
to testng...@googlegroups.com
Anirud,

The question is not clear because you are asking if factory method instantiation can be controlled by a condition. 

The factory method if made static can be invoked by TestNG without having to instantiate the class in which it resides in (which means you can make the factory method in Class A as static)

But the conditional invocation of the Factory method cannot be done as far as I know.


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/33faa160-e3c1-4c13-965b-7b41502971c7n%40googlegroups.com.

Java Enthusiast

unread,
Oct 24, 2022, 2:12:41 AM10/24/22
to testng-users
Thank you for your response, Krishnan! Let me rephrase the question.
Can we skip instantiation of test class (@Test method) inside factory method in TestNG? (Refer following code snippet for more clarity).
  • By this I mean that we expect every subclass (B, C, and D) which extend the super class A to invoke the factory method.  
  • But inside the factory method, we will have a logic to check if NUM value is greater than 0. If it is greater than 0, we will let the @Test method inside factory method to run, other wise we should be able to skip it.   


--------------------------------------------------------------------------------------------------
public class A { 

     @Factory 
     public Object[] tst_createFactoryTests() {
     final ArrayList<FactoryTest> factoryTests = new ArrayList<>();
     if (NUM < 0) {
     logger.info("Skip factoryTest")
     } else {
     factoryTests.add(new FactoryTest)
     }
     return factoryTests.toArray();
    }
    public static class FactoryTests {
     // Constructor definition
     @Test
     public void tst_factoryTest() {
     // Test logic 
    }
}

public class B extends A { 
public static final int NUM = 10;
}

public class C extends A { 
public static final int NUM = 20;
}

public class D extends A { 
public static final int NUM = -30;
}
--------------------------------------------------------------------------------------------------

⇜Krishnan Mahadevan⇝

unread,
Oct 24, 2022, 2:34:38 AM10/24/22
to testng...@googlegroups.com
Anirud,

Just to ensure that we are clear on the terms and terminologies.

Factory methods are expected to produce testclass (A class which has one or more "@Test" methods is called a test class) instances.

So you are expecting that we build some condition (based on positivity of a number that gets yielded by every test class) based on which the factory method returns or does not return the test class instance.

Here's where the confusion starts:
  • If the field is an instance level field, then the factory method would not be able to access the field until the object gets instantiated and that is what we are conditionally trying to control.
  • If the field is a static field, then you would need to use reflection to check the value of that field (if you would like it to be generic) and then proceed with instantiation, which in my opinion is kind of over engineering.
So please clarify what is the use case that you are trying to solve and also include a bit more concrete code sample that explains your problem statement.

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/

Reply all
Reply to author
Forward
0 new messages