模拟类

时间:2017-07-24 15:28:52

标签: java spring unit-testing mockito

public Employe{
   public Double method1(int id){
      return method2(id);
   }
   public Double method2(int id) throws DatabaseException{
      //Some Code
      Employe e = employeRepositoy.getEmployeWithId(id);
      return e.getSalary();
   }
}

现在我的两个测试场景就像这样

@Test(expected = DatabaseException.class)
public void testemploye1() throws ServiceException, DatabaseException {
    Employe employe = Mockito.spy(new Employe());
    when(employe.method2(45585)).thenThrow(DatabaseException.class);
    employe .method1(45585);
}

@Test
public void testEmployee() throws ServiceException, DatabaseException {
    Employe employe = Mockito.spy(new Employe());
    when(employe.method2(45585)).thenReturn(3256);
    Double sal = employe .method1(45585);
    isTrue(sal.equals(3256));
}

首次测试scenerio运行罚款并抛出数据库异常。但是在第二个测试中,scenrio而不是模拟调用进入内部方法2并抛出空指针异常。我在这方面有点帮助。

3 个答案:

答案 0 :(得分:1)

我认为对于Spy来说,你需要使用以下语法来存根方法:

when(employe.method2(45585)).thenReturn(3256);

而不是:

{{1}}

答案 1 :(得分:1)

NPE正在发生,因为public class MainActivity extends AppCompatActivity { String nombreChef; Double ratingChef; private RecyclerView rvMain; private RVMainAdapter mRVMainAdapter; private ArrayList<Chef> data= new ArrayList<>(); private class AsyncCallWS extends AsyncTask<Void, Void, Void>{ @Override protected void onPreExecute() { } @Override protected Void doInBackground(Void... params) { SoapObject soapObject = new SoapObject(Conexion.NAMESPACE, Conexion.METHOD_NAME_CHEFS_CERCA); soapObject.addProperty("idUser", Integer.valueOf(5)); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(Conexion.URL); try { httpTransportSE.call(Conexion.SOAP_ACTION_TPNAME, envelope); String resultado = envelope.getResponse().toString(); JSONObject json = new JSONObject(resultado); nombreChef=json.getString("nombre_chef"); ratingChef=json.getDouble("rating"); //IM NOT SURE HOW TO PUT THE ARRAY RESPONSE INTO ARRAYLIST???? data.add(new Chef(12,nombreChef,"Riuos","21","Jr los belepos",12,"Delivery",ratingChef.toString(),"Activo")); } catch (Exception e) { Log.e("Estacion", e.getMessage()); //result = "0"; } return null; } @Override protected void onPostExecute(Void result) { } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AsyncCallWS task = new AsyncCallWS(); task.execute(); rvMain = (RecyclerView) findViewById(R.id.rv_prueba); rvMain.setLayoutManager(new LinearLayoutManager(MainActivity.this)); mRVMainAdapter = new RVMainAdapter(data); rvMain.setAdapter(mRVMainAdapter); } 为空。您是否确定 employeRepositoy正在通过?据我所知,它会因同一个NPE失败,也许如果你偷看实际的异常,你可能会发现testemploye1()也在抛出一个NPE。

我认为测试方法有点“关闭”,如果打算测试method2的行为(无论是直接调用还是通过method1()调用),那么我认为你应该嘲笑{{1然后注入它然后为它提供预期的行为。例如:

testemploye1()

然而,正如其他海报所指出的那样,这种测试方法确实看起来很奇怪。

答案 2 :(得分:0)

无法使用when(Object)来存根spies.

@Test
public void testEmployee() throws ServiceException, DatabaseException {
  Employe employe = Mockito.spy(new Employe());
  doReturn(3256).when(employe).method2(45585);    
  //when(employe.method2(45585)).thenReturn(3256);
  Double sal = employe .method1(45585);
  isTrue(sal.equals(3256));
}