0

I am facing a problem with using assertThat(object.method(new SomeClass(someParam))) as a result, the comparison is when actually running the test, the matcher is comparing object references not the contents of the object as equals method isn't overrided.

I don't want to do the following to solve the problem

  • Mock constructor since I want to use the real object where the method is actually called
  • assertThat(object.method(any(SomeClass.class))) since it loosens the test and the parameter someParam is important.

Is there a solution which would do something like the following?

  • assertThat(object.method(any(SomeClass.class, someParam))) where it will match both object calling it with a specific constructor passing the parameter.
Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64
  • What `assertThat()` do you mean? The standard assertThat from JUnit Assert class has always TWO paramerers. Why don't you use that? – Honza Zidek Mar 23 '17 at 13:15

2 Answers2

1

You can write your own matcher, I usually extend mockito's ArgumentMatcher...

slowy
  • 227
  • 1
  • 6
1

Mock constructor since I want to use the real object where the method is actually called

This is the wrong approach.

The better approach is to refactor your production code to use dependency injection, separation of concerns and other OO principles. Then it is easy to replace object with a mock and use verify(object).method(eq(parameter));

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51