mocking a method with a dynamic datasource

I need to test this method but I don’t know how can I test without connecting to the db on the server.

public List<String> searchQuery(String key) {
    List<String> resultList = null;
    try {
        this.createConnection(CheckDataServlet.getKey(Integer.parseInt(key)).getConnection().getName());
        stmt = conn.prepareStatement(CheckDataServlet.getKey(Integer.parseInt(key)).getSelect().trim());
        rs = stmt.executeQuery();
        resultList = this.getValues(rs);
    } catch ( SQLException e) {
        Archicon.getLogger().error("ValidationData.searchQuery error: "+e);
    }finally {
        this.closeConnection("searchQuery",conn,stmt,rs);
    }
    if (resultList == null) {
        resultList = new ArrayList<>();
    }

    return resultList;
}

public void createConnection(String key){
        try {
            ds = (DataSource) Archicon.getResource("jdbc/"+key);
            conn = ds.getConnection();
        } catch (SQLException | NamingException e) {
            throw new RuntimeException(e);
        }
    }

I tried testing with Mockito but always give me this error because the datasource is null:

java.lang.NullPointerException
at it.sisal.archicon.model.valor.ValidationData.createConnection(ValidationData.java:157)
at it.sisal.archicon.model.valor.ValidationData.searchQuery(ValidationData.java:23)
at it.sisal.archicon.model.valor.AwpSapNewRepositoryTest.searchQuery(AwpSapNewRepositoryTest.java:95)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

I tried to build this test class:

@RunWith(MockitoJUnitRunner.class)
public class AwpSapNewRepositoryTest {
     @Mock
   private ValidationData validationData;
@Before
    public void setup() throws SQLException {
        MockitoAnnotations.initMocks(this);
     try (final MockedStatic<Archicon> archicon= Mockito.mockStatic(Archicon.class)) {
      DataSource ds = mock(DataSource.class);
      archicon.when(() -> Archicon.getResource(any())).thenReturn(ds);
     }
 @Test
    public void searchQuery() throws NamingException {


      //  System.setProperty("jboss.server.config.dir","C:\\Program Files\\wildfly-18.0.0.Final\\standalone\\configuration\\archicon");
       Archicon.setConfigPath("C:\\Program Files\\wildfly-18.0.0.Final\\standalone\\configuration\\archicon");
        when(validationData.searchQuery("11")).thenReturn(new ArrayList<>(List.of("Name, 99","Stuff, 99")));
        validationData = new ValidationData();
        assertEquals(true,validationData.searchQuery("11").size()>0);
    }

    

Does anybody know a good way to test this method?

>Solution :

You could mock the static method of the Archicon class with mockito to return a mock of DataSource:

try (final MockedSatic<Archicon> archicon= Mockito.mockStatic(Archicon.class)) {
    DataSource ds = mock(DataSource.class);
    archicon.when(() -> Archicon.getResource(any())).thenReturn(ds);
}

Leave a Reply