What is the best way to count method invocations in a Unit Test. Do any of the testing frameworks allow that?
相关问题
- Dependencies while implementing Mocking in Junit4
- How to unit test a reactive component where ngCont
- Stubbing a method with dependend generic arguments
- Access for global variables JavaScript unit testin
- Googletest Parametrized tests crash
相关文章
- How to replace file-access references for a module
- How to mock methods return object with deleted cop
- What is a good way of cleaning up after a unit tes
-
EF6 DbSet
returns null in Moq - React testing library: Test attribute / prop
- React/JestJS/Enzyme: How to test for ref function?
- python unit testing methods inside of classes
- Running into System.MissingMethodException: Method
In Mockito you can do something like this:
You can count number of method invocation by using interface Answer in Mockito.
Depending on what methods you want to count, you can create a test config, with a
@Before
advice matching your class / package / method:You can use vanilla AspectJ or Spring AOP via above or XML configs if you find it easier.
You can create different pointcuts / aspect if you need to.
It sounds like you may want to be using the .expects(1) type methods that mock frameworks usually provide.
Using mockito, if you were testing a List and wanted to verify that clear was called 3 times and add was called at least once with these parameter you do the following:
(From http://code.google.com/p/mockito/wiki/MockitoVSEasyMock)
It sounds like you may want a test spy. See, for example, Mockito.spy().
You've got a few options
1) Add some special code which counts invocations in the function. It will work, but it's not a great solution.
2) After you run your unit tests, check the code coverage. Most coverage tools will count invocations but they are really designed for post-processing.
3) Use a profiler. A profiler will let you count how many times a function is invoked. This is a very manual process so it's not really designed for unit testing.
A better solution would be to check that output is what you expect rather than checking how it works internally.