TestBase Class

  • java.lang.Object
    • com.azure.core.test.TestBase

public abstract class TestBase

Base class for running live and playback tests using InterceptorManager.

Field Summary

Modifier and Type Field and Description
static final String AZURE_TEST_HTTP_CLIENTS_VALUE_ALL

Specifies to use all HttpClient implementations in testing.

static final String AZURE_TEST_HTTP_CLIENTS_VALUE_NETTY

Specifies to use Netty HttpClient implementation in testing.

static final String AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL

Specifies to use all service versions in testing.

protected InterceptorManager interceptorManager

InterceptorManager used for this test run.

protected TestContextManager testContextManager

TestContextManager used for this test run.

protected TestResourceNamer testResourceNamer

TestResourceNamer used for this test run.

Constructor Summary

Constructor Description
TestBase()

Creates a new instance of TestBase.

Method Summary

Modifier and Type Method and Description
protected PollerFlux<T,U> <T,U>setPlaybackPollerFluxPollInterval(PollerFlux<T,U> pollerFlux)

Sets the polling interval for the passed PollerFlux<T,U>.

protected SyncPoller<T,U> <T,U>setPlaybackSyncPollerPollInterval(SyncPoller<T,U> syncPoller)

Sets the polling interval for the passed SyncPoller<T,U>.

protected void afterTest()

Dispose of any resources and clean-up after a test case runs.

protected void beforeTest()

Performs any set-up before each test case.

protected HttpClient getHttpClientOrUsePlayback(HttpClient httpClient)

Convenience method which either returned the passed HttpClient or returns a PlaybackClient depending on whether the test mode is playback.

static Stream<HttpClient> getHttpClients()

Returns a list of HttpClient that should be tested.

protected Path getTestClassPath()

Returns the path of the class to which the test belongs.

TestMode getTestMode()

Gets the TestMode that has been initialized.

protected String getTestName()

Deprecated

This method is deprecated as JUnit 5 provides a simpler mechanism to get the test method name through TestInfo. Keeping this for backward compatability of other client libraries that still override this method. This method can be deleted when all client libraries remove this method. See setupTest(TestContextManager testContextManager).

Gets the name of the current test being run.

static boolean isTestProxyEnabled()

Indicates whether the out of process test recording proxy is in use.

protected static void setTestProxyEnabled()

Enables use of the test proxy.

static void setupClass()

Before tests are executed, determines the test mode by reading the AZURE_TEST_MODE environment variable.

void setupTest(TestContextManager testContextManager)

Sets-up the testResourceNamer and interceptorManager before each test case is run.

static boolean shouldClientBeTested(HttpClient client)

Returns whether the given http clients match the rules of test framework.

protected void sleepIfRunningAgainstService(long millis)

Sleeps the test for the given amount of milliseconds if TestMode isn't PLAYBACK.

void teardownTest()

Disposes of InterceptorManager and its inheriting class' resources.

Methods inherited from java.lang.Object

Field Details

AZURE_TEST_HTTP_CLIENTS_VALUE_ALL

public static final String AZURE_TEST_HTTP_CLIENTS_VALUE_ALL

Specifies to use all HttpClient implementations in testing.

AZURE_TEST_HTTP_CLIENTS_VALUE_NETTY

public static final String AZURE_TEST_HTTP_CLIENTS_VALUE_NETTY

Specifies to use Netty HttpClient implementation in testing.

AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL

public static final String AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL

Specifies to use all service versions in testing.

interceptorManager

protected InterceptorManager interceptorManager

InterceptorManager used for this test run.

testContextManager

protected TestContextManager testContextManager

TestContextManager used for this test run.

testResourceNamer

protected TestResourceNamer testResourceNamer

TestResourceNamer used for this test run.

Constructor Details

TestBase

public TestBase()

Creates a new instance of TestBase.

Method Details

<T,U>setPlaybackPollerFluxPollInterval

protected PollerFlux setPlaybackPollerFluxPollInterval(PollerFlux pollerFlux)

Sets the polling interval for the passed PollerFlux<T,U>.

