Home Java javaTutorial The difference between assertEquals and assertSame methods in JUnit

The difference between assertEquals and assertSame methods in JUnit

Dec 28, 2016 am 11:40 AM

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)!


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 Article

Hot tools Tags

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 are annotations used for test methods in the JUnit framework? How are annotations used for test methods in the JUnit framework? May 06, 2024 pm 05:33 PM

How are annotations used for test methods in the JUnit framework?

Usage of JUnit unit testing framework in multi-threaded environment Usage of JUnit unit testing framework in multi-threaded environment Apr 18, 2024 pm 03:12 PM

Usage of JUnit unit testing framework in multi-threaded environment

JUnit Unit Testing Framework: A Beginner's Tutorial JUnit Unit Testing Framework: A Beginner's Tutorial Apr 18, 2024 pm 01:51 PM

JUnit Unit Testing Framework: A Beginner's Tutorial

JUnit unit testing framework: advantages and limitations of using it JUnit unit testing framework: advantages and limitations of using it Apr 18, 2024 pm 09:18 PM

JUnit unit testing framework: advantages and limitations of using it

Application of JUnit unit testing framework in Spring projects Application of JUnit unit testing framework in Spring projects Apr 18, 2024 pm 04:54 PM

Application of JUnit unit testing framework in Spring projects

What are the best practices for JUnit unit testing framework in Java? What are the best practices for JUnit unit testing framework in Java? Apr 18, 2024 pm 09:03 PM

What are the best practices for JUnit unit testing framework in Java?

JUnit unit testing framework: A guide to solving common memory leak problems JUnit unit testing framework: A guide to solving common memory leak problems Apr 18, 2024 pm 04:51 PM

JUnit unit testing framework: A guide to solving common memory leak problems

JUnit unit testing framework: Tips for running test cases in debug mode JUnit unit testing framework: Tips for running test cases in debug mode Apr 18, 2024 pm 03:54 PM

JUnit unit testing framework: Tips for running test cases in debug mode

See all articles