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
sgsssgss 

SOQL AND TEST CLASS

 Need COde for Following SOQL along with Test Cases

Need SOQL and Test Class for each Question. Any Help will be appreciated.
Thanks
Best Answer chosen by sgss
devedeve
Hello Supriya,

You can write test class for these as follows:-

For 1st query Class:-

public class Query() {
    public List<Contact> contactQuery() {
       return [SELECT Name, Account.Name FROM Contact WHERE Indudtry__c = 'MEDIA' AND Active__c = true];
    } 
}

TestClass:-

@isTest
private class QueryTest {

    @testSetUp static void testSetUp() {
        Contact contact = new Contract();
        contact.Name = 'testContact';
        contact.Industry__c = 'MEDIA';
        contact.Active__c = true;
        insert contact;
    } 

    @isTest static void testContactQuery() {
        Query testController = new Query();

       Test.startTest();
            List<Contact> conList = testController.contactQuery();
        Test.stopTest();

         System.assertEquals(true, conList.size()>0; 'Contact should be returned');
    }

}

You can write testclasses for all other queries in the same way.I hope this will help you.

All Answers

Raj VakatiRaj Vakati
1 . Select Id, Name ,(Select id,Name from Contacts) from Account where Industry='media'
2.Select Id, Name ,(Select id,Name from Contacts where Name='John') from Account where Industry='media'
3.Select Id , Name from Contact where Account.Name='John'
4.Select Id ,Account.Name from Opportunity where StageName='Closed Won'


FIND {"test"} IN ALL Fields RETURNING lead(name, phone), contact(name, phone),Account(name),User(name)
devedeve
Child to Parent,
1. SELECT Name, Account.Name FROM Contact WHERE Indudtry__c = 'MEDIA' AND Active__c = true.
2.SELECT Name, Account.Name, Account.Id FROM Contact WHERE Name = 'John'.

Parent To Child
1. SELECT Id, Name , (SELECT Id,Name FROM Contacts WHERE Name='John') FROM Account WHERE Industry='media'.
2 SELECT Id, Account.Name FROM Opportunity WHERE StageName='Closed Won'.
3. SELECT Id, (SELECT Id,Name FROM Contacts WHERE Name='John') FROM Account

SOSL

FIND {"test"} IN ALL Fields RETURNING Contact(Name, phone), Lead(Name, phone), Account(Name), User(name)

 
sgsssgss
Thanks,
I am clear with SOQL query, need Test Class for the following, If any one can help in Test Class of following questions considering positive and negative test cases.
devedeve
Hello Supriya,

You can write test class for these as follows:-

For 1st query Class:-

public class Query() {
    public List<Contact> contactQuery() {
       return [SELECT Name, Account.Name FROM Contact WHERE Indudtry__c = 'MEDIA' AND Active__c = true];
    } 
}

TestClass:-

@isTest
private class QueryTest {

    @testSetUp static void testSetUp() {
        Contact contact = new Contract();
        contact.Name = 'testContact';
        contact.Industry__c = 'MEDIA';
        contact.Active__c = true;
        insert contact;
    } 

    @isTest static void testContactQuery() {
        Query testController = new Query();

       Test.startTest();
            List<Contact> conList = testController.contactQuery();
        Test.stopTest();

         System.assertEquals(true, conList.size()>0; 'Contact should be returned');
    }

}

You can write testclasses for all other queries in the same way.I hope this will help you.
This was selected as the best answer