/** * This class contains unit tests for validating the behavior of Apex classes * and triggers. * * Unit tests are class methods that verify whether a particular piece * of code is working properly. Unit test methods take no arguments, * commit no data to the database, and are flagged with the testMethod * keyword in the method definition. * * All test methods in an organization are executed whenever Apex code is deployed * to a production organization to confirm correctness, ensure code * coverage, and prevent regressions. All Apex classes are * required to have at least 75% code coverage in order to be deployed * to a production organization. In addition, all triggers must have some code coverage. * * The @isTest class annotation indicates this class only contains test * methods. Classes defined with the @isTest annotation do not count against * the organization size limit for all Apex scripts. * * See the Apex Language Reference for more information about Testing and Code Coverage. */ @isTest private class CheckInspectionSubmitUserTriggerTest { private static User u1 { get; set; } private static User u2 { get; set; } private static User u3 { get; set; } private static void init() { Profile p = [select id from Profile where id =:System.Label.ProfileId_SystemAdmin]; u1 = new User(); u1.LastName = '_サンブリッジ'; u1.FirstName = 'あ1'; u1.Alias = 'あ1'; u1.Email = 'olympusTest01@sunbridge.com'; u1.Username = 'olympusTest01@sunbridge.com'; u1.CommunityNickname = 'あ1'; u1.IsActive = true; u1.EmailEncodingKey = 'ISO-2022-JP'; u1.TimeZoneSidKey = 'Asia/Tokyo'; u1.LocaleSidKey = 'ja_JP'; u1.LanguageLocaleKey = 'ja'; u1.ProfileId = p.id; u1.Job_Category__c = '销售服务'; insert u1; u2 = new User(); u2.LastName = '_サンブリッジ'; u2.FirstName = 'あ2'; u2.Alias = 'あ2'; u2.Email = 'olympusTest02@sunbridge.com'; u2.Username = 'olympusTest02@sunbridge.com'; u2.CommunityNickname = 'あ2'; u2.IsActive = true; u2.EmailEncodingKey = 'ISO-2022-JP'; u2.TimeZoneSidKey = 'Asia/Tokyo'; u2.LocaleSidKey = 'ja_JP'; u2.LanguageLocaleKey = 'ja'; u2.ProfileId = p.id; u2.Job_Category__c = '销售服务'; u2.ManagerId = u1.Id; insert u2; /* u3 = new User(); u3.LastName = '_サンブリッジ'; u3.FirstName = 'あ3'; u3.Alias = 'あ3'; u3.Email = 'olympusTest03@sunbridge.com'; u3.Username = 'olympusTest03@sunbridge.com'; u3.CommunityNickname = 'あ3'; u3.IsActive = true; u3.EmailEncodingKey = 'ISO-2022-JP'; u3.TimeZoneSidKey = 'Asia/Tokyo'; u3.LocaleSidKey = 'ja_JP'; u3.LanguageLocaleKey = 'ja'; u3.ProfileId = p.id; insert u3; */ } static testMethod void myUnitTest() { init(); Inspection_Report__c ir; System.runAs(u2){ Account hp = new Account( Name = 'test hp', RecordTypeId = [select Id from RecordType where IsActive = true and SObjectType = 'Account' and DeveloperName = 'HP'].Id, OwnerId = u2.Id ); insert hp; ir = new Inspection_Report__c( Name = '*', Inspection_Date__c = Date.today(), Hospital__c = hp.Id, Reporter__c = u2.Id, Manual_Department__c = 'test', Inspection_StartTime__c = Datetime.now(), Inspection_EndTime__c = Datetime.now().addHours(1), Phone__c = '11112222', Responsible_Person__c = 'test', Status__c = '草案中' ); insert ir; } //User u = [select Id from User where Id = :UserInfo.getUserId()]; System.runAs(u1) { ir.Status__c = '填写完毕'; try { update ir; } catch (Exception e) { } } } static testMethod void testChangeHp() { init(); // 病院作成 Account hp1 = new Account( Name = 'test hp1', RecordTypeId = [select Id from RecordType where IsActive = true and SObjectType = 'Account' and DeveloperName = 'HP'].Id ); Account hp2 = new Account( Name = 'test hp2', RecordTypeId = [select Id from RecordType where IsActive = true and SObjectType = 'Account' and DeveloperName = 'HP'].Id ); insert new Account[] {hp1, hp2}; // 戦略科室取得 Account strategicDep1 = [SELECT ID, Name FROM Account WHERE parentId = :hp1.Id AND recordType.DeveloperName = 'Department_Class_OTH']; Account strategicDep2 = [SELECT ID, Name FROM Account WHERE parentId = :hp2.Id AND recordType.DeveloperName = 'Department_Class_OTH']; // 診療科作成 Account dep1 = new Account( RecordTypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id, Name = 'test dep1', ParentId = strategicDep1.Id, Department_Class__c = strategicDep1.Id, Hospital__c = hp1.Id ); Account dep2 = new Account( RecordTypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id, Name = 'test dep2', ParentId = strategicDep2.Id, Department_Class__c = strategicDep2.Id, Hospital__c = hp2.Id ); insert new Account[] {dep1, dep2}; // 製品 Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',Asset_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',ProductCode_Ext__c='pc01',Manual_Entry__c=false); insert pro1; // 保有設備 Asset asset1 = new Asset( SerialNumber = 'ass01', Name = 'ass01', AccountId = dep1.Id, Department_Class__c = strategicDep1.Id, Hospital__c = hp1.Id, Product2Id = pro1.Id ); insert asset1; Inspection_Report__c ir = new Inspection_Report__c( Name = '*', Inspection_Date__c = Date.today(), Hospital__c = hp1.Id, Reporter__c = u2.Id, Manual_Department__c = 'test', Inspection_StartTime__c = Datetime.now(), Inspection_EndTime__c = Datetime.now().addHours(1), Phone__c = '11112222', Responsible_Person__c = 'test', Status__c = '草案中' ); insert ir; Inspection_Item__c ii = new Inspection_Item__c( Inspection_ReportId__c = ir.Id, AssetId__c = asset1.Id ); insert ii; try { ir.Hospital__c = hp2.Id; update ir; // 必ず失敗、下のAssertに入らないはず System.assertEquals(0, 1); } catch (Exception e) { } asset1.AccountId = dep2.Id; update asset1; try { ir.Hospital__c = hp2.Id; update ir; } catch (Exception e) { // 必ず成功、下のAssertに入らないはず System.assertEquals(0, 1); } } }