1 00:00:00,000 --> 00:00:02,230 [No Audio] 2 00:00:02,230 --> 00:00:03,442 In this video, we're going to explore 3 00:00:03,442 --> 00:00:04,604 a really good way to structure those 4 00:00:04,604 --> 00:00:06,239 unit test methods which you write, and that's 5 00:00:06,239 --> 00:00:09,094 with the so called Arrange/ Act/ Assert paradigm. 6 00:00:09,094 --> 00:00:10,844 So let's have a look at this now together. 7 00:00:10,844 --> 00:00:13,088 So where assertions are useful in general 8 00:00:13,088 --> 00:00:15,358 are at the end of a test. So this kind kind of brings 9 00:00:15,358 --> 00:00:17,194 us onto the structure of tests. 10 00:00:17,194 --> 00:00:19,940 So tests in general, are structured into 11 00:00:19,940 --> 00:00:22,304 what's known as AAA formula, and that 12 00:00:22,304 --> 00:00:24,664 stands for Arrange/ Act/ Assert. 13 00:00:24,664 --> 00:00:26,842 So for example, if we just put the comments 14 00:00:26,842 --> 00:00:31,664 here, arrange, act and assert, this is basically the 15 00:00:31,664 --> 00:00:34,308 format that your tests should be taking. So a 16 00:00:34,308 --> 00:00:37,198 unit test in general will be split into three sections, 17 00:00:37,198 --> 00:00:40,054 within the test method. We've got an arranged 18 00:00:40,054 --> 00:00:42,140 section, and that's basically where we're doing any 19 00:00:42,140 --> 00:00:44,103 setup which we need to do. So that's 20 00:00:44,103 --> 00:00:46,532 creating objects for the system and the test might 21 00:00:46,532 --> 00:00:49,556 need, setting any data up that we need, setting up 22 00:00:49,556 --> 00:00:52,676 any mocks or mock objects with any stubbed behavior 23 00:00:52,676 --> 00:00:54,884 or canned answers that we want to return. 24 00:00:54,884 --> 00:00:56,834 Join the test, those kinds of things. 25 00:00:56,834 --> 00:01:00,031 That's the arrange section of the unit test. 26 00:01:00,031 --> 00:01:02,974 Then in the act section, we generally 27 00:01:02,974 --> 00:01:04,786 just have one line here, and that's 28 00:01:04,786 --> 00:01:06,757 basically the method that's been tested. It's 29 00:01:06,757 --> 00:01:08,882 that method being invoked, with whatever test 30 00:01:08,882 --> 00:01:10,940 input we want it to run with for this test. 31 00:01:10,940 --> 00:01:12,956 And then after that one method call to the 32 00:01:12,956 --> 00:01:15,298 system under test, we have this assert section. 33 00:01:15,298 --> 00:01:17,972 And this is basically going to be a few assertions 34 00:01:17,972 --> 00:01:20,324 using that JUnit assertions API which we've just 35 00:01:20,324 --> 00:01:22,654 seen, usually in conjunction with a matcher library 36 00:01:22,654 --> 00:01:25,290 like Hamcrest, you're going to have those assertions 37 00:01:25,290 --> 00:01:27,932 which effectively together check the state of the 38 00:01:27,932 --> 00:01:30,232 world after the method's been executed, with the 39 00:01:30,232 --> 00:01:31,822 test input that it was supplied. 40 00:01:31,822 --> 00:01:33,070 So yeah, three sections. 41 00:01:33,070 --> 00:01:34,774 Arrange/ Act/ Assert. 42 00:01:34,774 --> 00:01:36,464 Always remember AAA, Arrange, Act, 43 00:01:36,464 --> 00:01:38,528 Assert, and sometimes as well by the way, 44 00:01:38,528 --> 00:01:40,736 Arrange/ Act/ Assert can be thought of as 45 00:01:40,736 --> 00:01:43,603 Given-When-Then, which is popular in BDD 46 00:01:43,603 --> 00:01:46,330 methodology. That's behavior driven development. 47 00:01:46,330 --> 00:01:48,128 So you might see this being used as well. 48 00:01:48,128 --> 00:01:50,686 But for now, as an implementer, a developer of the unit 49 00:01:50,686 --> 00:01:53,324 tests, if you stick to Arrange/ Act/ Assert and always 50 00:01:53,324 --> 00:01:55,150 have that in your mind when you're writing your tests. 51 00:01:55,150 --> 00:01:56,650 And that should give you a good template 52 00:01:56,650 --> 00:01:58,256 you can use to structure those tests when 53 00:01:58,256 --> 00:02:00,180 you're cranking out those test cases. 54 00:02:00,180 --> 00:02:02,180 Yeah, Arrange/ Act/ Assert always bear that 55 00:02:02,180 --> 00:02:04,500 in mind and stick to it all the time.