how to create mock for extended method

77 views
Skip to first unread message

Harshita Sharma

unread,
May 24, 2021, 3:51:14 PM5/24/21
to mockito
does anybody knows how to create mock for extended class?
class A{
      public String methodA(String str){
           return "hey"+ " "+ str;
      }
}
class B extends A{
       public  boolean methodB(){
             String str = methodA("amma"); 
              return true;
       }
}
i want to test methodB

Henri Tremblay

unread,
May 25, 2021, 4:39:10 AM5/25/21
to moc...@googlegroups.com
B b = mock(B.class); ???
when(b.methodB()).thenReturn(true); ???

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/2eb90409-1c3f-4324-b1c4-f8099bddc25fn%40googlegroups.com.

Harshita Sharma

unread,
May 25, 2021, 2:25:30 PM5/25/21
to mockito
public class BucketDetailServiceImpl extends AbstractService <Integer, BucketDetail> implements IBucketDetailService    {

                @Autowired
                 private IBucketDetailDao bucketDetailDao;

                public BucketResponseWrapper createBucket()   {
                            String accountId="12345";
                            BucketDetail bucketdetail = new BucketDetail();
                            Account account = bucketDetailDao.getAccountByAccountId(accountId);
                            bucketdetail.setAccount(account);
                            bucketdetail.setContainerId("abc");

                            bucketdetail = create (bucketdetail);                                             // AbstractService.create(BucketDetail arg)   //create bucket in database

                            BucketResponseWrapper bucketResponseWrapper = new BucketResponseWrapper ();
                            bucketResponseWrapper.setAccount(bucketdetail.getAccount);
                            bucketResponseWrappe.setContainer(bucketdetail.setContainerId);
                            return bucketResponseWrapper;
                }
}

@RunWith(SpringRunner.class)
public class TestClass {

               @Mock
                private IBucketDetailDao bucketDetailDao;

                 @Mock                                           // create mock for extended AbstractService
                   AbstractService abstractService;

                 @InjectMock
                  BucketDetailServiceImpl bucketDetailService;

              @Test
               public void createBucketSuccessTest(){
                           Account account= new Account();
                           account.setName("Harry");
                           account.setAdd("privateDrive");
                           account.setPhone("9088890556");

                           BucketDetail bucketdetail = new BucketDetail();
                           bucketdetail.setAccount(account);
                           bucketdetail.setContainerId("abc");

                           when(bucketDetailDao.getAccountByAccountId(anyString())).thenReturn(account);

                   when(AbstractService.create(any(BucketDetail.class))).thenReturn(bucketDetail);

                           BucketResponseWrapper bucketResponseWrapper  = bucketDetailService.createBucket();

           assertNotNull(bucketResponseWrapper);
                           assetEqual(bucketResponseWrapper.getAccount,account);
              }

i just want to know ... 
why i  am getting .NullPointerException here
i think because may be i cant create mock for extended super class???

Henri Tremblay

unread,
May 25, 2021, 4:02:07 PM5/25/21
to moc...@googlegroups.com
when(AbstractService.create(any(BucketDetail.class))).thenReturn(bucketDetail);  looks like a static call. It's not using the mock at all.

Harshita Sharma

unread,
May 27, 2021, 2:30:13 PM5/27/21
to moc...@googlegroups.com
no it's not a static class ,it's an Abstract class

Harshita Sharma

unread,
May 27, 2021, 2:30:23 PM5/27/21
to moc...@googlegroups.com
Is there any rule about mocking abstract class or something like that in mockito ...?

Harshita Sharma

unread,
May 27, 2021, 2:34:15 PM5/27/21
to moc...@googlegroups.com
public abstract class AbstractService<Pk, Entity> implements IGenericService<Pk, Entity> {
   private IGenericDao<Pk, Entity> a;
   public IGenericDao<Pk, Entity> getDao() {
      return this.a;
   }

   public void setDao(IGenericDao<Pk, Entity> var1) {
      this.a = var1;
   }

public Entity create(Entity var1) {
      try {
         return this.a.create(var1);
      } catch (BusinessException var3) {
         throw new BusinessException(var3.getErrCode());
      }
   }

   public Entity update(Entity var1) {
      try {
         return this.a.update(var1);
      } catch (BusinessException var3) {
         throw new BusinessException(var3.getErrCode());
      }
   }
}

this is my AbstractService class
 

Henri Tremblay

unread,
May 27, 2021, 2:38:35 PM5/27/21
to moc...@googlegroups.com
I said static call, not class.

This abstract class should be perfectly mockable.

Harshita Sharma

unread,
May 27, 2021, 3:03:55 PM5/27/21
to moc...@googlegroups.com
oohh yeah my mistake...i should say static call or method, obviously.
but i don't get it ...why i am getting NPE


Tim van der Lippe

unread,
May 27, 2021, 3:05:32 PM5/27/21
to mockito
Unfortunately it is difficult for us to understand the full context of your application and why you are getting an exception. I recommend starting small and adding smaller pieces at a time, to find out what the culprit is. Additionally, I recommend a read through of https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html to learn about the behavior of Mockito and how to interact with the library.

Op do 27 mei 2021 om 20:03 schreef 'Harshita Sharma' via mockito <moc...@googlegroups.com>:
Reply all
Reply to author
Forward
0 new messages