-2

I'm working with JUnit and Mockito to make some unitary tests. I want to handle the excepcion by using the Mockito.doThrow() and I've already made use of it in another tests and it worked the finest way possible. Now I have:

Assigned newAssigned  = new Assigned();

      Mockito.doThrow(new SdkFault()).when(Api).addLicenseAssigned("2144", newAssigned  );

and when I run the test while making debugging of it when I find this

   Api.addLicenseAssinged(licenseId, newAssigned);

It should get the excepcion and throw it me right? Or am I making something wrong? And I'm not mocking static methods.

R.Quintal
  • 43
  • 1
  • 3
  • 13
  • what is Api, is it a class or class isntance? addLicenseAssigned is a static method? – pvpkiran Sep 15 '17 at 10:14
  • @pvpkiran Api is a class but I don't have access to him unless I uncompile the code because its an api to comunicate with the server. But I think that It doesn't even matter because we never reach the code taking into account that we automatically say "when the method xxxx is called return the exception SdkFault()" – R.Quintal Sep 15 '17 at 10:44
  • Possible duplicate of [Mocking static methods with Mockito](https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito) – GhostCat Sep 15 '17 at 11:25
  • Please rework your question. Dont put information here or there in comments. Instead provide a real [mcve] **within** your question. And follow java naming conventions. Variables go `camelCase`. By naming an object `Api` instead of `api` you are creating that you want to mock a **static** method call - which is not possible with Mockito. – GhostCat Sep 15 '17 at 11:26

1 Answers1

1

I think your issue is the stubbing you are using is trying to match against an object you have created in your test and so it is not the same object that is called in your class under test, namely:

Assigned newAssigned  = new Assigned();

To solve this you should use matchers instead for your stubbing i.e.:

Mockito.doThrow(new SdkFault()).when(Api)
   .addLicenseAssigned(Matchers.eq("2144"), Matchers.any(Assigned.class));
Plog
  • 9,164
  • 5
  • 41
  • 66
  • I'm not mocking static methods! I'm working with an Api wich code I can't see, it only provides the methods for me to connect with the server. Like an webservice. That's why I'm wondering why is this happening. Thanks for your time in advance and I aprecciate all the help you can provide me. – R.Quintal Sep 15 '17 at 11:03
  • I assumed it was static because. 1. It starts with a capital letter which should not be used for class instances. 2. You said it was a class not an object (i.e. a class instance). Can you show the code where you instatiate Api then? – Plog Sep 15 '17 at 11:05
  • @Mock private LicenseApi licenseApi; Mockito.when(protexServer.getLicenseApi()).thenReturn(licenseApi); – R.Quintal Sep 15 '17 at 11:09
  • Ok, final question. Can you provide the code where you are actually calling the class under test and is newAssigned passed to this method? – Plog Sep 15 '17 at 11:11
  • I'm calling the call under test when I created the test class and I don't undrstand what you want by saying "is newAssigned passed to this method".. Thank you one more time for your time. – R.Quintal Sep 15 '17 at 11:18
  • Updated my answer – Plog Sep 15 '17 at 11:29
  • Thank you for your help, it's working now and I've been going crazy bout this. Thank you very much. – R.Quintal Sep 15 '17 at 11:40