Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify a method is called or not in Unit Test

Tags:

I have a unit test I am checking whether a method is called once or not so I attempted this way:-

This is my Mock of ILicenseManagerService and I am passing its object through constructor.

    public Mock<ILicenseManagerService> LicenseManagerService { get { return SetLicenseManagerServiceMock(); } }          private Mock<ILicenseManagerService> SetLicenseManagerServiceMock()         {             var licencemangerservicemock = new Mock<ILicenseManagerService>();             licencemangerservicemock.Setup(m => m.LoadProductLicenses()).Returns(ListOfProductLicense).Verifiable();              return licencemangerservicemock;         }          public static async Task<IEnumerable<IProductLicense>> ListOfProductLicense()         {             var datetimeoffset = new DateTimeOffset(DateTime.Now);              var lst = new List<IProductLicense>             {                 GetProductLicense(true, datetimeoffset, false, "1"),                 GetProductLicense(true, datetimeoffset, false, "2"),                 GetProductLicense(true, datetimeoffset, true, "3")             };              return lst;         } 

I am using this mock object to set _licenseManagerService and calling the LoadProductLicenses() in method under test. like this. licences are coming fine.

    var licenses = (await _licenseManagerService.LoadProductLicenses()).ToList(); 

My attempt for verify the call to this method -

     LicenseManagerService.Verify(m => m.LoadProductLicenses(),Times.Once); 

But when I run my unit test, an exception coming that say method is not invoked at all. Where I am doing wrong ?

EDIT @dacastro I am invoking the same mock here is my unit test.

    [TestMethod]         [TestCategory("InApp-InAppStore")]         public async Task return_products_from_web_when_cache_is_empty()         {             // this class basically for setting up external dependencies             // Like - LicenceManagerService in context, i am using this mock only no new mock.             var inAppMock = new InAppMock ();                                 // object of Class under test- I used static method for passing external                      //services for easy to change              var inAppStore = StaticMethods.GetInAppStore(inAppMock);              // method is called in this method             var result = await inAppStore.LoadProductsFromCacheOrWeb();              // like you can see using the same inAppMock object and same LicenseManagerService             inAppMock.LicenseManagerService.Verify(m => m.LoadProductLicenses(),Times.Once);                                 } 
like image 567
loop Avatar asked Jun 25 '14 07:06

loop


People also ask

How do you verify that a method is called in Mockito?

Mockito verify only method call If we want to verify that only one method is being called, then we can use only() with verify method.

What is the use of Verify method in JUnit?

Verify in Mockito simply means that you want to check if a certain method of a mock object has been called by specific number of times. When doing verification that a method was called exactly once, then we use: ? verify(mockObject).

Which method in Mockito verifies that no interaction has happened with a mock in Java?

Mockito verifyZeroInteractions() method It verifies that no interaction has occurred on the given mocks. It also detects the invocations that have occurred before the test method, for example, in setup(), @Before method or the constructor.


1 Answers

LicenseManagerService.Verify(m => m.LoadProductLicenses(),Times.Once); 

By calling the LicenseManagerService property, you're creating a new mock object. Naturally, no invocations have ever been performed on this instance.

You should change this property's implementation to return the same instance every time it is called.

like image 124
dcastro Avatar answered Sep 20 '22 06:09

dcastro