Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Only JUnit cannot be used for e2e testing in Java. you would need to use lot of 3rd party tools that you can use in different layers of testing.

Example to test your DAO calls using JUnit you may ideally not want a connection to the database cause if your tests run while using Maven and there is no data present in the database at that moment your tests are going to fail ending in stopping the entire build for you project which should not happen during build in production environments.

So how to get over this problem. You will need to mock your database calls are you can use MOCKITO 3rd party toll to mock database calls. So there are other tools available on net which you can use.

Ideally use JUnit only to test code which contains some logic and you expect this logic to work a particular way. To test the various methods in your code having the logic in the business layer of your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top