Weather App in Need of Tests9:07 with Chris Ramacciotti
The project we'll be working with in this workshop is a Spring web app that utilizes the forecast.io API and a couple Google APIs. This video introduces you to that project and shows you how to get it up and running on your own machine.
Sync Your Code to the Start of this Video
git checkout -f v2
Using Git with this Workshop
See the README on Github for information on how to use Git with this repo to follow along with this workshop.
Forking on Github
Extending this project to make it your own is a wonderful way for you to take your learning and experience to the next level. Consider forking the project on Github!
Consuming a REST API with Spring's RestTemplate
The question you'll keep asking yourself when coding an application is, 0:00 does this thing actually work? 0:03 I mean, does it really work? 0:05 Unit tests help us answer that question by allowing us to test whether or 0:08 not the individual components of our application work. 0:11 Now, this is separate from whether or not two components talk nicely with each other 0:14 or whether or not the entire application works as a whole. 0:19 Those questions are the subjects of integration and end to end testing. 0:22 We'll be covering unit tests in this course with a brief peek at 0:26 integration testing. 0:30 Now in this course we'll be using mostly the spring test framework and 0:31 JUnit to test our app. 0:35 As the cherry on top we'll pull in some third party goodies to test our database 0:37 interactivity at the end. 0:41 But before we start looking at how to test controllers, services, and DAOs, 0:43 let's fire up the project we'll be writing. 0:47 The project is hosted here on GitHub as you see and is tagged for 0:50 the beginning of each video. 0:53 Check the teacher's notes for updating your local repo to match my code. 0:55 So that I can get a local copy of this repo on my machine, 0:59 I'm going to copy the SSH URL from right here. 1:03 And then I'll open a terminal window and change directories to the one where I want 1:07 to store this project, then clone the get repo into its own directory. 1:11 So I'll get clone and I'll paste that SSH URL here and 1:16 what this does is that it will download the whole project into a directory that's 1:21 named the same as the GitHub repo itself, spring-unit-test-weather. 1:26 So if I do a quick ls, I can see that that repo made it in to my directory here. 1:32 Excellent, okay, let me now hop over to IntelliJ to import the project. 1:39 So I'll just open this project since it was already configured as 1:45 an IntelliJ project. 1:49 And mine is under the Code directory. 1:51 Also under Screencasts and there it is right there. 1:53 I'll click OK and I'll go ahead and accept all the defaults here. 1:57 Click OK and let IntelliJ build that project and 2:01 soon the IDEA editor should fire up ready for 2:05 us to code Excellent. 2:09 There is our project. 2:14 I'll go ahead and add my version control route there. 2:17 Since this is a Gradle project, I'm going to open the Gradle tool window and 2:23 refresh dependencies, so I can get those pulled down. 2:26 Hit the refresh and you'll see these this output here at the bottom showing that 2:30 dependencies are downloading and looks like that finished fairly quickly. 2:35 Now before you run the boot run tasks you'll need to add API keys to 2:39 the api.properties file, that's api.properties right here. 2:44 Let me close this Gradle tool window. 2:48 This project uses the Google geocoding and places API to translate a search location, 2:51 like a city or zip code, into latitude and 2:56 longitude coordinates, as well as get coordinates for saved places. 2:59 The projects then use the forcast.io API to get live weather data. 3:03 So you'll need to sign up for API keys for each of those services and 3:07 I've put these instructions in the teacher's notes. 3:11 After you have your keys paste them here, here, and here. 3:14 Now I'm going to paste my own keys behind the scenes. 3:22 Okay, I paste my API keys into the API.properties file and I've closed that. 3:27 If you haven't guessed already, this is a weather app that will allow a user 3:32 to see live forecast and weather data for a searched location. 3:36 Let's run the boot run task and see this thing in action. 3:41 So I'll open the Gradle tool window. 3:44 And I'll go under Tasks > Application and double click Boot Run. 3:46 And I'll close my Gradle tool window. 3:50 And there we see our spring boot output. 3:54 All right. 3:58 Looks like the application has successfully started. 3:59 So let's go back to our browser, open a new tab and open localhost:8080. 4:03 Sweet. 4:09 And here's the app. 4:10 So we can search by zip code, I'll use mine, 60657. 4:11 And here we see up to date weather data for Chicago. 4:15 It shows the current temperature and this nifty little icon for 4:20 the current conditions. 4:23 No forecast data's on display yet, but you can definitely take this app and 4:25 make it your own. 4:28 Heck, you could even fork this project on GitHub and take it to the moon! 4:29 Literally displaying the moon phases and such. 4:33 Seriously though, check out the teacher's notes for info about forking on GitHub. 4:36 If I click this star, I see that I must be logged in. 4:41 So I'll go ahead and do that. 4:44 The username is user and the password is password. 4:46 After logging in, I can search for that zip code again, use the same one, 60657. 4:51 And when I click the star, it is added to my favorites and 4:57 I see that flash message indicating that that is the case. 5:01 I can also look at my list of favorites by clicking on the My Favorites link. 5:05 And there it is, Chicago, Illinois. 5:08 And that's about it for the project as it exists. 5:11 Now let's switch back to IntellJ and take a spin through the code. 5:14 I'm going to collapse this panel here at the bottom and 5:19 the first thing I want to point out is the software stack that we're using. 5:22 I'll open the Gradle build file, build.gradle. 5:25 So I can point out the dependencies as they apply. 5:28 So let me scroll down to those dependencies. 5:32 I'll hide this tip here. 5:35 Let me scroll down here and as you can see this is a spring app 5:36 that uses thymeleaf as a template engine and hibernate as an ORM. 5:43 Spring security is present here for user authentication. 5:49 And notice that I've listed h2 as a runtime dependency 5:53 since we aren't leveraging the library in any of our source code. 5:58 The tests compile dependencies list dependencies that are part of the class 6:02 path for testing source code which is the main subject of our workshop here. 6:06 The first two dependencies give us the libraries necessary for 6:10 testing a spring app and spring authentication respectively. 6:14 That's the spring boot starter test dependency and 6:19 the spring security test dependency. 6:22 The next two dependencies give us a library called dbunit and allow us 6:25 to integrate that testing library into spring, in order to test our data layer. 6:30 We'll get to this at the end of the workshop when we test our DAOs. 6:37 And last but not least, junit. 6:40 I think that one speaks for itself. 6:44 Now moving on to the application code, we have a layout of an MVC application. 6:46 As you can see, if I go to the main package here, a com.treamtreehouse 6:51 package, I’ve got an MVC application here with packages for models, 6:55 that's the domain package, a directory for thymeleaf templates is the views. 7:01 That's under Resources > Templates. 7:05 And a package for controllers. 7:08 That's under web, web.controller is the package there. 7:10 This app also implements the service DAO pattern as you can see with packages for 7:15 services and for DAOs. 7:19 One note about our services is that included in these are three services, 7:22 the geocoding service, the places service, and 7:27 the weather service you can see down here. 7:29 All of which leverage external APIs using Spring's REST template. 7:32 Check out the teacher's notes for a more on REST template. 7:37 Essentially, that allows us to easily communicate with external APIs. 7:41 And one thing worth pointing out about the DAOs is that we are leveraging Spring data 7:46 JPA. 7:51 Let me show you that here. 7:52 If I open the UserDao, 7:53 I see that this is an interface extending the CrudRepository Interface. 7:56 And I don't actually have implementations in the Dao package. 8:02 I only have interfaces and this is because Spring data is going to generate 8:07 the implementations of these interfaces when I boot the application. 8:13 Now the last thing I want to point out is that for 8:19 authorization, that is checking user roles, I'm using the pre-authorized 8:21 annotations in place of configuring the Spring security filter chain. 8:26 Let me open the FavoriteController and show you what I mean. 8:29 What we can do is apply the pre-authorized annotation to enforce 8:34 authorization before beginning the execution of the controller method. 8:38 And notice here I specify the authorization method I'd like to call 8:42 the hasRole method. 8:46 Here I want to ensure the authenticated user has the ROLE_USER role. 8:49 Okay that'll wrap up our speedy breeze through the application. 8:55 Before moving on to the next video I highly suggest you take a few minutes to 8:59 look more thoroughly at our application code. 9:03
You need to sign up for Treehouse in order to download course files.Sign up