Skip to content

Commit a944564

Browse files
author
Changjian Wang
committed
Add graceful test skipping when required environment variables are not set
- Updated ContentUnderstandingClientTestBase to include skipIfRequiredEnvironmentVariablesNotSet() utility method - Added @beforeeach method to all 17 Sample test classes - Tests now skip (instead of failing with NullPointerException) when CONTENTUNDERSTANDING_ENDPOINT or AZURE_CONTENT_UNDERSTANDING_KEY are not available - This allows Azure DevOps pipeline to run without test credential setup
1 parent 834912b commit a944564

18 files changed

+247
-0
lines changed

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/ContentUnderstandingClientTestBase.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,27 @@
1717
import com.azure.core.test.utils.MockTokenCredential;
1818
import com.azure.core.util.Configuration;
1919
import com.azure.identity.DefaultAzureCredentialBuilder;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.opentest4j.TestAbortedException;
2022

2123
class ContentUnderstandingClientTestBase extends TestProxyTestBase {
2224
protected ContentUnderstandingClient contentUnderstandingClient;
2325

26+
/**
27+
* Utility method to check if required environment variables are available.
28+
* If they are not available, the test will be skipped.
29+
* This is useful for integration tests that require real Azure credentials.
30+
*/
31+
protected static void skipIfRequiredEnvironmentVariablesNotSet() {
32+
String endpoint = Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT");
33+
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
34+
35+
if ((endpoint == null || endpoint.trim().isEmpty()) && (key == null || key.trim().isEmpty())) {
36+
throw new TestAbortedException(
37+
"Required environment variables CONTENTUNDERSTANDING_ENDPOINT or AZURE_CONTENT_UNDERSTANDING_KEY are not set. Skipping test.");
38+
}
39+
}
40+
2441
@Override
2542
protected void beforeTest() {
2643
ContentUnderstandingClientBuilder contentUnderstandingClientbuilder = new ContentUnderstandingClientBuilder()

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample00_ConfigureDefaultsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.azure.core.util.BinaryData;
1414
import com.azure.core.util.Configuration;
1515
import com.azure.identity.DefaultAzureCredentialBuilder;
16+
import org.junit.jupiter.api.BeforeEach;
1617
import org.junit.jupiter.api.Test;
1718

1819
import static org.junit.jupiter.api.Assertions.*;
@@ -26,6 +27,11 @@
2627
*/
2728
public class Sample00_ConfigureDefaultsTest {
2829

30+
@BeforeEach
31+
public void setUp() {
32+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
33+
}
34+
2935
@Test
3036
public void testConfigureDefaults() {
3137
// BEGIN: com.azure.ai.contentunderstanding.buildClient

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample01_AnalyzeBinaryTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.azure.core.util.polling.SyncPoller;
2020
import com.azure.identity.DefaultAzureCredentialBuilder;
2121
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
2324

2425
import static org.junit.jupiter.api.Assertions.*;
@@ -41,6 +42,11 @@
4142
*/
4243
public class Sample01_AnalyzeBinaryTest {
4344

45+
@BeforeEach
46+
public void setUp() {
47+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
48+
}
49+
4450
@Test
4551
public void testAnalyzeBinaryAsync() throws IOException {
4652
// BEGIN: com.azure.ai.contentunderstanding.buildClient

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample02_AnalyzeUrlTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.azure.core.util.polling.SyncPoller;
2020
import com.azure.identity.DefaultAzureCredentialBuilder;
2121
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
2324

2425
import static org.junit.jupiter.api.Assertions.*;
@@ -37,6 +38,11 @@
3738
*/
3839
public class Sample02_AnalyzeUrlTest {
3940

41+
@BeforeEach
42+
public void setUp() {
43+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
44+
}
45+
4046
@Test
4147
public void testAnalyzeUrlAsync() {
4248
// BEGIN: com.azure.ai.contentunderstanding.buildClient

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample03_AnalyzeInvoiceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.azure.core.util.polling.SyncPoller;
2323
import com.azure.identity.DefaultAzureCredentialBuilder;
2424
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
2627

2728
import static org.junit.jupiter.api.Assertions.*;
@@ -40,6 +41,11 @@
4041
*/
4142
public class Sample03_AnalyzeInvoiceTest {
4243

44+
@BeforeEach
45+
public void setUp() {
46+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
47+
}
48+
4349
@Test
4450
public void testAnalyzeInvoiceAsync() {
4551
// BEGIN: com.azure.ai.contentunderstanding.buildClient

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample04_CreateAnalyzerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.azure.identity.DefaultAzureCredentialBuilder;
2929
import org.junit.jupiter.api.AfterEach;
3030
import org.junit.jupiter.api.Assertions;
31+
import org.junit.jupiter.api.BeforeEach;
3132
import org.junit.jupiter.api.Test;
3233

3334
import static org.junit.jupiter.api.Assertions.*;
@@ -48,6 +49,11 @@
4849
*/
4950
public class Sample04_CreateAnalyzerTest {
5051

52+
@BeforeEach
53+
public void setUp() {
54+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
55+
}
56+
5157
private String createdAnalyzerId;
5258

5359
@AfterEach

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample05_CreateClassifierTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.azure.identity.DefaultAzureCredentialBuilder;
2020
import org.junit.jupiter.api.AfterEach;
2121
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
2324

2425
import static org.junit.jupiter.api.Assertions.*;
@@ -36,6 +37,11 @@
3637
*/
3738
public class Sample05_CreateClassifierTest {
3839

40+
@BeforeEach
41+
public void setUp() {
42+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
43+
}
44+
3945
private String createdAnalyzerId;
4046

4147
@AfterEach

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample06_GetAnalyzerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.azure.core.util.Configuration;
1212
import com.azure.identity.DefaultAzureCredentialBuilder;
1313
import org.junit.jupiter.api.Assertions;
14+
import org.junit.jupiter.api.BeforeEach;
1415
import org.junit.jupiter.api.Test;
1516

1617
import static org.junit.jupiter.api.Assertions.*;
@@ -25,6 +26,11 @@
2526
*/
2627
public class Sample06_GetAnalyzerTest {
2728

29+
@BeforeEach
30+
public void setUp() {
31+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
32+
}
33+
2834
@Test
2935
public void testGetAnalyzerAsync() {
3036
// BEGIN: com.azure.ai.contentunderstanding.buildClient

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample07_ListAnalyzersTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.azure.core.util.Configuration;
1313
import com.azure.identity.DefaultAzureCredentialBuilder;
1414
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.BeforeEach;
1516
import org.junit.jupiter.api.Test;
1617

1718
import static org.junit.jupiter.api.Assertions.*;
@@ -26,6 +27,11 @@
2627
*/
2728
public class Sample07_ListAnalyzersTest {
2829

30+
@BeforeEach
31+
public void setUp() {
32+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
33+
}
34+
2935
@Test
3036
public void testListAnalyzersAsync() {
3137
// BEGIN: com.azure.ai.contentunderstanding.buildClient

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample09_DeleteAnalyzerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.azure.core.util.Configuration;
1818
import com.azure.identity.DefaultAzureCredentialBuilder;
1919
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.BeforeEach;
2021
import org.junit.jupiter.api.Test;
2122

2223
import static org.junit.jupiter.api.Assertions.*;
@@ -34,6 +35,11 @@
3435
*/
3536
public class Sample09_DeleteAnalyzerTest {
3637

38+
@BeforeEach
39+
public void setUp() {
40+
ContentUnderstandingClientTestBase.skipIfRequiredEnvironmentVariablesNotSet();
41+
}
42+
3743
@Test
3844
public void testDeleteAnalyzerAsync() {
3945
// BEGIN: com.azure.ai.contentunderstanding.buildClient

0 commit comments

Comments
 (0)