I am writing test scripts with Rest-Assured java. I have two classes ApiMethods and AppMethods belonging to one package and another test package called apiTests using the methods created in the ApiMethods and AppMethods classes. I am getting error (Can't invoke public void apiTests.TestCase.caseApi(): either make it static or add a no-args constructor to your class) while running the test class file.
AppMethods class looks like this:
package com.sape.utilMethods;
import com.sape.base.Base;
public class AppMethods extends Base{
private String MethodA(String abc) {
//Some code
//return statement
}
}
ApiMethods class looks like this:
package com.sape.utilMethods;
import com.sape.base.Base;
public class ApiMethods extends Base{
public AppMethods appObj;
public ApiMethods(AppMethods appObj) {
this.appObj = appObj;
}
public String MethodB() {
appObj.MethodA();
}
}
The test case class under apiTests package looks like this:
package apiTests;
import com.sape.base.Base;
import com.sape.utilMethods.ApiMethods;
import com.sape.utilMethods.AppMethods;
import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;
import io.restassured.response.Response;
public class TestCase extends Base {
public AppMethods appObj;
public ApiMethods apiMethods;
public TestClass(AppMethods appObj) {
this.appObj = appObj;
this.apiMethods= new ApiMethods(appObj);
}
@Test
public void caseApi() {
apiMethods.MethodB();
}
}
It seems I am doing something incorrectly in the caseApi method, while creating ApiMethods object. Can someone help me in passing the data correctly? I am bit new to java so sorry if this question is something very basic
1 Answer 1
Why you are initializing the AppMethods
ApiMethods
inside the testclass constrcutor this is not right way testng works only with the class which has constructor with no arguments
You can use @BeforeTest
for intilizing the classes so that your testclass.java
will be as shown below
public class TestClass extends Base {
public AppMethods appObj;
public ApiMethods apiMethods;
@BeforeTest
public void start(){
appObj =new AppMethods();
apiMethods= new ApiMethods(appObj);
}
@Test
public void caseApi() {
apiMethods.MethodB();
}
}
-
Specifically, a
class
only has a no-arg constructor if 1) it specifically declares it, such aspublic TestCase() { /* .... /* }
or 2) it has no other constructor. Mohamed's solution opts for the second, which is arguably the simplest way in vanilla Java.2020年08月03日 20:04:46 +00:00Commented Aug 3, 2020 at 20:04
Explore related questions
See similar questions with these tags.
TestCase
class. Where is it?