Remove the use of InjectRule in UrlFetchUtilsTest

Random used to be a static variable which requires InjectRule to mock it in unit tests. It is now a singleton, which ensures that the same instance is called every time and Random.nextBytes() generates results that distribute uniformly between each call.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=217592767
This commit is contained in:
jianglai 2018-10-17 14:53:23 -07:00
parent 84c3544097
commit 4140ef6315
5 changed files with 35 additions and 12 deletions

View file

@ -52,10 +52,10 @@ public class UrlFetchUtilsTest {
@Rule
public final InjectRule inject = new InjectRule();
private final Random random = mock(Random.class);
@Before
public void setupRandomZeroes() {
Random random = mock(Random.class);
inject.setStaticField(UrlFetchUtils.class, "random", random);
doAnswer(
info -> {
Arrays.fill((byte[]) info.getArguments()[0], (byte) 0);
@ -69,7 +69,12 @@ public class UrlFetchUtilsTest {
public void testSetPayloadMultipart() {
HTTPRequest request = mock(HTTPRequest.class);
setPayloadMultipart(
request, "lol", "cat", CSV_UTF_8, "The nice people at the store say hello. ヘ(◕。◕ヘ)");
request,
"lol",
"cat",
CSV_UTF_8,
"The nice people at the store say hello. ヘ(◕。◕ヘ)",
random);
ArgumentCaptor<HTTPHeader> headerCaptor = ArgumentCaptor.forClass(HTTPHeader.class);
verify(request, times(2)).addHeader(headerCaptor.capture());
List<HTTPHeader> addedHeaders = headerCaptor.getAllValues();
@ -97,7 +102,7 @@ public class UrlFetchUtilsTest {
IllegalStateException thrown =
assertThrows(
IllegalStateException.class,
() -> setPayloadMultipart(request, "lol", "cat", CSV_UTF_8, payload));
() -> setPayloadMultipart(request, "lol", "cat", CSV_UTF_8, payload, random));
assertThat(thrown)
.hasMessageThat()
.contains(