简体   繁体   中英

Is there a way to make a java object visible in all methods in a subclass when it is called from a parent class?

In a test class, I utilized a built-in AppiumDriverLocalService class, used across three methods and the test runs successfully.

 public class AppiumServer {
 AppiumDriverLocalService service;
      
 static String appiumMainJSPath = "/Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js";
      
    
     
 public void startServer() {
 service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
         .withAppiumJS(new File(appiumMainJSPath)));
        
 service.start(); //server starts successfully
 }
    
      
 public void testServer() throws FileNotFoundException {
 System.err.println(("The Server URL is: " + service.getUrl().toString()));
 System.err.println(("Is Server Running? : " + service.isRunning())) //Server is running  
}  




  public void stopServer() {
  if (service.isRunning()) {
  service.stop(); //server successfully stopped
    }
  }

Now, in a subclass, I created an object of the parent class and called the methods from the parent class. If I call all three methods inside one method, the object is visible.

public class LoginTest {

AppiumServer appiumServer = new AppiumServer();


public void startApp() throws MalformedURLException, InterruptedException, FileNotFoundException {
appiumServer.startServer(); //server starts successfully
appiumServer.testServer(); //server is running
Thread.sleep(2000)
appiumServer.stopServer(); //server stops
}

So, this is the problem. If I use the object in multiple methods, it is visible only in the first method and suddenly becomes null in subsequent methods, losing visibility. What could possibly be wrong?

public void startApp() throws MalformedURLException, InterruptedException, FileNotFoundException {
appiumServer.startServer(); //server starts successfully
appiumServer.testServer(); //server is running
}


 public void quitApp() {
 appiumServer.stopServer(); //Throws a NullPointerException. Object visibility is lost
  }

Now, in a subclass, I created an object of the parent class and called the methods from the parent class. If I call all three methods inside one method, the object is visible.

You're confused, or at least using the wrong terms.

This is a subclass:

class Dog extends Animal {}

What you wrote doesn't extend anything - therefore LoginTest is not a subclass of AppiumServer .

I created an object of the parent class and called the methods from the parent class.

So, that's not your parent class. If it was, you would not want to 'make an object' - the idea behind LoginTest extends AppServer is that LoginTest is an AppServer already. If LoginTest then invokes new AppServer() , you have 2 appservers: The LoginTest instance (which is an appserver - just one with some extra behaviours tacked on), and the appserver object you made, clearly not right. If you write class LoginTest extends AppiumServer , you'd invoke these methods on yourself . After all, you are an AppiumServer. (just call startServer() , no foo. in front of it).

Given that LoginTest is most likely not intended to be a specialization of AppiumServer , the problem here is your wording and ideas on what subclasses / parent classes are. You have a class named LoginTest , which makes an AppiumServer instance and invokes a bunch of methods on it. That's all. No subclasses, parent classes, etc are involved in this story.

So why is it null?

In your pasted code it wouldn't be, but it looks like you wrote some new code that you didn't run just for this question.

Test frameworks generally restart for every new test, in order to ensure that the execution order of tests do not affect test results.

That's a good idea and you shouldn't mess with that.

Test frameworks have facilities to have shared info between a bunch of tests. For example, JUnit has the @BeforeClass annotation that is appropriate for this: The setup and teardown of the appiumserver itself should be done there, then each test can just use the appiumServer field.

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.

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