Unable to unit test AzureResourceManager.java operations

Ayush Shrivastava 100 Reputation points
2024-09-29T08:28:31.7066667+00:00

I have this method in my java code and I am trying to unit test this

AzureResourceManager azure = // assume that this is getting initialised properly
return azure.sqlServers()
       .manager()
       .serviceClient()
       .getJobAgents();

I am not able to unit test this because azure.sqlServers() returns null.

even if I do

when(azureResourceManager.sqlServers()).thenReturn(mock(SqlServers.class));

Because if I see this method

public SqlServers sqlServers() {
    return this.sqlServerManager.sqlServers();
}

Now this sqlServerManager is null. Can someone please help me unit test this.

Azure SQL Database
{count} votes

2 answers

Sort by: Most helpful
  1. Sina Salam 10,491 Reputation points
    2024-09-30T01:59:24.56+00:00

    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.

    0 comments No comments

  2. Sai Raghunadh M 390 Reputation points Microsoft Vendor
    2024-09-30T11:33:00.83+00:00

    Hi @ Ayush Shrivastava,

    Thanks for the question and using MS Q&A platform.

    In addition to Sina Salam.

    • Make sure that you mock the ServiceClient which is necessary for getJobAgents()
    • Each method in the chain needs to return a valid mock to avoid NullPointerException. By ensuring that serviceClient() is mocked and returns the mock(ServiceClient.class), you can prevent this issue.

    Please check with this example.

    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.ServiceClient;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.mockito.Mock;
    import org.mockito.MockitoAnnotations;
    import static org.junit.jupiter.api.Assertions.assertNotNull;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    import static org.mockito.Mockito.*;
    public class AzureResourceManagerTest {
        @Mock
        private AzureResourceManager azureResourceManager;
        @Mock
        private SqlServerManager sqlServerManager;
        @Mock
        private SqlServers sqlServers;
        @Mock
        private ServiceClient serviceClient;
        @Mock
        private JobAgents jobAgents;
        @BeforeEach
        public void setUp() {
            MockitoAnnotations.openMocks(this);
            // Set up the mock behavior
            when(azureResourceManager.sqlServers()).thenReturn(sqlServers);
            when(sqlServers.manager()).thenReturn(sqlServerManager);
            when(sqlServerManager.serviceClient()).thenReturn(serviceClient);
            when(serviceClient.getJobAgents()).thenReturn(jobAgents);
        }
        @Test
        public void testGetJobAgents() {
            // Call the method being tested
            JobAgents result = azureResourceManager.sqlServers()
                                                   .manager()
                                                   .serviceClient()
                                                   .getJobAgents();
            // Verify the result
            assertNotNull(result);
            assertEquals(jobAgents, result);
        }
    }
    

    Hope this helps. Do let us know if you any further queries.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.