2

I have a test like below. and the error: Method does not exist or incorrect signature: void doPost() from the type MyOriginalClass In MyOriginalClass, the method has parameter like doPost(String url) and this method should be giving meback a tag.

How can I resolve this error? Please help me.

@isTest
public class MyOriginalClassTest {
    @isTest (SeeAllData = true)static void test1(){
       
        RestRequest request = new RestRequest();
        RestResponse response = new RestResponse();
        
        request.requestURI = 'https://xxx.salesforce.com/services/apexrest/xxx';
        request.requestBody = Blob.valueOf('{"url":"https://xxx/api.html"}');
        request.addHeader('Content-Type', 'application/json');       
        request.httpMethod = 'POST';
        RestContext.request = request;
        RestContext.response = response;

        MyOriginalClass.doPost();
        
        System.assertNotEquals(NULL, response);
        
    }

}
1
  • 2
    There are very few circumstances in which you should write test classes with seeAllData=true. Your tests should create their own data. Additionally, your assertion is a tautology because you initialize response to a non-null value.
    – David Reed
    Commented Sep 7, 2020 at 1:55

1 Answer 1

1

Your class Method has a definition that has a Parameter of string type as shown below

 doPost(String url)

This means you will need to pass the URL when you invoke the method.

Change your apex test class as below

@isTest
public class MyOriginalClassTest {
 @isTest static void test1(){
   
    RestRequest request = new RestRequest();
    RestResponse response = new RestResponse();
    
    request.requestURI = 'https://xxx.salesforce.com/services/apexrest/xxx';
    request.requestBody = Blob.valueOf('{"url":"https://xxx/api.html"}');
    request.addHeader('Content-Type', 'application/json');       
    request.httpMethod = 'POST';
    RestContext.request = request;
    RestContext.response = response;

    MyOriginalClass.doPost('https://example.com'); // Make sure you pass the url
    System.assertNotEquals(NULL, response);
    
   }

 }

As mentioned in comment do not use seeAllData and also add better assertions.

1
  • 1
    Thank you so much, Mohith!! You saved my day!!
    – rosa
    Commented Sep 7, 2020 at 1:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .