Package com.azure.core.test
Class TestBase
- java.lang.Object
-
- com.azure.core.test.TestBase
-
- All Implemented Interfaces:
org.junit.jupiter.api.extension.BeforeEachCallback,org.junit.jupiter.api.extension.Extension
public abstract class TestBase extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback
Base class for running live and playback tests usingInterceptorManager.
-
-
Field Summary
Fields Modifier and Type Field Description static StringAZURE_TEST_HTTP_CLIENTS_VALUE_ALLSpecifies to use all HttpClient implementations in testing.static StringAZURE_TEST_HTTP_CLIENTS_VALUE_NETTYSpecifies to use Netty HttpClient implementation in testing.static StringAZURE_TEST_SERVICE_VERSIONS_VALUE_ALLSpecifies to use all service versions in testing.protected InterceptorManagerinterceptorManagerInterceptorManagerused for this test run.protected TestContextManagertestContextManagerTestContextManagerused for this test run.protected com.azure.core.test.utils.TestResourceNamertestResourceNamerTestResourceNamerused for this test run.
-
Constructor Summary
Constructors Constructor Description TestBase()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidafterTest()Dispose of any resources and clean-up after a test case runs.voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)protected voidbeforeTest()Performs any set-up before each test case.protected HttpClientgetHttpClientOrUsePlayback(HttpClient httpClient)Convenience method which either returned the passedHttpClientor returns aPlaybackClientdepending on whether the test mode is playback.static Stream<HttpClient>getHttpClients()Returns a list ofHttpClientsthat should be tested.TestModegetTestMode()Gets the TestMode that has been initialized.protected StringgetTestName()Deprecated.This method is deprecated as JUnit 5 provides a simpler mechanism to get the test method name throughTestInfo.protected <T,U>
PollerFlux<T,U>setPlaybackPollerFluxPollInterval(PollerFlux<T,U> pollerFlux)Sets the polling interval for the passedPollerFlux.protected <T,U>
SyncPoller<T,U>setPlaybackSyncPollerPollInterval(SyncPoller<T,U> syncPoller)Sets the polling interval for the passedSyncPoller.static voidsetupClass()Before tests are executed, determines the test mode by reading theAZURE_TEST_MODEenvironment variable.voidsetupTest(org.junit.jupiter.api.TestInfo testInfo)Sets-up thetestResourceNamerandinterceptorManagerbefore each test case is run.static booleanshouldClientBeTested(HttpClient client)Returns whether the given http clients match the rules of test framework.protected voidsleepIfRunningAgainstService(long millis)Sleeps the test for the given amount of milliseconds ifTestModeisn'tTestMode.PLAYBACK.voidteardownTest(org.junit.jupiter.api.TestInfo testInfo)Disposes ofInterceptorManagerand its inheriting class' resources.
-
-
-
Field Detail
-
AZURE_TEST_HTTP_CLIENTS_VALUE_ALL
public static final String AZURE_TEST_HTTP_CLIENTS_VALUE_ALL
Specifies to use all HttpClient implementations in testing.- See Also:
- Constant Field Values
-
AZURE_TEST_HTTP_CLIENTS_VALUE_NETTY
public static final String AZURE_TEST_HTTP_CLIENTS_VALUE_NETTY
Specifies to use Netty HttpClient implementation in testing.- See Also:
- Constant Field Values
-
AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL
public static final String AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL
Specifies to use all service versions in testing.- See Also:
- Constant Field Values
-
interceptorManager
protected InterceptorManager interceptorManager
InterceptorManagerused for this test run.
-
testResourceNamer
protected com.azure.core.test.utils.TestResourceNamer testResourceNamer
TestResourceNamerused for this test run.
-
testContextManager
protected TestContextManager testContextManager
TestContextManagerused for this test run.
-
-
Method Detail
-
setupClass
@BeforeAll public static void setupClass()
Before tests are executed, determines the test mode by reading theAZURE_TEST_MODEenvironment variable. If it is not set,TestMode.PLAYBACK
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
setupTest
@BeforeEach public void setupTest(org.junit.jupiter.api.TestInfo testInfo)
Sets-up thetestResourceNamerandinterceptorManagerbefore each test case is run. Then calls its implementing class to perform any other set-up commands.- Parameters:
testInfo-TestInfoto retrieve test method name.
-
teardownTest
@AfterEach public void teardownTest(org.junit.jupiter.api.TestInfo testInfo)
Disposes ofInterceptorManagerand its inheriting class' resources.- Parameters:
testInfo- the injected testInfo
-
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 throughTestInfo. 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. SeesetupTest(TestInfo).Gets the name of the current test being run.- Returns:
- The name of the current test.
-
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.
-
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.
-
getHttpClients
public static Stream<HttpClient> getHttpClients()
Returns a list ofHttpClientsthat should be tested.- Returns:
- A list of
HttpClientsto be tested.
-
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.
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 ifTestModeisn'tTestMode.PLAYBACK.- Parameters:
millis- Number of milliseconds to sleep the test.- Throws:
IllegalStateException- If the sleep is interrupted.
-
setPlaybackSyncPollerPollInterval
protected <T,U> SyncPoller<T,U> setPlaybackSyncPollerPollInterval(SyncPoller<T,U> syncPoller)
Sets the polling interval for the passedSyncPoller.This configures the
SyncPollerto 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.- Type Parameters:
T- The type of poll response value.U- The type of the final result of long-running operation.- Parameters:
syncPoller- TheSyncPoller.- Returns:
- The updated
SyncPoller.
-
setPlaybackPollerFluxPollInterval
protected <T,U> PollerFlux<T,U> setPlaybackPollerFluxPollInterval(PollerFlux<T,U> pollerFlux)
Sets the polling interval for the passedPollerFlux.This configures the
PollerFluxto 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.- Type Parameters:
T- The type of poll response value.U- The type of the final result of long-running operation.- Parameters:
pollerFlux- ThePollerFlux.- Returns:
- The updated
PollerFlux.
-
getHttpClientOrUsePlayback
protected HttpClient getHttpClientOrUsePlayback(HttpClient httpClient)
Convenience method which either returned the passedHttpClientor returns aPlaybackClientdepending on whether the test mode is playback.When the test mode is playback the
PlaybackClientcorresponding to the test will be returned, otherwise the passedHttpClientwill be returned.- Parameters:
httpClient- The initialHttpClientthat will be used.- Returns:
- Either the passed
HttpClientorPlaybackClientbased on the test mode.
-
-