How to get InitialContext value in test class

463 Views Asked by At

I am trying to Mock the InitialContext Value to my test class, but was getting the below exception.

Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial.

  1. I tried using @Mock and @Injectmocks
  2. I used the below code in my test class

    when((DataSource) ctx.lookup("Database name")).thenReturn(dataSource);
    

    but still getting the same excpetion.

The complete test method:

public void getdetails() { 

  MainclassObj mainclassObj=new MainclassObj(); 
  InitialContext ctx = Mockito.mock(InitialContext.class); 

  when((DataSource) ctx.lookup("Database name")).thenReturn(dataSource); 
  //This is the manin class where DB call is made.
  mainclassObj.getRequiredData(String value); 
  when(response.getStaus).thenReturn(200); 
}

Could anyone help me to resolve this issue?

0

There are 0 best solutions below