Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm learning testing with EasyMock and Mockito. What are differences between them?
What are advantages and disadvantages of any of them? Which one is better to use?
Although this question is majorly opinion based but you get the differences from here:
Differences:
no record/replay modes - no need for them. There only 2 things you can do with Mockito mocks - verify or stub. Stubbing goes before execution and verification afterwards.
all mocks are 'nice' (even somehow nicer, because collection-returning methods return empty collections instead of nulls). Even though mocks are nice, you can verify them as strictly
as you want and detect any unwanted interaction.
explicit language for better readability: verify() and when() VS the mixture of expect(mock.foo()) and mock.foo() (plain method call without 'expect'). I'm sure some of you will find this argument subjective :)
simplified stubbing model - stubbed methods replay all the time with stubbed value no matter how many times they are called. Works exactly like EasyMock's andStubReturn(), andStubThrow(). Also, you can stub with different return values for different arguments (like
in EasyMock).
Verification of stubbed methods is optional because usually it's more important to test if the stubbed value is used correctly rather than where's it come from.
verification is explicit - verification errors point at line of code showing what interaction failed. verification in order is flexible and doesn't require to verify every single interaction.
custom argument matchers use hamcrest matchers, so you can use your existing hamcrest matchers. (EasyMock can also integrate with hamcrest though it is not a part of EasyMock but hamcrest. See the
documentation of hamcrest).
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Kind of a meta question, but I was wondering if there would ever be cases where writing a unit test for a unit test would make sense.
You don't have to write a test for that, generally its considered Mutation Testing.
Mutation Testing is a type of software testing in which certain statements of the source code are changed/mutated to check if the test cases are able to find errors in source code. The goal of Mutation Testing is ensuring the quality of test cases in terms of robustness that it should fail the mutated source code.
From Guru99
There's a few well known examples,
PIT for Java
MytPy for Python
Stryker looks like its the one for Javascript.
You could write you own tests that mutate your code itself, but its sensible to use OSS projects and help contribute to them if theres any unique needs given the complexity of the area. You don't want to have to own your own mutating framework either and all the maintance with that.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I see lots of developers writing unit tests like this:
[TestMethod]
public void AddTrainer_InvokesProviderAddTrainer()
{
//ARRANGE
var trainerBusiness = new TrainersBusiness(_trainerProviderMock.Object);
//ACT
trainerBusiness.AddTrainer(new UserProfile());
//ASSERT
_trainerProviderMock.Verify(v => v.AddTrainer(It.IsAny<UserProfile>()), Times.Once);
}
I understand what is happening, I just don't quite what problem this intends to solve. Here the test is proving that the AddTrainer method in the business object is calling the AddTrainer method in the provider.
As long as the trainer is correctly added, what is the importance to me how the business method goes about doing it? To me this means if we decide we want to change the way the business method goes about adding a trainer in future, even though the trainer is still added, this unit test fails and we have to rewrite it.
It seems to me that this type of test is just confirming no one has changed your method, a bit like a checksum, rather than confirming anything useful with the behavior.
What am I missing?
You see this frequently in Unit Tests of void return methods because people don't know what to assert. Sometimes it's a good idea and sometimes it is just to get high code coverage numbers. I think the example you have falls under code coverage. The key for me is the "It.IsAny" usage combined with the name of the test method. The author could just have named it AddTrainer_GetCodeCoverage. If you had 2 tests named AddTrainer_ValidTrainer_InvokesProviderAddTrainer and AddTrainer_InvalidTrainer_ThrowsSpecificExeception each making different checks on the Mock and confirming the values passed to the mock, you'd have something of value.
This is to make sure that no one messes with the TrainersBusiness.AddTrainer method by adding some condition which would prevent UserProfile from adding into the TrainerProvider
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I need to provide unit tests for an application written in c++, this is a very big application and content many sources (.h, .cpp) , actually I don't know where to start? How to process? ...
So any help is more than welcome.
Thanks
Did you upset someone? Given there are no unit tests, the chances of the code being written to be testable range from slim to absolutely none.
Without seeing the code and spending several weeks if not months with it no one can give you more than a general strategy.
There will be some functions you can write unit tests for. Those will be ones where the arguments are easy to generate, they do very few things, one thing would be nice, and they don't have side effects. Attack these first, get them out of the way.
There will be others which nearly fit the above. Now you'll be tempted to re-engineer them a bit so they do, don't do it until you have some sort of test. Write tests for the bits you can. Write integration tests where you can't.
So the basic idea is to get as many tests as you can before you start changing the code, so you can test it and then, to make the smallest change possible to make the code better and write the tests first!
There are a fair few patterns or strategies you can use (get a good book on re-factoring legacy code), start with the simple ones.
Prepare for dismay, hard work and rework, but the best piece of advice I can give is don't try to take short cuts, after all that's what the chuffer who left you with this did isn't it?
Grab a good test framework.
I have used google test a lot with my last company, and it was pretty good, though there are likely better around.
Reading:
http://code.google.com/p/googletest/
Comparison of c++ unit test frameworks
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
How can I unit test a JSP? I want to ensure there are no tag misspellings, etc.
I am already aware of the HtmlUnit/HttpUnit/JWebUnit/Selenium possibilities for System testing. I want more of a unit test.
I would say that testing a web app (be it JSP or other) using its web interface, with one of the above mentioned frameworks is not unit testing in the strict sense. Unit testing means testing small parts of your app (one method, one class) in isolation. With a web framework, you test the web app as a whole, so this is more like system or integration test. Which is not to say it is not useful - on the contrary - just it is better to clarify terminology.
Having said that, if I were to test a JSP, I would most likely be satisfied with a system test of all specific scenarios associated with the JSP, using some of the tools mentioned above.
However, if you really want "classic" unit tests, I guess the closest you can get is compiling your JSP into a servlet class, then calling the servlet methods directly (from e.g. JUnit, using a mocking framework like EasyMock to prepare the Http request, response etc. objects).
Update: IMO the only reason to need unit tests for a JSP is if you have business logic in it. Which, in turn, goes against the separation of UI and business layer. So it is better to redesign the app and move the logic into a separate POJO (if you have the choice) than try to write contrived unit tests in order to test business logic where it doesn't belong.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can any of the Rhino experts explain me by giving a suitable example of the difference between the above methods on the MockRepository class (Rhino Mocks framework).
Where should one use Stub over Mock method or otherwise?
you should use a mock when you are going to verify that something happened on the object, like a method was called. You should use a stub when you just want the object to be involved in the test to return a value but it is not the thing you are testing. A stub which does not have a expectation fulfilled can never fail a test.
I think the general rule should be that you should only ever have a single mock object in a test, but may have several stubs which provide information to the mock object. I believe that more than 1 mock in a test is a code smell.
Although not a Rhino example Martin Fowler has a description of the difference
Also this question might be useful as might this one