Reputation: 11
I have a class LocalizedCollatorProvider
public class LocalizedCollatorProvider {
private String language = "pl";
private String country = "PL";
public Collator getCollator() {
return Collator.getInstance(new Locale(language, country)):
}
}
I try to write a test of this class and I get an error Unnecessary stubbing detected in line
given(collatorProvider.getCollator()).willReturn(getInstance(new Locale("pl", "PL")));
collatorProvider
is a mock of the above class LocalizedCollatorProvider
Upvotes: 0
Views: 50
Reputation: 3306
Mockito tries to help you to create clean tests. In strict mode it shall detect the unused stubs in test code.
In this case you mocked something but you didn't use that.
I will use Hungarian locale to demonstrate a simple comare method. The á
vowel is between a
and b
.
@Test
void assertedTest() {
var collatorProvider = Mockito.mock(LocalizedCollatorProvider.class);
given(collatorProvider.getCollator()).willReturn(Collator.getInstance(new Locale("hu", "hu")));
assertTrue(collatorProvider.getCollator().compare("álom", "barna") < 0);
}
In the previous example I called the mocked method and made an assertion. So I necessarily made a mock.
If you really want to create an unnecessary mock, then you can bypass that using lenient stictness.
@Test
void lenientTest() {
var collatorProvider = Mockito.mock(LocalizedCollatorProvider.class, withSettings().strictness(Strictness.LENIENT));
given(collatorProvider.getCollator()).willReturn(Collator.getInstance(new Locale("hu", "hu")));
// there is neither a method call nor an assertion -> this stubbing is unnecessary but won't fail
}
Upvotes: 0