Home > Java > javaTutorial > body text

The difference between assertEquals and assertSame methods in JUnit

黄舟
Release: 2016-12-28 11:40:45
Original
1521 people have browsed it

1) The number of interfaces provided is not exactly the same.
assertEquals supports boolean, long, int and other java primitiveType variables.
assertSame only supports Object.

2) The comparison logic is different, and the results may be different.
assertSame is a direct comparison of objects. assertEquals can use the comparison logic provided by the compared object to perform comparisons.
Under the same conditions, the running results of the two are not necessarily the same.
A brief explanation is as follows:
The comparison logic of assertEquals(Object A, Object B):
If A and B are both Null, return true. Otherwise, call A.equals(B) to determine.

Comparison logic of assertSame(Object A, Object B):
Judged by the result of A == B operation.

The difference between A.equals(B) and A==B is.
If A does not override the equals method of java.lang.Object,
Then the memory address comparison of the two java objects will be the same as the result of A==B.
If A overrides the equals method (such as GregorianCalendar, BigDecimal class),
The result of the comparison may not be the same as the result of A==B.

The above are the different contents of the assertEquals and assertSame methods in JUnit. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template