[SalesForce] Illegal assignment from void to String

I am trying to save my test class for a Rest a webservice (GET) but get the error
Illegal assignment from void to String on line 16. If I change the void to string I get a missing return statement string error.

Could someone explain my error?

Testclass

@IsTest
private class InvoiceManagerTest {
    //GET
    @isTest static string testGetInvoice() {
        Invoice__c invoice = createInvoice();
        String query = '?customerId='+ invoice.customerId__c + '&month=' + invoice.invoiceDate__c.month();
        // Set up a test request
        RestRequest request = new RestRequest();
        request.requestUri =
            'https://aaXX.salesforce.com/services/apexrest/invoices/'+
            query;

        request.httpMethod = 'GET';
        RestContext.request = request;
        // Call the method to test
        string thisInvoice = InvoiceManager.getInvoice();
        // Verify results
        System.assert(thisInvoice != null);
        System.assertEquals(
            ' {'+
        '"customerId": "1",'+
        '"addressId": "2018AA12",'+
        '"invoiceId": "00ec5a04a43c014aa9e8",'+
        '"invoiceType": "AdvancePayment",'+
        '"invoiceTypeLocalized": "Voorschot",'+
        '"invoiceDate": "2018-06-01T00:00:00",'+
        '"DueDate": "2018-06-01T00:00:00",'+
        '"invoiceNumber": "157005888",'+
        '"startDate": "2016-07-01T00:00:00",'+
        '"endDate": "2017-07-01T00:00:00",'+
        '"Description": "June 2018",'+
        '"amount": 100.10,'+
        '"vatAmount": 30.30,'+
        '"totalAmount": 130.40'+
        '}'
           , thisInvoice);
    }

    // Helper method
    static Invoice__c createInvoice() {
        // Create test record
        Invoice__c invoiceTest = new Invoice__c(
            customerId__c = '1',
            addressId__c = '2018AA12',
            invoiceId__c = '00ec5a04a43c014aa9e8 ',
            invoiceType__c = 'AdvancePayment',
            invoiceTypeLocalized__c = 'Voorschot',
            invoiceDate__c = Date.valueOf('2018-06-01 00:00:00'),
            paymentDueDate__c = Date.valueOf('2018-06-08 00:00:00'),
            invoiceNumber__c = '157005888',
            startDate__c = Date.valueOf('2015-06-01 00:00:00'),
            endDate__c = Date.valueOf('2020-06-01 00:00:00'),
            periodDescription__c = 'June 2018',
            amount__c = 100.10,
            vatAmount__c = 30.30,
            totalAmount__c = 130.40 );
        insert invoiceTest;
        return invoiceTest;
    } 
}

Best Answer

By my count this:

string thisInvoice = InvoiceManager.getInvoice();

is line 16 which means the getInvoice method (you have not posted) returns void.

You may find that rather than returning a value it is setting the result in the RestContext.response static field and your test can get the value from there:

InvoiceManager.getInvoice();
string thisInvoice = RestContext.response.responseBody.toString();
Related Topic