As far as I know, instanceof in Apex has always returned true when the operand is null, which is not consistent with Java which will return false.
The instanceof documentation doesn't explictly address the situation where the operand is null.
For what it's worth, I found this blog post which observed it in 2010.
According to the Winter 15 Release Notes this is scheduled to change. Note that this isn't the finalised release so is subject to change.
instanceof
keyword:
Versioned behavior change when instanceof is used with a null object
In Apex saved with API version 32.0 and later, instanceof returns
false if the left operand is a null object. For example, the following
sample returns false.
Object o = null;
Boolean result = o instanceof Account;
System.assertEquals(false, result);
In API version 31.0 and earlier, instanceof returns true in this case.
I believe user data is accessible, although I have no idea what may cause you problem I will try help you with your code.
This :
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
System.runAs(thisUser) {
is pointless. I even think it may cause some distraction, as you change context from current user to current user...
Creating user in test should not be done in future method - its just wasting of resources (Test.startTest()/Test.stopTest()). If there are any problems with this - investigate and fix, because there is some bug.
Testing - judging by your code there is unhandled exception in trigger, handle it and check number of successfully inserted records instead of exception text.
Try something like this(it is not working code) :
@isTest(seeAllData=true)
public class IncidentLimit_Test {
public static testmethod void testcountincident(){
User usr = CreateUser('Some firstName','Some lastName', 'some profile Id collected by other methode ');
insert usr;
System.runAs(thisUser) {
Test.startTest();
List<Incident__c> inclist = new List<Incident__c>();
for(integer i=1;i<=5;i++)
{
inclist.add(new Incident__c(client__c=u.id,Description__c='incident'+i));
}
insert inclist;
Test.stopTest();
}
// assert here }
public static UserRole CreateUserRole(String name) {
UserRole ur = new UserRole();
ur.Name = name;
return ur;
}
public static User CreateUser(String firstname, String lastname, String profileId) {
UserRole ur = CreateUserRole('Testing');
User usr = new User(FirstName = firstname,
LastName = lastname,
Username = firstname + '.' + lastname + '@testing.com',
IsActive = true,
UserRoleId = ur.Id,
Email = 'testuser@testing.com',
Alias = 'tusr',
TimeZoneSidKey = 'Europe/Berlin' ,
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
ProfileId = profileId,
LanguageLocaleKey = 'en_US'
);
return usr;
}
Best Answer
To write
null
type, you should use:or
There is a separate method given. You can not write a
String
by saying itnull
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_System_JsonGenerator.htm#apex_class_System_JsonGenerator