I have following validation in scalatest based unit test:
response shouldBe Some(any[Long])
I just need to check that response is of type Some containing any long value.
But it fails as:
Expected :Some(null)
Actual :Some(1635758033586)
What is correct way of doing it?
CodePudding user response:
It seems that you are combining mockito matchers with scala-test matchers. It won't work in this case.
If your value of long
is different in each test run, you could just assert its presence.
response.isDefined shouldBe true
If value of the long
is the same, assert it with a literal
response shouldEqual Some(1635758033586)
CodePudding user response:
Warning, strong opinion ahead:
I would strongly suggest that you not use ScalaTest's matcher DSL and go with plain old assertions instead: https://www.scalatest.org/user_guide/using_assertions
All the matcher DSL does is give you another syntax to express what you already know how to do in regular Scala. And if you don't know, you can learn it and later apply that knowledge in your production code as well, which isn't the case with the matcher DSL. I hence consider the matcher DSL largely pointless and a huge waste of everybody's time.
As for your question “What if value is same always, how to validate then?”: There are many ways to do it. Here are some of them:
assert(response.contains(1635758033586))
assertResult(Some(1635758033586)) {
result
}
assertResult(1635758033586) {
result.get
}