0

我正在尝试将InitialContext值模拟到我的测试类,但得到了以下异常。

引起:javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中:java.naming.factory.initial。

  1. 我尝试使用@Mock@Injectmocks
  2. 我在测试类中使用了以下代码

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

    但仍然得到同样的结果。

完整的测试方法:

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); 
}

谁能帮我解决这个问题?

4

0 回答 0