Meet JUnit2:56 with Craig Dennis
It's time to get familiar with JUnit the premier solution for unit testing in Java.
[MUSIC] 0:00 Unit testing is popular in just about every programming language. 0:04 We're going to be diving deep into the JUnit testing framework here. 0:08 But you should know that just about all of the concepts 0:12 are implemented in every other testing framework out there. 0:15 There is a whole family of testing frameworks called the xUnit family. 0:19 In fact, JUnit has been quite influential in the testing movement and 0:23 has widespread acceptance. 0:27 So, we're going to be working with a very well-loved, time-tested, mature, and 0:29 ever-evolving framework. 0:34 You'll see its influence in all the other frameworks, 0:36 just like how all those bands from the 90s were pretty much just covering Pearl Jam. 0:39 JUnit has evolved over time right along with Java. 0:44 The way you actually write JUnit tests now is a little bit different than 0:47 when the framework was first created in 1997. 0:51 Now, the birth story of the xUnit family of frameworks is pretty great. 0:53 Two developers, Kent Beck and Erich Gamma were headed to the same conference and 0:57 sitting on a flight next to each other. 1:01 And as coders do, 1:04 they started talking about how there was no good framework for testing. 1:05 So they built a test framework for Smalltalk. 1:09 But get this, they did it using test-driven development. 1:11 I don't know about you, but I would have loved to be the extra seat on that flight 1:15 watching that metamagic happen. 1:18 Today JUnit uses annotations to define tests. 1:21 Obviously though, 1:24 before the annotations were part of the language, this wasn't an option. 1:25 The way they used to work was by following a naming convention of prefixing your text 1:29 with the word test. 1:33 This isn't the case anymore. 1:35 And I'll show you the more newer accepted pattern. 1:37 But again, as I pointed out in the past, 1:39 there's a lot of dated information on the Internet. 1:41 And some of these older patterns might make you think that you're required to do 1:44 things this way. 1:48 Some code completion tools, IntelliJ included, 1:49 end up generating test methods following these outdated ways of writing tests, so 1:52 it doesn't help to add to the confusion. 1:57 As we explore, I'll do my best to call out the old way of doing things, 1:59 and I'd like you to always remember to check the teachers notes. 2:03 All right, so let's talk at a high level about what a testing framework gives you. 2:06 This is what each individual test is called. 2:12 Typically every class you end up attempting to test has many test cases. 2:15 In JUnit, these are implemented as methods annotated with the @Test annotation. 2:19 The methods return void and are grouped together under a parent class. 2:25 That surrounding class around the test cases is known as a test fixture. 2:30 It's responsible for setting things up that are needed for each test. 2:35 A test runner finds and runs all the test cases that you specify. 2:39 When it completes, it provides you with the results of how many tests passed and 2:44 how many tests failed. 2:48 So what do you say we get your test runner installed in IntelliJ? 2:49 You'll be writing test fixtures jammed full of test cases in no time. 2:52
You need to sign up for Treehouse in order to download course files.Sign up