How to use system.runAs()?

Here is the example to use System.runAs() in apex test class: For example we have a class to create the campaign only if logged in user is marketing profile otherwise throwing error.

// Apex Trigger

trigger campaignBeforeInser on Account (before insert) {
for(Campaign campaign:{
if(userInfo.getProfileId == ‘marketing profile Id’){
campaign.currency = ‘USD’;
campaign.addError(‘you are not authorized to create campaign’);

//Here is the Test class for above trigger.

private class CampaignTriggersTest{
private static testmethod void campaignTriggersTest(){
Profile prof = [select id from profile where name LIKE '%marketing%'];
User user = new User();
user.firstName = ‘test1’;
user.lastName = test2;
user.profileId =,username = ‘’; = ‘’;
insert user;
Campaign campaign = new Campaign(); = ‘laptop’;
insert campaign();