So... recently I was forcefully introduced to the wonderful world of unit testing(karma for grunt(AngularJS in the middle)), and it's been a wild ride; I really cannot believe how my code held together without test cases...
Anyways, going back to my question, in the Java project I'm doing as well, I've been adding unit tests to most of the classes... but now I've run into a question which i cannot find an answer to (or perhaps i'm asking the wrong questions), but how do i properly make an e2e test in Java? can i use JUnit for this? or should I use something else entirely? my project also involves a lot of Database movement, and just recently i read that if a test uses a database, it's not a unit test anymore... should this tests be modified as well?
This is actually the first time I post a question here =) but I've been using the site to find answers for years... if there's any extra data i should add or change, please, let me know... and I thank everyone here in advance, not just for the answers to this question, but for all the answers to all the questions this awesome site helps people share...
All the answers before mine are useful so I won't repeat the same information.
Yes, if in your test you hit a database then it's not a unit but an integration test, or as it is called in the javascript community, end-to-end (but I don't know why they did not stuck with the same terminology).
From my experience it is better to start a transaction in the @Before
and to reject it in the @After
method. In this way you can be sure that your results are as close as possible to the real world since you are not mocking the database access (so you can detect if a problem happens on the database side).
You will also see a lot of opinions in the other way around, with people wanting to mock the database access so the tests can run faster, but I think the speed, in this time and age, will not be an issue anymore so you can hit the database and not mock it :)
While developing actual code, you should ideally write unit tests for major number of your classes with functionality. While doing this, majority of the code is not totally functional. So you have write unit tests.
Your unit tests should run standalone irrespective of the server and database. This is made possible using mocking frameworks like mockito, easymock and jmockit.
Once the whole functionality starts working, you can write integration tests using junit if you want to. It is important to make sure that each test is independent of any other test. Also, if possible, you should even automate the startup and shutdown of the server within the test suite. For example, jersey-test-framework for testing RESTful APIs has an embedded grizzly server where the web services get deployed for the test and the actual server is nowhere in picture.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With