Max
Max

Reputation: 11

How to use Mockito/stubbing to mock a String.length to a particular value

I need to simulate a scenario where my test needs to be returning a particular value for String.length function i.e- String str[]= abc.getValue();

if(String.length != 100){//do this} else {//do that}

I want to mock String length to return 100 so that I can execute else loop

Any suggestions will be appreciated.

Upvotes: 0

Views: 1341

Answers (1)

Wim Deblauwe
Wim Deblauwe

Reputation: 26868

If you are on Java 11 or above, you can use this to create a String of length 100 and avoid the mocking:

String s = "x".repeat(100)

Upvotes: 1

Related Questions