Home Java javaTutorial assertThat, assertEquals, assertTrue

assertThat, assertEquals, assertTrue

Dec 28, 2016 am 11:46 AM

Last night was the Open House event of AgileChina 2011, and I was a volunteer in the coding session. The main purpose of the Coding session is to let the participating developers experience the process of pair programming, test-driven development and refactoring. We have prepared four different types of programming questions. The company will have eight or nine colleagues and participating peers to experience this process:

Time Sheet

Supermarket Checkout

Source code line number statistics

Blackjack Game

Overall, the event was quite successful, except for two small episodes:

1. The company wanted to I bought a new keyboard and mouse, but they feel too tactile when typing, and the keys on the keyboard are flat. One participant may not be very familiar with the keyboard and always mistakenly types enter into a slash many times.

2. The system's lock screen shortcut keys conflict with the IDE's formatting code shortcut keys, which resulted in me typing the wrong key twice to lock the screen. Moreover, the machine does not belong to me, so I have to find other colleagues to help input it. Password, damn it.

In the last round of Pair, a classmate asked: Why not use assertEquals? I see that you are all using assertThat, and it seems that the use of assertEquals, assertTrue, etc. is not highly recommended.

Because the event was almost over and we were going to take a group photo, we simply answered. Here is a detailed answer to this question.

What do we want to get from assertions

What do we want to get from assertions in tests? Just displaying a red bar after the test fails is not what we want. In addition, we also want to get some information from the test:

1. Where did the test fail? Is there a line of code? All assertions can provide this function

2. Why the test failed is difficult to define. Most assertions can provide similar functions:

Desired results vs actual results

But different assertions provide different information. There are also problems where comparisons are difficult to make using simple assertions like equality. Moreover, assertions also have a very important role: documentation. That is, when you read the test code, you see the assertions as if you were seeing all the expectations. And very clear expectations.

Actual example

Look at the second parameter of assertThat. It accepts a Matcher. There are very rich Matcher implementations in org.hamcrest. For example, now our API returns a list of user objects, and we want to assert that this list contains the user we expect. There is a Matcher such as hasItem in hamcrest:

   1: assertThat(userDAO.findAll(), hasItem(expected));
Copy after login

What should we do if we use other assertions?

   1: assertTrue(userDAO.findAll().contains(expected));
Copy after login

Okay, here’s the problem, if both assertions above fail, the failure message of the first assertion will be:

The expected result contains expected….

But actually...all the users returned by findAll will be printed here

And what about the second assertion? It's like this:

Expected to be true, actually false

Which one is more reliable? Obviously the failure information of the first assertion is more helpful for us to find the problem.

For documentation, let’s look at this requirement again: the user list returned by the assertion does not contain the given user:

   1: assertThat(userDAO.findAll(), not(hasItem(expected));
Copy after login

Is it very clear? You won’t feel it when reading this assertion. When you are reading code, it is like reading Spec. If other assertions are used:

   1: assertFalse(userDAO.findAll().contains(expected));
Copy after login

is not as well documented as the previous one.

Of course, for some APIs that return true or false, or return a single value, you can also use other assertions without much problem. For example:

   1: assertEquals(userDAO.findBy(id), expected);
Copy after login

Ah, what? You said I used it wrong, why? After checking the parameter description, I found that the expected value should be placed in the first parameter, and the actual value should be placed in the second parameter. It’s so helpless. My memory is not good, so I always make these mistakes. Fortunately, we have assertThat:

   1: assertThat(userDAO.findBy(id), is(expected));
Copy after login

Will you still make mistakes?

The above is the content of assertThat, assertEquals, and assertTrue. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

How to restrict access to specific interfaces of nested H5 pages through OAuth2.0's scope mechanism? How to restrict access to specific interfaces of nested H5 pages through OAuth2.0's scope mechanism? Apr 19, 2025 pm 02:30 PM

How to use OAuth2.0's access_token to achieve control of interface access permissions? In the application of OAuth2.0, how to ensure that the...

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? Apr 19, 2025 pm 01:51 PM

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

In Java remote debugging, how to correctly obtain constant values ​​on remote servers? In Java remote debugging, how to correctly obtain constant values ​​on remote servers? Apr 19, 2025 pm 01:54 PM

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

How to choose Java project management tools when learning back-end development? How to choose Java project management tools when learning back-end development? Apr 19, 2025 pm 02:15 PM

Confused with choosing Java project management tools for beginners. For those who are just beginning to learn backend development, choosing the right project management tools is crucial...

Ultimate consistency in distributed systems: how to apply and how to compensate for data inconsistencies? Ultimate consistency in distributed systems: how to apply and how to compensate for data inconsistencies? Apr 19, 2025 pm 02:24 PM

Exploring the application of ultimate consistency in distributed systems Distributed transaction processing has always been a problem in distributed system architecture. To solve the problem...

Why does the Python script not be found when submitting a PyFlink job on YARN? Why does the Python script not be found when submitting a PyFlink job on YARN? Apr 19, 2025 pm 02:06 PM

Analysis of the reason why Python script cannot be found when submitting a PyFlink job on YARN When you try to submit a PyFlink job through YARN, you may encounter...

See all articles