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
1: assertThat(userDAO.findAll(), hasItem(expected));
What should we do if we use other assertions?
1: assertTrue(userDAO.findAll().contains(expected));
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));
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));
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);
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));
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)!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.