Introduction to Unit Testing in Java with J-Unit 4

Everything you need to learn to get up an running writing unit tests, including tools, strategies, and techniques.
4.0 (10 ratings)
Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
196 students enrolled
$19
$20
5% off
Take This Course
  • Lectures 14
  • Length 1.5 hours
  • Skill Level Intermediate Level
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 9/2014 English

Course Description

Unit testing is not a new idea, but it has become much more popular in the days of Agile and continuous, iterative development. Unit tests ensure that quality code is written, and gives the developer and the team the peace of mind to know that changes to the system can be made with confidence.

This short module is designed to be like a one-day training session, where the user can come in only knowing how to program in java and leave knowing how to create unit tests against their java code.

By the time you've completed this course, you'll know how to create a unit test, as well as understand the different attributes used in testing, along with the valid commands to issue to write good tests.

Additionally, this course will help you to learn how to use TDD as a driver for all of your development efforts going forward. With a "test-first" approach, we change our standard viewpoint of how we write code. Now, not only do we think about "how do I make this code work," we also think about "how do I break this code" or more often, "what happens when the user or system doesn't play nice with our code?"

This course is structured to take you anywhere from 2-8 hours to complete, depending on how much of the work you do on your own or just use from the provided samples. The lessons and knowledge build from the ground up, but the last few lessons could be done in any order, depending on your needs.

If you've ever wondered how to get started with unit testing, this is the course for you. This course would also be good for anyone looking to implement testing in future and existing development efforts.

What are the requirements?

  • Java Programming basic knowledge
  • A computer
  • Ability to solve problems

What am I going to get from this course?

  • Learn J-Unit Syntax
  • Understand what it means to "Assert" code is working as expected
  • Learn strategies for testing code
  • Learn techniques for testing code
  • Understand differences between J-Unit 3 and J-Unit 4

What is the target audience?

  • Anyone who wants to learn about unit testing
  • You should already have a basic understanding of programming languages
  • Anyone curious about what unit tests are and why would we use them

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Getting Started
03:32

This video is a welcome to our course and an overview of what is to come. During this video, we discuss what you will and won't learn in this course module, as well as some expectations about the amount of time it will take to go through this course.

08:37

This video is the first part of two parts of an introduction to testing. In this part, we go over some of the major arguments against testing and talk about how testing is actually very important, even if there is a bit more work up front.

06:55

In this video, we conclude our introduction to testing by discussing some testing strategies and the unit testing methodologies. We also look at the different assertions and how we'll use assertions to pass or fail the unit tests.

08:40

In this video, we go over creation of the unit tests in Eclipse and start our Person object and testing on the Person object, working from the ground up. After we get the test class and JUnit libraries in place, we begin our testing by testing the Person class constructors.

There are two sets of files for this video. The starter pack and the finished code. I highly recommend starting with the starter pack and working along with me. If you get stuck, there are some text files that contain finished versions of the code in both projects. If you are pressed for time and just want to review, or if you have completed the video and want to check your work, take a look at the finished version to compare results or see the end result of this part of the project.

11:39

In this video, we continue our look at testing by adding the tests and then coding the accessors (getters) and mutators (setters) for our Person class instance variables.

As before, there are two sets of files for this part of the project. I again recommend using the starter pack and working along with me. The finished version is there for your convenience as well.

05:39

In this video, we continue testing our Person object by writing the tests to verify the toString() method is working as expected.

We'll just verify toString() contains our expected property values using the contains method of the String class. If you need to do more thorough testing, like validating a specific format, you could do that here as well.

As before, there are two sets of files for this part of the project. I again recommend using the starter pack and working along with me. The finished version is there for your convenience as well.

07:56

In this video, we start our examination of how we'll go about testing the Comparable interface, which will implement in the Person class. This method can sometimes be tricky to implement, so we'll take some time planning before we get started. Some good things to think about will be what inputs can be sent to the method, what specific values we want to return, and what the order of precedence will be for determining if one person is greater than, less than, or equal to another person.

Comparable is an important interface to implement when we are going to be ordering objects. It is very conceivable that an object like person would need to be ordered by last name, then first name, and sometimes we'll care about age as well. We take all three into account in this class.

Please find the planning spreadsheet and pdf/Word Document that I used attached to this video for your reference.

08:05

In this video, we go over the implementation of the Comparable interface and take the time to work through some of the testing. The amount of testing needed would go far too long for the video, so a set of instructions exists that allows you to follow through with creating all the tests and learn more about the TDD process.

As with the other testing sessions, I recommend getting the starter pack and working through to the end. The finished code is also available for you to review.

04:12

In this video, we take the time to discuss how the equals method works, and how we can override it and test it in our Person class. Once again, we'll benefit from our grid that helps us see which cases we really need to focus on for comparing to determine if two Person objects are equal.

06:31

In this video, we take a look at writing the code and tests for the equals() method. Once again, we'll do part of it in the video but it's up to you to practice it.

As with other videos in this section, we have both a starter and finished version of the project. There is also a step-by-step document for adding tests and code to PersonTest and Person classes.

07:08

In this video, we take the time to look at how we can test exceptions in J-Unit 4. With J-Unit 4 exception testing, we can use the @Test(expected=...) attribute to specifically identify the exception class that we expect should happen when our code executes. This is an extremely handy feature and makes testing for exceptions very convenient.

There are some things to remember. For example, once an exception happens, test code will immediately stop execution, so we don't want to put test code after the line in our test that we expect to trigger the exception.

Additionally, we'll need to write a specific test for each exception that we expect could happen in method. If there are multiple exceptions that could happen, make sure to write a test for each one.

Finally, we need to remember to also check to make sure valid code is not triggering the exception. A good way to begin to write the test then is to expect the exception and only have the valid code. The valid code should not trigger the exception so the test should fail (when no exception is thrown). Then add the code that should trigger the exception, and your test will now pass as expected.

03:54

This video is a brief discussion of the major differences in syntax between J-Unit 4 and J-Unit 3. By the end of the video, you should be able to easily write tests in either version, based on what we've already learned about testing and how tests would be structured in both J-Unit 4 and J-Unit 3.

01:32

This video is the conclusion for our course. I hope you enjoyed this course and would love to hear any feedback you have for me, either good or bad! Again, I wish you all the best in the future!

Article

This is a bonus lecture and has nothing to do with the course.

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

Brian Gorman, Software Engineer and Computer Science Instructor

I am an experienced .Net developer, certified as an MCSD .Net in C#.net. I have a masters of science degree in computer information systems, and a bachelor of science degree in computer science. I also have many years of experience instructing college courses online in SQL databases, C#/VB .Net programming, java programming, and Microsoft office technology. I have created many online technical training courses that can be purchased here or on other various platforms like O'Reilly/InfiniteSkills, and my own site at training MajorGuidanceSolutions

Ready to start learning?
Take This Course