我在为带有While循环的方法编写JUnit测试时遇到了一些困难。 我的方法如下所示:
private void deleteMethod(DeleteRequest dr){ // below statement am calling some service which returns me a object after querying it from Database. SomeObject ob = db.getdata(dr); while(ob != null) { // this method deletes the Data from DB db.deleteData(ob); // again calling the same service operation as we did before while loop. I have a situation where my service only returns single record at a time. It is avoidable that I need to do a dirty job. ob = db.getdata(dr); }下面是我的JUnit:
@Test public void testDeleteMethod() throws Exception{ DeleteRequest mockDR = new DeleteRequest(); mockDR.setX(y); SomeObject mockOB = new SomeObject(); mockOB.setZ(k); // making a mockcall to the method before I can assert anything mockClassObject.deleteMethod(mockDR); }上面的JUnit方法永远都会被执行,我明白它不会脱离while循环。 我如何处理这个问题。 只是提到使用Mockito框架,我不知道是否有任何方法可以处理Mockito中的这种情况。
I am facing some difficulty in writing a JUnit test for a method with a While loop. My method looks like below:
private void deleteMethod(DeleteRequest dr){ // below statement am calling some service which returns me a object after querying it from Database. SomeObject ob = db.getdata(dr); while(ob != null) { // this method deletes the Data from DB db.deleteData(ob); // again calling the same service operation as we did before while loop. I have a situation where my service only returns single record at a time. It is avoidable that I need to do a dirty job. ob = db.getdata(dr); }Below is my JUnit:
@Test public void testDeleteMethod() throws Exception{ DeleteRequest mockDR = new DeleteRequest(); mockDR.setX(y); SomeObject mockOB = new SomeObject(); mockOB.setZ(k); // making a mockcall to the method before I can assert anything mockClassObject.deleteMethod(mockDR); }The above JUnit method is getting struck in execution for ever and I understand that its not getting out of the while loop. How I can approach this issue. Just to mention am using Mockito framework and am not aware if at all there is any way to handle this situation in Mockito.
最满意答案
使用Mockito,您必须先模拟数据库连接,然后在测试方法之前使用@InjectMocks,构造函数注入或通过setter将其注入服务。 这就是我写测试的方式。
@Test public void testDeleteMethod() throws Exception{ DeleteRequest deleteRequest = new DeleteRequest(); deleteRequest.setX(y); SomeObject someObject = new SomeObject(); someObject.setZ(k); Database db = Mockito.mock(Database.class); // Notice chain of calls Mockito.when(db.getdata(deleteRequest)) .thenReturn(someObject).thenReturn(null); // TODO Inject the Database object into your mockClassObject. // making a mock call to the method before I can assert anything mockClassObject.deleteMethod(deleteRequest); Mockito.verify(db, Mockito.times(2)).getdata(deleteRequest); Mockito.verify(db).deleteData(someObject); }您可以看到我在db.getData()方法上链接两次调用,第一次返回someObject ,第二次返回null 。
Using Mockito, you have to mock the db connection and inject it into your service using @InjectMocks, constructor injection, or via a setter before you test your method. This is how I would write your test.
@Test public void testDeleteMethod() throws Exception{ DeleteRequest deleteRequest = new DeleteRequest(); deleteRequest.setX(y); SomeObject someObject = new SomeObject(); someObject.setZ(k); Database db = Mockito.mock(Database.class); // Notice chain of calls Mockito.when(db.getdata(deleteRequest)) .thenReturn(someObject).thenReturn(null); // TODO Inject the Database object into your mockClassObject. // making a mock call to the method before I can assert anything mockClassObject.deleteMethod(deleteRequest); Mockito.verify(db, Mockito.times(2)).getdata(deleteRequest); Mockito.verify(db).deleteData(someObject); }You can see that I chain the calls on the db.getData() method two times, the first time returns someObject and the second time returns null.
更多推荐
发布评论