1

I was given this test code on here, and I'm having some problems with it.

UPDATE : This is the current code with the static fix for generateTestUser()

public with sharing class AutoCreateSubsTriggerTest {

    static testMethod void testAutoCreateSubs() {
        // First, set up your test User.
        User testUser = generateTestUser();

        // Generate your position but don't perform DML yet
        Contract_Overview__c contover = new Contract_Overview__c(Subsidiaries_On_Contract__c = testUser.Id);

        // Start your test and insert your contract overview
        Test.startTest();
        insert contover;
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT
                                                 Id
                                             FROM
                                                 Subs_Serviced_On_Contract__c
                                             WHERE
                                                 Contract_Overview__c = :contover.Id
        ];

        // There should be 1 interviewer because only one user ID was provided
        System.assert(1, subs.size(), 'One sub object is expected');
    }

    private static User generateTestUser(){
        // We use the System Admin profile because it will always be there
        Profile sysAdmin = [SELECT 
                               Id 
                            FROM 
                               Profile 
                            WHERE 
                               Name = 'System Administrator'
                            LIMIT 1
        ];

        // Generate the user
        User newUser = new User(
            UserName = '[email protected]',
            LastName = 'Test',
            FirstName = 'Jane',
            Email = '[email protected]',
            phone = '555-555-5555',
            MobilePhone = '555-555-5554',
            Street = '123 Fake Street',
            City = 'Los Angeles',
            State = 'CA',
            PostalCode = '12345',
            CommunityNickName = 'unit.test',
            Alias = 'abcd',
            ProfileId = sysAdmin.Id,
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles'
        );

        // Insert that user
        insert newuser;

        return newuser;
    }
}

....and now I'm getting that same type of error :

Save Error : Method does not exist or incorrect siganure: System.assert(Integer, Integer, String)


Save error: Method does not exist or incorrect signature: generateTestUser()

that is referencing this line (25) :

System.assert(1, subs.size(), 'One sub object is expected');

One other question, are these lines correct at the bottom :

insert newuser;

return newuser;

..or should they be :

insert user;

insert user;

Thanks for everything. I really appreciate the time & effort.

2 Answers 2

3

generateTestUser() is not a static method so you cannot call it from your static test method. Either make generateTestUser() static (preferred I would think) or create an instance of AutoCreateSubsTriggerTest and access it as instance.generateTestMethod().

For example:

public with sharing class AutoCreateSubsTriggerTest {

    static testMethod void testAutoCreateSubs() {
        User testUser = generateTestUser();
        // your code
    }

    private static User generateTestUser(){
        // your code
    }
}

Note the static keyword between private and User. Also note, you can only make a variable or method 'static' if it is at the top level, i.e. you cannot make a method or variable static if it is a child/nested class.

11
  • Yes, this should be a static method. Sorry I missed that when I originally wrote that stuff up in the other question @Zoom_v. Commented Mar 20, 2013 at 15:40
  • aah, shameful attempt by me to score booty - not even OP's own code :) Commented Mar 20, 2013 at 16:02
  • dumb rookie question : How can I do that in this code ? I notice it would be within another static method. Sorry, that's probably a very dumb question.
    – Zoom_v
    Commented Mar 20, 2013 at 16:39
  • no probs, there are no dumb questions @Zoom_v. Updated my answer to illustrate. Commented Mar 20, 2013 at 16:55
  • 1
    Should be System.assert(1 == subs.size(), 'One sub object is expected');. The bottom lines are fine. Commented Mar 20, 2013 at 19:34
2

Use System.assertEquals() instead of System.assert(). See the System methods documentation.

To fix the other problem, try decorating your class with an @isTest annotation. That will help with your code coverage and code limits as well.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .