共用方式為


TestUtils Class

  • java.lang.Object
    • com.azure.core.test.utils.TestUtils

public final class TestUtils

Contains utility methods used for testing.

Method Summary

Modifier and Type Method and Description
static void assertArraysEqual(byte[] expected, byte[] actual)

Asserts that two arrays are equal.

static void assertArraysEqual(byte[] expected, int expectedOffset, byte[] actual, int actualOffset, int length)

Asserts that two arrays are equal.

static void assertByteBuffersEqual(ByteBuffer expected, ByteBuffer actual)

Asserts that two ByteBuffers are equal.

static HttpClient getFaultInjectingHttpClient(HttpClient clientToWrap, boolean useHttps)

Wraps an HttpClient to make calls to HTTP fault injector to test random network failures.

static HttpClient getFaultInjectingHttpClient(HttpClient clientToWrap, boolean useHttps, int successRate, int partialRate, int failureRate)

Wraps an HttpClient to make calls to HTTP fault injector to test random network failures.

static File getRecordFolder()

Get the File pointing to the folder where session records live.

static Path getRepoRootResolveUntil(Path testClassPath, String resolveFolder)

Locates the root of the current repo until the provided folder's parent.

static URI toURI(URL url)

Returns a URI equivalent to this URL.

Methods inherited from java.lang.Object

Method Details

assertArraysEqual

public static void assertArraysEqual(byte[] expected, byte[] actual)

Asserts that two arrays are equal.

This method is similar to JUnit's Assertions#assertArrayEquals(byte[], byte[]) except that it takes advantage of hardware intrinsics offered by the JDK to optimize comparing the byte arrays.

If the arrays aren't equal this will call Assertions#assertArrayEquals(byte[], byte[]) to take advantage of the better error message, but this is the exceptional case and worth the double comparison performance hit.

Parameters:

expected - The expected byte array.
actual - The actual byte array.

assertArraysEqual

public static void assertArraysEqual(byte[] expected, int expectedOffset, byte[] actual, int actualOffset, int length)

Asserts that two arrays are equal.

This method is similar to JUnit's Assertions#assertArrayEquals(byte[], byte[]) except that it takes advantage of hardware intrinsics offered by the JDK to optimize comparing the byte arrays and allows for comparing subsections of the arrays.

If the arrays aren't equal this will copy the array ranges and call Assertions#assertArrayEquals(byte[], byte[]) to take advantage of the better error message, but this is the exceptional case and worth the double comparison performance hit.

Parameters:

expected - The expected byte array.
expectedOffset - Starting offset to begin comparing in the expected array.
actual - The actual byte array.
actualOffset - Starting offset to begin comparing in the actual array.
length - Amount of bytes to compare.

assertByteBuffersEqual

public static void assertByteBuffersEqual(ByteBuffer expected, ByteBuffer actual)

Asserts that two ByteBuffers are equal.

This method is similar to JUnit's Assertions#assertArrayEquals(byte[], byte[]) except that it takes advantage of hardware intrinsics offered by the JDK to optimize comparing the ByteBuffers.

If the ByteBuffers aren't equal this will copy the ByteBuffer contents into byte arrays and call Assertions#assertArrayEquals(byte[], byte[]) to take advantage of the better error message, but this is the exceptional case and worth the double comparison performance hit.

Parameters:

expected - The expected ByteBuffer.
actual - The actual ByteBuffer.

getFaultInjectingHttpClient

public static HttpClient getFaultInjectingHttpClient(HttpClient clientToWrap, boolean useHttps)

Wraps an HttpClient to make calls to HTTP fault injector to test random network failures.

Using the HttpClient returned by this method requires all setup required by HTTP fault injector to be configured. useHttps determines whether requests are forwarded to HTTP fault injector using HTTPS or HTTP, using HTTP doesn't require the self-signed certificate used by HTTP fault injector to be trusted by the JVM making it easier to prototype tests using HTTP fault injector. Merge ready tests should always use HTTPS.

The HttpClient returned will use the default successful and failure response percentages. 75% of request will succeed, 24% of requests will fail with a partial body returned, and 1% of requests will never return a response. It is recommended for tests using HTTP fault injector to set HttpClientOptions#setResponseTimeout(Duration) and HttpClientOptions#setReadTimeout(Duration), or the equivalent methods directly in the HTTP client builder being used, as the default timeouts are 60 seconds and with responses failing randomly reducing the timeout will let the tests run faster while still generally testing the same.

Parameters:

clientToWrap - The HttpClient being wrapped that will send the actual request.
useHttps - Whether HTTPS should be used to communicate with HTTP fault injector.

Returns:

An HttpClient that forwards requests to HTTP fault injector with automatic fault injection handling to run tests with flaky network.

getFaultInjectingHttpClient

public static HttpClient getFaultInjectingHttpClient(HttpClient clientToWrap, boolean useHttps, int successRate, int partialRate, int failureRate)

Wraps an HttpClient to make calls to HTTP fault injector to test random network failures.

Using the HttpClient returned by this method requires all setup required by HTTP fault injector to be configured. useHttps determines whether requests are forwarded to HTTP fault injector using HTTPS or HTTP, using HTTP doesn't require the self-signed certificate used by HTTP fault injector to be trusted by the JVM making it easier to prototype tests using HTTP fault injector. Merge ready tests should always use HTTPS.

The HttpClient returned will use the specified successful and failure response percentages. The combination of successRate, partialRate, and failureRate must equal 100, if not an IllegalArgumentException will be thrown. An IllegalArgumentException will also be thrown if any of the values are negative. It is recommended for tests using HTTP fault injector to set HttpClientOptions#setResponseTimeout(Duration) and HttpClientOptions#setReadTimeout(Duration), or the equivalent methods directly in the HTTP client builder being used, as the default timeouts are 60 seconds and with responses failing randomly reducing the timeout will let the tests run faster while still generally testing the same.

Parameters:

clientToWrap - The HttpClient being wrapped that will send the actual request.
useHttps - Whether HTTPS should be used to communicate with HTTP fault injector.
successRate - Percent of requests that will succeed.
partialRate - Percent of requests that will partially succeed.
failureRate - Percent of requests that will fail.

Returns:

An HttpClient that forwards requests to HTTP fault injector with automatic fault injection handling to run tests with flaky network.

getRecordFolder

public static File getRecordFolder()

Get the File pointing to the folder where session records live.

Returns:

The session-records folder.

getRepoRootResolveUntil

public static Path getRepoRootResolveUntil(Path testClassPath, String resolveFolder)

Locates the root of the current repo until the provided folder's parent.

Parameters:

testClassPath - the test class path
resolveFolder - the folder parent to resolve the path until

Returns:

The Path to the root of the repo.

toURI

public static URI toURI(URL url)

Returns a URI equivalent to this URL.

Parameters:

url - the url to be converted to URI

Returns:

the URI

Applies to