1

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

asked Aug 3, 2020 at 17:13
2
  • Your error message mentions TestCase class. Where is it? Commented Aug 3, 2020 at 17:46
  • Corrected the question Commented Aug 3, 2020 at 19:00

1 Answer 1

2

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(); 
 
 }
}
answered Aug 3, 2020 at 18:34
1
  • Specifically, a class only has a no-arg constructor if 1) it specifically declares it, such as public TestCase() { /* .... /* } or 2) it has no other constructor. Mohamed's solution opts for the second, which is arguably the simplest way in vanilla Java. Commented Aug 3, 2020 at 20:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.