I have super simple project written in page object pattern and the only dependency is TestNg 7.5.
[Screen with the project structure] : https://i.stack.imgur.com/NUt9z.png
TextEditorTabPage
package pages;
public class TextEditorTabPage {
VisualEditorTabPage VisualEditor = new VisualEditorTabPage();
public void qwer (){
System.out.println("gg");
}
}
VisualEditorTabPage
package pages;
public class VisualEditorTabPage {
TextEditorTabPage TextEditor = new TextEditorTabPage();
}
Testclass
package tests;
import pages.TextEditorTabPage;
import pages.VisualEditorTabPage;
import org.testng.annotations.*;
public class Testclass {
TextEditorTabPage textEditor = new TextEditorTabPage();
VisualEditorTabPage visualEditor = new VisualEditorTabPage();
@Test
public void test (){
}
}
When I run the test, I recieve StackOverFlow error:
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
org.testng.TestNGException:
Cannot instantiate class tests.Testclass
at org.testng.internal.objects.InstanceCreator.newInstance(InstanceCreator.java:41)
at org.testng.ITestObjectFactory.newInstance(ITestObjectFactory.java:18)
at org.testng.internal.objects.SimpleObjectDispenser.instantiateUsingDefaultConstructor(SimpleObjectDispenser.java:178)
at org.testng.internal.objects.SimpleObjectDispenser.createInstance(SimpleObjectDispenser.java:87)
at org.testng.internal.objects.SimpleObjectDispenser.dispense(SimpleObjectDispenser.java:40)
at org.testng.internal.objects.GuiceBasedObjectDispenser.dispense(GuiceBasedObjectDispenser.java:28)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:106)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:136)
at org.testng.TestClass.getInstances(TestClass.java:129)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:109)
at org.testng.TestClass.init(TestClass.java:101)
at org.testng.TestClass.<init>(TestClass.java:66)
at org.testng.TestRunner.initMethods(TestRunner.java:465)
at org.testng.TestRunner.init(TestRunner.java:333)
at org.testng.TestRunner.init(TestRunner.java:286)
at org.testng.TestRunner.<init>(TestRunner.java:176)
at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:635)
at org.testng.SuiteRunner.init(SuiteRunner.java:221)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:114)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1342)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1160)
at org.testng.TestNG.runSuites(TestNG.java:1092)
at org.testng.TestNG.run(TestNG.java:1060)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.testng.internal.objects.InstanceCreator.newInstance(InstanceCreator.java:38)
... 25 more
Caused by: java.lang.StackOverflowError
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:3)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
at pages.VisualEditorTabPage.<init>(VisualEditorTabPage.java:4)
at pages.TextEditorTabPage.<init>(TextEditorTabPage.java:4)
Where
VisualEditorTabPage. -> TextEditorTabPage.
repeats potentialy infinite times.
What am I doing wrong and how I can fix that?
Each class requires an object of the other. Therefore when you try to create an object of class TextEditorTabPage, it tries to create an object of VisualEditorTabPage. But VisualEditorTabPage requires an object of TextEditorTabPage to create. Here generates a loop.
What is your exact requirement?
Visual uses Text's methods, and Text uses Visual's methods To do it in a right way , do you think that I should just make some methods static? Instead of instantiating classes?
The right way would be to use the advantage of the Inheritance. Say you have:
Entity A
that have methods aA
, aB
and aC
Entity B
that have methods bE
, bF
, bG
You want Entity A
to use bF
and Entity B
to use aB
and aC
So you could deign Entity C
in the way it would have aB
, aC
and bF
.
Then your Entity A
would extend Entity C
with aA
and Entity B
would extend Entity C
with bE
and bG
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.