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

  • Your error message mentions TestCase class. Where is it? – Alexey R. Aug 3 at 17:46
  • Corrected the question – Sarang123 Aug 3 at 19:00
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();           
        
    }
}
| improve this answer | |
  • 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. – corsiKa Aug 3 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.