Class 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 using InterceptorManager.
    • 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
    • Constructor Detail

      • TestBase

        public TestBase()
    • Method Detail

      • setupClass

        @BeforeAll
        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, TestMode.PLAYBACK
      • beforeEach

        public void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
        Specified by:
        beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      • setupTest

        @BeforeEach
        public void setupTest​(org.junit.jupiter.api.TestInfo testInfo)
        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:
        testInfo - TestInfo to retrieve test method name.
      • teardownTest

        @AfterEach
        public void teardownTest​(org.junit.jupiter.api.TestInfo testInfo)
        Disposes of InterceptorManager 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 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(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 of HttpClients 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.
        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.
      • 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 if TestMode isn't TestMode.PLAYBACK.
        Parameters:
        millis - Number of milliseconds to sleep the test.
        Throws:
        java.lang.IllegalStateException - If the sleep is interrupted.