Hello Ayush Shrivastava,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
To unit test the AzureResourceManager operations, there is a need to mock the dependencies you used properly. Since sqlServerManager is null, mock it and set it in the AzureResourceManager, also SqlServers class to return the mock SqlServerManager and the ServiceClient to return the desired result. Check this example and links below:
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.sql.SqlServerManager;
import com.azure.resourcemanager.sql.SqlServers;
import com.azure.resourcemanager.sql.models.JobAgents;
import com.azure.resourcemanager.sql.models.SqlServer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.*;
public class AzureResourceManagerTest {
@Mock
private AzureResourceManager azureResourceManager;
@Mock
private SqlServerManager sqlServerManager;
@Mock
private SqlServers sqlServers;
@Mock
private JobAgents jobAgents;
@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
// Mock the sqlServerManager and sqlServers
when(azureResourceManager.sqlServers()).thenReturn(sqlServers);
when(sqlServers.manager()).thenReturn(sqlServerManager);
when(sqlServerManager.serviceClient().getJobAgents()).thenReturn(jobAgents);
}
@Test
public void testGetJobAgents() {
// Call the method to test
JobAgents result = azureResourceManager.sqlServers()
.manager()
.serviceClient()
.getJobAgents();
// Verify the result
assertNotNull(result);
assertEquals(jobAgents, result);
}
}
https://github.com/Azure/azure-sdk-for-java/wiki/Unit-Testing and https://learn.microsoft.com/en-us/dotnet/azure/sdk/unit-testing-mocking
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.