easyforgood / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Equality for arguments fails within Mockito.when #445

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create a POJO.
Class Course {
    String courseName;
    Set<String> studentNames;
}

2. Add lombok @Data annotation. http://projectlombok.org/features/Data.html
@Data
Class Course {...

3. Create two equal Course objects.
Course PHY101 = new Course();
PHY101.setCourseName("physics 101");
PHY101.setStudentNames(Collections.<String>singleton("Newton"));

Course ENG101 = new Course();
ENG101.setCourseName("physics 101");
ENG101.setStudentNames(Collections.<String>singleton("Newton"));

PHY101.equals(ENG101) returns true.

What is the expected output? What do you see instead?
Suppose I am testing with PHY101 object. And PHY101.equals(ENG101) is true.
1.
when( mockUniversity.start(PHY101) )
Succeed. Given.

2.
when( mockUniversity.start(ENG101) )
FAIL. It should also succeed because PHY101 equals ENG101.

3.
when( mockUniversity.start(Matchers.<Course>eq(ENG101)) )
FAIL. It should also succeed because PHY101 equals ENG101.

4.
when( mockUniversity.start(Matchers.refEq(ENG101, "studentNames")) )
Succeed. So the root cause is narrowed down to studentNames.

5.
ENG101.setStudentNames(PHY101.getStudentNames());
when( mockUniversity.start(ENG101)
Succeed. It appears within when(), it's not using the given equals method. 
Rather it's checking whether the two are the same.

What version of the product are you using? On what operating system?
Mockito 1.9.0. Linux.

Please provide any additional information below.

Original issue reported on code.google.com by chinajia...@gmail.com on 26 Jul 2013 at 4:52

GoogleCodeExporter commented 8 years ago
Please close this issue. It is a user error. You cannot modify any object in a 
HashSet.

Original comment by chinajia...@gmail.com on 27 Jul 2013 at 12:16

GoogleCodeExporter commented 8 years ago
OK, goog luch

Cheers,
Brice

Original comment by brice.du...@gmail.com on 4 Dec 2013 at 2:55