How to write Java e2e (end to end) tests

Dixel picture Dixel · Apr 19, 2014 · Viewed 9.2k times · Source

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...

Answer

amb picture amb · Apr 19, 2014

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 :)