function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
simon chaosimon chao 

bulk test class inquiry

I have written the below controller class. Was wondering how to write a bulk test for it. any suggestions? Are there good guides out there that teaches how to write test class? Besides for the trailhead module?
 
public with sharing class newStudent { 
@AuraEnabled 
public static Contact getStudent(Id stuId) { 
     Contact stu = [ 
                     SELECT Id, Name, Gender__c, School__c 
                     ( 
                        SELECT class_id__c, class_teacher__r.Name, class_subject__c FROM class__r 
                     )
                     FROM Contact WHERE Id =: stuId ]; 
     return stu; 
} 

//https://developer.salesforce.com/blogs/developer-relations/2015/11/create-lightning-component-drag-drop-profile-pictures.html 

@AuraEnabled 
public static Attachment studentPicture (Id stuId) { 
     if (!Schema.sObjectType.Contact.isAccessible()) { 
          throw new System.NoAccessException(); 
     } 
     List<Attachment> lAttachment = new List<Attachment>(
          [
               SELECT Id, Name, LastModifiedDate, ContentType FROM Attachment 
               WHERE parentid=:stuId AND ContentType IN ('image/png', 'image/jpeg', 'image/gif') 
               ORDER BY LastModifiedDate DESC LIMIT 1
          ]
     ); 
     return lAttachment.size() > 0 ? lAttachment[0] : null; 
} 
    @AuraEnabled
    public static Id saveAttachment(Id parentId, String fileName, String base64Data, String contentType) {
        if (!Schema.sObjectType.Contact.isUpdateable()) {
            throw new System.NoAccessException();
        }
        Attachment attachment = new Attachment();
        attachment.parentId = parentId;
        attachment.body = EncodingUtil.base64Decode(base64Data);
        attachment.name = fileName;
        attachment.contentType = contentType;
        insert attachment;
        return attachment.id;
    }
}
Best Answer chosen by simon chao
Raj VakatiRaj Vakati
Try this
 
static testMethod void newStudentTest() { 

        Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
		
		
		Blob b = Blob.valueOf('Test Data');
    	
    	Attachment attachment = new Attachment();
    	attachment.ParentId = c.Id;
    	attachment.Name = 'Test Attachment for Parent';
    	attachment.Body = b;
    	attachment.ContentType 
    	insert(attachment);
		
        Test.StartTest(); 
        List<class_subject__c> stds = new List<class_subject__c>() ;
		for( Integer  i = 0 ;i<10 ; i++){
		class_subject__c  std =new class_subject__c () ; 
		std.Name ='Test' ;
		std.Gender__c ='M';
		std.School__c='est';
		std.Contact__c =c.Id;
		stds.add(std) ; 
		
		}
		//Add All fields 
		insert stds ; 
		newStudent.getStudent(c.Id);
		newStudent.studentPicture (c.Id);
        Test.StopTest();
    }

 

All Answers

Raj VakatiRaj Vakati
Try this
 
static testMethod void newStudentTest() { 

        Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
		
		
		Blob b = Blob.valueOf('Test Data');
    	
    	Attachment attachment = new Attachment();
    	attachment.ParentId = c.Id;
    	attachment.Name = 'Test Attachment for Parent';
    	attachment.Body = b;
    	attachment.ContentType 
    	insert(attachment);
		
        Test.StartTest(); 
        List<class_subject__c> stds = new List<class_subject__c>() ;
		for( Integer  i = 0 ;i<10 ; i++){
		class_subject__c  std =new class_subject__c () ; 
		std.Name ='Test' ;
		std.Gender__c ='M';
		std.School__c='est';
		std.Contact__c =c.Id;
		stds.add(std) ; 
		
		}
		//Add All fields 
		insert stds ; 
		newStudent.getStudent(c.Id);
		newStudent.studentPicture (c.Id);
        Test.StopTest();
    }

 
This was selected as the best answer
simon chaosimon chao
For some reason the code coverage is below 40%, i made some changes to it and it has now doubled. Thank you :)
simon chaosimon chao
how do you test line 32?  
throw new System.NoAccessException();

 
Raj VakatiRaj Vakati
That line you need to test it as System.runAs(User) where user profile dnt have an access to contact 

 
simon chaosimon chao
I see. I can also test it by putting it in the same line as my if statement, that will automatically run it. 
Raj VakatiRaj Vakati
I got thr reason why code coverage is down 
  • Set The Attachdmnet Content Type 
  • Call saveAttachment method in test class