Java Unit Tests: Guide to write meaningful unit tests

What are Unit Tests in Java

Why are Unit Tests a vital point in any Java project

How to write meaningful Unit Tests in Java

Unit Tests – Tips

What practices should be avoided when writing unit tests

Requirements

  • Basic Java and Spring Boot knowledge needed.
  • Injection mechanism in Spring Boot

Description

This course presents a way to start with Unit Tests in Java. The examples that I’m providing are inside a Spring Boot App. I think this adds value for you when it comes to getting a clearer picture of Unit Tests in an actual environment that is used by thousands if not millions of apps.

I think that it’s important for you to understand that these are my opinions when it comes to unit testing. I’m sure that there are many advice out there which are better than mine. What I’m trying to present here is how I see the Unit Tests after a couple of years of professional experience with them. I try to discuss about different topics that I learned about in the most basic ways.

This course comes up with answers about what are Unit Tests and why it’s good to have them. It also has a practical part, in which I present you some code samples of unit testing. In the end, I mention some tips that helped me along the way, and then I enumerate bad practices that I’m trying to avoid.

I hope from the bottom of my heart that this course will help you!

Who this course is for:

  • This course is recommended to all Java developers who want to learn or improve their Java Unit Testing skills.

Course content

6 sections • 15 lectures • 41m total lengthExpand all sections

Course introduction1 lecture • 1min

  • Course introduction00:37

Introduction to Java Unit Tests3 lectures • 5min

  • What is a unit test?00:53
  • Why create and use unit tests?02:40
  • Unit tests – mandatory or optional?01:00

Coding part: Write meaningful Unit Tests5 lectures • 36min

  • Create a Spring Boot App that has two endpoints05:48
  • Which classes are we going to unit test and why?04:21
  • Unit tests for the Controller class – addNewCar()10:59
  • Unit tests for the Controller class – getAllCars()06:32
  • Unit tests for the Service class – Code coverage07:52

Tips2 lectures • 1min

  • Golden rule00:10
  • List of things that I try to keep in my mind00:15

Bad practices1 lecture • 1min

  • Try to avoid doing the following00:12

Course recap3 lectures • 1min

  • Course Recap00:20
  • Thank you for your time00:05
  • Helpful Links00:05

Software Testing

About this course

When writing software, destruction can be just as valuable as creation. Learn how to catch bugs and break software as you discover different testing methods that will help you build better software.

What you will learn

  1. Domains, Ranges, Oracles, and Kinds of Testing
    • How to think about the different elements of software testing.
  2. Code Coverage
    • How to find parts of a program that need more testing.
  3. Random Testing
    • How to automatically generate test cases that break code in unexpected ways.
  4. Advanced Random Testing
    • How to engineer a sophisticated random test case generator.
  5. Consequences
    • How to deal with lots of bugs, how to take a big input that triggers a bug and make it smaller, how to report a bug, and more!
  6. Conclusion

Prerequisites and requirements

Programming experience is required.

Automated Software Testing: Unit Testing, Coverage Criteria and Design for Testability

About this course

Software testinggets a bad rap for being difficult, time-consuming, redundant, and above all – boring. But in fact, it is a proven way to ensure that your software will work flawlessly andcan meet release schedules.

In a two-course series, we will teach you automated software testing in an inspiring way. We will show you that testing is not as daunting a task as you might think, and how automated testing will make you a better developer who programs excellent software.

This first course will teach you specification-based testing, boundary testing, test adequacy and code coverage, unit vs system testing, mock objects, design for testability, and test code quality.

This is a highly practical course. Throughout the lessons, you will test various programs by means of different techniques. By the end, you will be able to choose the best testing strategies for different projects.

If you are or want to become a five-star software developer, QA engineer, or software tester, join this course. Testing will never be the same again!

At a glance

  • Institution: DelftX
  • Subject: Computer Science
  • Level: Intermediate
  • Prerequisites:Introductory knowledge of programming, specifically with Java.
  • Language: English
  • Video Transcript: English
  • Associated programs:
    • Professional Certificate in Automated Software Testing
  • Associated skills: Software Quality (SQA/SQC), Software Testing, Unit Testing, Automation, Testability, System Testing, Test Automation, Code Coverage, Boundary Testing, Safety Assurance

What you’ll learn

  • How to test any software system using current state-of-the-arttechniques
  • How to derive test cases that deal with exceptional, corner, and bad-weather cases by means of several different techniques
  • The limitations of current testing techniques and how to decide on the best testing strategies for a given context
  • How to develop testable architectures and to write maintainable test code

Syllabus

Topic 1: Automated software testing

  • Understand some basic principles of testing and whyit requires creativity.
  • Get familiar withsome testing vocabulary, which improves our communication.
  • Devise our first set of tests for a Java program and automatethese testsusing JUnit.
  • Understand the differences between unit, integration, and system testing, as well as the testing pyramid.

Topic 2: Functional testing

  • Learn how to read a requirement and derive the partitions, which are the actual concrete tests we will perform to make sure our system works (we call it “partitioning” and “equivalent partitioning”).
  • Learn how to think about and test possible corner cases, as developersoften forget to deal with such cases (we call itboundary testing).
  • Get a few tips, based on experience, on common corner cases.

Topic 3: Structural testing and code coverage

  • Learn how to analyze the structure of your source code and derive meaningful tests.
  • Be able to use the most popular structural testing adequacy criteria, such as line coverage, branch coverage, and path coverage as well as to understand their differences.
  • Derive a minimal set of tests when your branches (e.g., ifs) are too complex.
  • Use a standard industry code coverage tool and interpret its results.

Topic 4: Testability and mock objects

  • Understand the differences between different test levels (e.g., unit, integration, and system tests)
  • Understand what makes a system more or less testable.
  • Propose refactoring opportunities for increasing testability.
  • Be able to apply mock objects in order to unit test a class.
  • Understand when not to apply mock objects and go for integration tests.

Topic 5 : Test code smells

  • Judge the internal code quality of your test suite and advice possible improvements.
  • Understand and avoid flaky tests.
  • Refactor and clean smelly test code.

More about this course

LICENSE

The course materials of this course are Copyright Delft University of Technology and are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike (CC-BY-NC-SA) 4.0 International License.