Created
August 19, 2018 12:11
-
-
Save amitastreait/326027a11bbebaa54d0ace1532f785f1 to your computer and use it in GitHub Desktop.
How to Test ApexRest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class BookManager_UnitTest { | |
public static testMethod void unitTest_PostMethod(){ | |
RestRequest request = new RestRequest(); | |
request.requestURI = '/services/apexrest/v1/BookManagement/'; | |
request.httpMethod = 'POST'; | |
request.addHeader('Content-Type', 'application/json'); | |
request.addHeader('Accept', 'application/json'); | |
//request.addHeader('Authorization', 'Bearer '+Userinfo.getSessionId()); | |
BookManager.BooksInfo bInfo = new BookManager.BooksInfo(); | |
bInfo.book = new Book__c(Name='test Book', Price__c = 123.98); | |
String requestBody = JSON.serialize(bInfo); | |
request.requestBody = Blob.valueOf(requestBody); | |
Test.startTest(); | |
Book__c book = BookManager.doCreateBook(bInfo); | |
System.assertEquals('test Book', book.Name); | |
System.assertNotEquals(null, book.Id); | |
Test.stopTest(); | |
} | |
public static testMethod void unitTest_GetMethod(){ | |
Id bookId = createBookRecord(); | |
RestRequest request = new RestRequest(); | |
request.requestURI = '/services/apexrest/v1/BookManagement/'; | |
request.httpMethod = 'GET'; | |
request.addHeader('Content-Type', 'application/json'); | |
request.addHeader('Accept', 'application/json'); | |
request.addParameter('Id', bookId); | |
RestContext.request = request; | |
Test.startTest(); | |
Book__c book = BookManager.doGetBook(); | |
System.assertEquals('Another book', book.Name); | |
System.assertNotEquals(null, book.Id); | |
Test.stopTest(); | |
} | |
public static testMethod void unitTest_UpdateMethod(){ | |
Id bookId = createBookRecord(); | |
RestRequest request = new RestRequest(); | |
request.requestURI = '/services/apexrest/v1/BookManagement/'; | |
request.httpMethod = 'POST'; | |
request.addHeader('Content-Type', 'application/json'); | |
request.addHeader('Accept', 'application/json'); | |
//request.addHeader('Authorization', 'Bearer '+Userinfo.getSessionId()); | |
request.requestBody = Blob.valueOf('Update book Name'); | |
request.addParameter('Id', bookId); | |
RestContext.request = request; | |
Test.startTest(); | |
Book__c book = BookManager.doUpdateBook('Update book Name'); | |
System.assertEquals('Update book Name', book.Name); | |
System.assertNotEquals(null, book.Id); | |
Test.stopTest(); | |
} | |
public static testMethod void unitTest_DeleteMethod(){ | |
Id bookId = createBookRecord(); | |
RestRequest request = new RestRequest(); | |
request.requestURI = '/services/apexrest/v1/BookManagement/'; | |
request.httpMethod = 'GET'; | |
request.addHeader('Content-Type', 'application/json'); | |
request.addHeader('Accept', 'application/json'); | |
request.addParameter('Id', bookId); | |
RestContext.request = request; | |
Test.startTest(); | |
String deletedMessage = BookManager.doDeleteBook(); | |
System.assertEquals('Record Deleted!', deletedMessage); | |
Test.stopTest(); | |
} | |
public static Id createBookRecord(){ | |
Book__c book = new Book__c(Name ='Another book'); | |
insert book; | |
return book.Id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment