newcoder1010 Posted September 25, 2022 Share Posted September 25, 2022 Hello, public class Base { public String name = null; public String getName() { return name; } public void setName(String myName) { this.name = myName; } } GetData public class GetData extends Base{ public String GetMyData() { return getName(); } } Test: public class CreateAccount extends Base { @Test public void test1() { setName("Mike"); String name = getName(); System.out.println("get name: " + name); // get the value "Mike" GetData getData = new GetData(); name = getData.GetMyData(); System.out.println("get name: " + name); // get null } } Output: Quote get name: Mike get name: null I expected to get "Mike" both cases but I am getting null second time. I could fix it by writing static but I am doing some parallel testing. How can I fix it without writing static? public static String name = null; Link to comment Share on other sites More sharing options...
Ingolme Posted September 25, 2022 Share Posted September 25, 2022 You created a new GetData instance on this line: GetData getData = new GetData(); It currently has nothing in it because you only just created it. You need to call setName() to give it a value before you can read it. Link to comment Share on other sites More sharing options...
newcoder1010 Posted September 25, 2022 Author Share Posted September 25, 2022 Thanks for your reply. I am trying to set name from one class and be able to get name from another class. As you can see, I have set name in @Test method from CreateAccount class . setName("Mike"); I am trying to get the same name from GetData class. How can I do it? Link to comment Share on other sites More sharing options...
Ingolme Posted September 25, 2022 Share Posted September 25, 2022 You can't. They are completely different objects with no connection to each other. If you put the string "Mike" into object A and then ask object B for the string you won't find it there. When you write setName("Mike") it's a shorthand for calling this.setName("Mike") and this is an object. Then you have a completely different object called getData. You haven't written getData.setName("Mike") so it won't give you anything. For your test to make sense you need to write this code: public class CreateAccount { @Test public void test1() { GetData getData = new GetData(); getData.setName("Mike"); String name = getData.GetMyData(); System.out.println("get name: " + name); } } Why exactly do you want two different objects to point to the same data? Link to comment Share on other sites More sharing options...
newcoder1010 Posted September 25, 2022 Author Share Posted September 25, 2022 <!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports --> <dependency> <groupId>com.relevantcodes</groupId> <artifactId>extentreports</artifactId> <version>2.41.2</version> </dependency> BaseTest public class BaseTest extends Base{ public static ExtentReports reporter; public ExtentTest loggerParent = null; // invoked once before I run one or more tests @BeforeSuite public void initializeReport(){ reporter = new ExtentReports("ExtentReport6.html", true, DisplayOrder.OLDEST_FIRST); } // invoked before every @Test method @BeforeMethod public synchronized void beforeMethod(Method method) { System.out.println("beforeMethod"); logger = reporter.startTest(method.getName()); loggerParent.appendChild(logger); setLogger(logger); logger.log(LogStatus.INFO, "before method"); } @BeforeClass public void beforeClass() throws MalformedURLException { ITestResult itr = Reporter.getCurrentTestResult(); String className = itr.getInstance().getClass().getName(); loggerParent = reporter.startTest(className); loggerParent.log(LogStatus.INFO, "Before class"); } @AfterSuite public void afterSuite() { reporter.flush(); } } Base package Base; import com.relevantcodes.extentreports.ExtentTest; public class Base { public ExtentTest logger = null; public void setLogger(ExtentTest logger1) { this.logger = logger1; } public synchronized ExtentTest getLogger() { return logger; } } DataDriven public class DataDriven extends Base{ public synchronized void launchExcelFile(String fileNameWithExtension, String sheetName) { logger.log(LogStatus.INFO, "launch excel"); }} @Test public class CreateAccount extends BaseTest2 { @Test public void test1() { logger.log(LogStatus.INFO, "create account test8"); DataDriven dataDriven = new DataDriven(); dataDriven.launchExcelFile("TestData.xls", "profiles"); }} Error when I run the test: Quote java.lang.NullPointerException: Cannot invoke "com.relevantcodes.extentreports.ExtentTest.log(com.relevantcodes.extentreports.LogStatus, String)" because "this.logger" is null Error is from this line in DataDriven.java logger.log(LogStatus.INFO, "launch excel"); I spent all day but could not fix it yet. I am just not sure how to use the logger in DataDriven file. Thank you in advance! Link to comment Share on other sites More sharing options...
newcoder1010 Posted September 25, 2022 Author Share Posted September 25, 2022 To summerize, I am setting the logger before every test method. Then trying to get the logger from another class. Link to comment Share on other sites More sharing options...
Ingolme Posted September 25, 2022 Share Posted September 25, 2022 You need to call setLogger() logger.log(LogStatus.INFO, "create account test8"); DataDriven dataDriven = new DataDriven(); dataDriven.setLogger(logger); dataDriven.launchExcelFile("TestData.xls", "profiles"); Link to comment Share on other sites More sharing options...
newcoder1010 Posted September 25, 2022 Author Share Posted September 25, 2022 Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now