Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #4256
| Newsgroups | comp.lang.java.help |
|---|---|
| Date | 2019-03-21 04:58 -0700 |
| References | <g7coe7$hn9$1@news.nems.noaa.gov> |
| Message-ID | <258bdfbc-1e89-493c-978d-62e0802284fa@googlegroups.com> (permalink) |
| Subject | Re: What is/are the difference(s) between assertSame and assertEquals in JUnit? |
| From | onlyfullstackdev@gmail.com |
assertEquals - It check the objects are equal or not based on the overridded equals() method of that class. So we can check the equality of object based on their state(compare the values of their instance variables).
assertEquals() The assertEquals() method compares two objects for equality, using their equals() method.
@Test
public void assertEquals_example() {
Employee employeeNew = new Employee();
employee.setSalary(1000000.0);
assertEquals("EMPLOYEE OBJECT", employee, employeeNew);
}
If the two objects are equal according to there implementation of their equals() method, the assertEquals() method will return normally. Otherwise, the assertEquals() method will throw an exception, and the test will stop there.
assertSame() and assertNotSame() The assertSame() and assertNotSame() methods tests if two object references point to the same object or not. It is not enough that the two objects pointed to are equals according to their equals() methods. It must be exactly the same object pointed to.
Here is a simple example:
@Test
public void assertSame_assertNoSame_example() {
assertSame(employeeService.getEmployeeFromId(1), employeeService.getEmployeeFromId(1));
assertNotSame(employee, employeeService.getEmployeeFromId(1)); // We will get null as response
}
You can follow below url to get more information: https://onlyfullstack.blogspot.com/2019/02/junit-assert-methods.html
https://onlyfullstack.blogspot.com/2019/02/junit-tutorial.html
On Thursday, August 7, 2008 at 1:53:11 AM UTC+8, RC wrote:
> In JUnit Assert class have
>
> assertSame(Object expect, Object actual)
> and
> assertEquals(Object expect, Object actual)
>
> What is/are the difference(s)?
>
> Interesting, there is
>
> assertNotSame(Object expect, Object actual)
>
> But there is no
>
> assertNotEquals(Object expect, Object actual)
Back to comp.lang.java.help | Previous | Next | Find similar
Re: What is/are the difference(s) between assertSame and assertEquals in JUnit? onlyfullstackdev@gmail.com - 2019-03-21 04:58 -0700
csiph-web