0

I have one service class and one dao class. where service class getting some input from dao class.

public SomeDao{
 public String getValue(String urlType){
  String value = "";

 //do something here
 return value
 }
}

public class SomeClass {
@Autowired
SomeDao dao;

  public String checkurl(String str1, String str2) throws IOException 
  {     
    Value= dao.getValue("urlType");
    //Do something with URLResultAsString       
  }
}

writing some test for checkurl method.

@RunWith(PowerMockRunner.class)
@PrepareForTest({SomeClass.class, SomeDao.class})
public Class TestSomeClass {

 private SomeClass someClass;
 private SomeDao Dao = PowerMockito.mock(SomeDao.class);

 @Test
 public void test_checkurl() throws Exception {  
   PowerMockito.doReturn("Value").when(Dao).getValue(Mockito.anyString());   

   SomeClass.checkurl("str1", "str2");
   }
 }

but it giving null pointer exception here. whats wrong is done in Mocking Dao Class

Akhil Kumar
  • 140
  • 1
  • 9
  • The `Dao` you mock and the one inside of the `SomeClass` are two different variables. You never pass the mocked `Dao` object into `SomeClass`. Btw. `someClass == ProcessClass` in your example? Because you use them as if they were the same variable. – Amongalen Mar 09 '20 at 13:16
  • Appologies, its same, someClass is processclass. typo mistake, Edited the Question – Akhil Kumar Mar 09 '20 at 13:18
  • Use constructor autowiring instead of field – Misha Lemko Mar 09 '20 at 14:49

0 Answers0