1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Apex RestRequest クラスを使ったテストコード

Last updated at Posted at 2022-03-07

この質問に対する回答を考えます。よっしゃ、モックだな。あれ?エラーになる。悩みましたね。
なんだ、レスポンスじゃない。リクエストの方だけ。ああ、早とちりでした20分悩みました。

Can someone please help me to write test class for the Below code.

ちゃんと85%以上はカバーした。
image.png

モックは要らんので、単にbody(blob型)と必要なパラメータを追加するだけですね。

@IsTest
public class getCaseIDNumber_test {
    
    @IsTest
    public static void testMain(){
        
        Case cs = new Case(Subject = 'Test', Origin = 'Phone', Status = 'New');
        insert cs;
        
        Attachment att = new Attachment();
        att.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        att.body=bodyBlob;
        att.parentId=cs.id;        
        insert att;
        
        RestRequest req = new RestRequest();
        req.requestURI = '/services/apexrest/getContactdetails';
        req.httpMethod = 'GET';
        req.addParameter('Id', att.Id);
        Test.startTest();
        
        test0002 t = new test0002();
        Boolean result = t.getCaseIDNumber(req);
        Test.stopTest();
    }


	@IsTest
    public static void testError(){
        
                
        RestRequest req = new RestRequest();
        req.requestURI = '/services/apexrest/getContactdetails';
        req.httpMethod = 'GET';
        req.addParameter('Id', null);
        Test.startTest();
        
        test0002 t = new test0002();
        Boolean result = t.getCaseIDNumber(req);
        Test.stopTest();
    }
    
    
    
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?