From the course: Introduction to Test Classes in Salesforce

Testing with a test factory - Salesforce Tutorial

From the course: Introduction to Test Classes in Salesforce

Start my 1-month free trial

Testing with a test factory

- [Instructor] Now that we've finished creating our test factory, we're actually able to actually use it in writing test code. So again, this is going to look very similar to the account trigger handler test that we did using test utilities with a few minor differences. So, first thing we're going to do, line seven, we're going to start off by creating all of our data, and we're going to use this tag, testSetup so that Salesforce calls this method between each of our test classes and gives us a clean set of data. Now here's one of the major differences, notice now our account says Account myAcct, and we're going to typecast the return from the test factory, so remember the test factory's working at the SObject level, so we're going to tell the test factory, I want you to create me a new account, and when it comes back, I'm going to typecast it so I can actually store it as an account. So that's our example if we were just doing a single account. In this case, we're going to do multiple accounts, I'm going to skip this part right now for custom metadata, we will come back to it later and go through the details of what we're doing with custom metadata, but it's a great way to set the defaults for our testing. So, if we come on down to line 51 and 52, because we have two different types of accounts we're creating, we're going to create a customer account and we're going to create a business owner account, so the first thing we're going to do is we're going to do a list of accounts, we're going to typecast that to a list, and we're going to call our test factory with create this list of accounts, and the number, and in this case I happen to number is going to be five, because I'm going to create five user accounts and five business accounts. The business accounts was that new default value, so if I'm just creating accounts, it's going to use my account object in my test factory, in this case, I want them to have a different record type, so I'm actually specifying when this method is executed, use the AcctBizDefault. We're do the same thing, we're going to take our five customer accounts, we update some information on there that we want it to update, this is where we may still have a little bit of customization, this type, the account number, and the description could be done in the test factory, it may not need to be done in the test factory, if not, we can do it right here. We're going to create our 10 contacts, so very much similar to what we did last time, and we're going to go through here and associate our contacts with our account. And we have thus completed creating our test data. We're now ready to create and execute our test method, so MyAccountTest method One, remember we've already got our accounts created, and again, I'm skipping the metadata, we'll come back to that in a later chapter. So, I'm going to go ahead and query MyAccounts, and I'm going to go ahead and query MyCons, and in both cases, I'm doing System.asserts to make sure I have exactly the number of accounts and the number of contacts that I'm expecting. I'm then going to query my accounts and get the ones where this Account_Text_Info is null, I'm going to do that query, again, I'm expecting it to be 10. I'm going to go through and update my accounts, I'm going to query myCons again and this time I expect it to be zero, again, exactly what I did in the test utility example, but this time I'm using a test factory to create the data and run the test. In myAccountTestTwo method, I'm actually doing the exact same thing again, where I'm going through and looking at myAccounts, so you can see on line 160, I'm creating myCons. I'm going to come back to lines 146 through 157. One of the things we're going to look at a little bit later on is how to test based on a given user, so I will actually create the user, create the profile, and then run as user, but for now we're not going to do that, we're just running our test as though it was the same as the one above, when we execute that, we would expect it to work exactly the same way. So again, we go over here, let's go to Run Configuration, we're already in our devClass2, we're going to find our test factory test class, we're going to select the methods, apply that, hit Run, and as we can see, both of them passed, and we have 76% coverage, so identical to what we did before, but just a way to show you how to use a test factory instead of a test utility. So, before we leave, one thing I really want to stress here is it is okay, as we're showing here, to add in some additional information in the test class, so where we're setting an account number on our account, where we're setting the description, however, we don't want to create an account without actually going either to our test utility or our test factory, and in this case, the test factory. Everything that we can possibly do in the test factory, we need to do that, so we need to create all the fields that must have values, they should be done in the test factory. Anything that we specifically want to test for for this given test, for example, maybe AccountNumber, it's okay to do it here, if not, go ahead and do that in the test factory as well.

Contents