This configures the PollerFlux<T,U> to use a poll interval of one millisecond if the test mode is playback. In live or record test mode the polling interval is left as-is.

Parameters:

pollerFlux - The PollerFlux<T,U>.

Returns:

The updated PollerFlux<T,U>.

<T,U>setPlaybackSyncPollerPollInterval

protected SyncPoller setPlaybackSyncPollerPollInterval(SyncPoller syncPoller)

Sets the polling interval for the passed SyncPoller<T,U>.

This configures the SyncPoller<T,U> to use a poll interval of one millisecond if the test mode is playback. In live or record test mode the polling interval is left as-is.

Parameters:

syncPoller - The SyncPoller<T,U>.

Returns:

The updated SyncPoller<T,U>.

afterTest

protected void afterTest()

Dispose of any resources and clean-up after a test case runs. Can be overridden in an inheriting class to add additional functionality during test teardown.

beforeTest

protected void beforeTest()

Performs any set-up before each test case. Any initialization that occurs in TestBase occurs first before this. Can be overridden in an inheriting class to add additional functionality during test set-up.

getHttpClientOrUsePlayback

protected HttpClient getHttpClientOrUsePlayback(HttpClient httpClient)

Convenience method which either returned the passed HttpClient or returns a PlaybackClient depending on whether the test mode is playback.

When the test mode is playback the PlaybackClient corresponding to the test will be returned, otherwise the passed HttpClient will be returned.

Parameters:

httpClient - The initial HttpClient that will be used.

Returns:

Either the passed HttpClient or PlaybackClient based on the test mode.

getHttpClients

public static Stream getHttpClients()

Returns a list of HttpClient that should be tested.

Returns:

A list of HttpClient to be tested.

getTestClassPath

protected Path getTestClassPath()

Returns the path of the class to which the test belongs.

Returns:

The file path of the test class.

getTestMode

public TestMode getTestMode()

Gets the TestMode that has been initialized.

Returns:

The TestMode that has been initialized.

getTestName

@Deprecated
protected String getTestName()

Deprecated

This method is deprecated as JUnit 5 provides a simpler mechanism to get the test method name through TestInfo. Keeping this for backward compatability of other client libraries that still override this method. This method can be deleted when all client libraries remove this method. See setupTest(TestContextManager testContextManager).

Gets the name of the current test being run.

Returns:

The name of the current test.

isTestProxyEnabled

public static boolean isTestProxyEnabled()

Indicates whether the out of process test recording proxy is in use.

Returns:

true if test proxy is to be used.

setTestProxyEnabled

protected static void setTestProxyEnabled()

Enables use of the test proxy.

setupClass

public static void setupClass()

Before tests are executed, determines the test mode by reading the AZURE_TEST_MODE environment variable. If it is not set, PLAYBACK

setupTest

public void setupTest(TestContextManager testContextManager)

Sets-up the testResourceNamer and interceptorManager before each test case is run. Then calls its implementing class to perform any other set-up commands.

Parameters:

testContextManager - The TestContextManager managing information about this test.

shouldClientBeTested

public static boolean shouldClientBeTested(HttpClient client)

Returns whether the given http clients match the rules of test framework.

  • Using Netty http client as default if no environment variable is set.
  • If it's set to ALL, all HttpClients in the classpath will be tested.
  • Otherwise, the name of the HttpClient class should match env variable.

Environment values currently supported are: "ALL", "netty", "okhttp" which is case insensitive. Use comma to separate http clients want to test. e.g. set AZURE_TEST_HTTP_CLIENTS = NettyAsyncHttpClient, OkHttpAsyncHttpClient

Parameters:

client - Http client needs to check

Returns:

Boolean indicates whether filters out the client or not.

sleepIfRunningAgainstService

protected void sleepIfRunningAgainstService(long millis)

Sleeps the test for the given amount of milliseconds if TestMode isn't PLAYBACK.

Parameters:

millis - Number of milliseconds to sleep the test.

teardownTest

public void teardownTest()

Disposes of InterceptorManager and its inheriting class' resources.

Applies to