Package com.azure.android.core.test
Class TestBase
- java.lang.Object
-
- com.azure.android.core.test.TestBase
-
- All Implemented Interfaces:
org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
public abstract class TestBase extends java.lang.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 java.lang.String
AZURE_TEST_HTTP_CLIENTS_VALUE_ALL
static java.lang.String
AZURE_TEST_HTTP_CLIENTS_VALUE_OK_HTTP
static java.lang.String
AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL
protected InterceptorManager
interceptorManager
protected TestContextManager
testContextManager
protected TestResourceNamer
testResourceNamer
-
Constructor Summary
Constructors Constructor Description TestBase()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
afterTest()
Dispose of any resources and clean-up after a test case runs.void
beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
protected void
beforeTest()
Performs any set-up before each test case.static java.util.stream.Stream<com.azure.android.core.http.HttpClient>
getHttpClients()
Returns a list ofHttpClients
that should be tested.TestMode
getTestMode()
Gets the TestMode that has been initialized.protected java.lang.String
getTestName()
Deprecated.This method is deprecated as JUnit 5 provides a simpler mechanism to get the test method name throughTestInfo
.protected abstract java.net.URL
getUnitTestOutDir()
Get the output directory where UT artifacts are stored.static void
setupClass()
Before tests are executed, determines the test mode by reading theAZURE_TEST_MODE
environment variable.void
setupTest(org.junit.jupiter.api.TestInfo testInfo)
Sets-up thetestResourceNamer
andinterceptorManager
before each test case is run.static boolean
shouldClientBeTested(com.azure.android.core.http.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 ifTestMode
isn'tTestMode.PLAYBACK
.void
teardownTest(org.junit.jupiter.api.TestInfo testInfo)
Disposes ofInterceptorManager
and its inheriting class' resources.
-
-
-
Field Detail
-
AZURE_TEST_HTTP_CLIENTS_VALUE_ALL
public static final java.lang.String AZURE_TEST_HTTP_CLIENTS_VALUE_ALL
- See Also:
- Constant Field Values
-
AZURE_TEST_HTTP_CLIENTS_VALUE_OK_HTTP
public static final java.lang.String AZURE_TEST_HTTP_CLIENTS_VALUE_OK_HTTP
- See Also:
- Constant Field Values
-
AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL
public static final java.lang.String AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL
- See Also:
- Constant Field Values
-
interceptorManager
protected InterceptorManager interceptorManager
-
testResourceNamer
protected TestResourceNamer testResourceNamer
-
testContextManager
protected TestContextManager testContextManager
-
-
Method Detail
-
setupClass
@BeforeAll public static void setupClass()
Before tests are executed, determines the test mode by reading theAZURE_TEST_MODE
environment variable. If it is not set,TestMode.PLAYBACK
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
setupTest
@BeforeEach public void setupTest(org.junit.jupiter.api.TestInfo testInfo)
Sets-up thetestResourceNamer
andinterceptorManager
before each test case is run. Then calls its implementing class to perform any other set-up commands.- Parameters:
testInfo
-TestInfo
to retrieve test method name.
-
teardownTest
@AfterEach public void teardownTest(org.junit.jupiter.api.TestInfo testInfo)
Disposes ofInterceptorManager
and 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 java.lang.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 java.util.stream.Stream<com.azure.android.core.http.HttpClient> getHttpClients()
Returns a list ofHttpClients
that should be tested.- Returns:
- A list of
HttpClients
to be tested.
-
shouldClientBeTested
public static boolean shouldClientBeTested(com.azure.android.core.http.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.
-
getUnitTestOutDir
protected abstract java.net.URL getUnitTestOutDir()
Get the output directory where UT artifacts are stored.- Returns:
- The output directory where UT artifacts are stored.
-
sleepIfRunningAgainstService
protected void sleepIfRunningAgainstService(long millis)
Sleeps the test for the given amount of milliseconds ifTestMode
isn'tTestMode.PLAYBACK
.- Parameters:
millis
- Number of milliseconds to sleep the test.- Throws:
java.lang.IllegalStateException
- If the sleep is interrupted.
-
-