We've all seen them. Top 10 Java interview questions, Spring questions, etc. I get sent these lists from time to time through linked in and other places.
Here's a link to a recent one:
https://intellipaat.com/interview-question/junit-interview-questions/
OK, JUnit. You wouldn't think there was that much to say really. It's a simple testing framework, probably the simplest out there but even so a decent Q and A site can teach you things you hadn't thought about before.
This isn't a decent Q and A site.
We'll skip Q1 which was just a 'What is JUnit' question and expects you to repeat some standard mantras for the answer.
Q2. What are important features of JUnit?
A fatuous question of the kind you hope you don't get in a real interview as whatever set of answers you provide the interviewer will likely have a different list in front of him so can always sit smug.
These are the answers these guys provide:
- It is an open source framework.
- Provides Annotation to identify the test methods.
- Provides Assertions for testing expected results.
- Provides Test runners for running tests.
- JUnit tests can be run automatically and they check their own results and provide immediate feedback.
- JUnit tests can be organized into test suites containing test cases and even other test suites.
- JUnit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.
1, It's Open Source.
Hmm. Why is this an important feature? Who cares as long as you can run tests and it provides feedback as to whether the tests passed or failed.
Someone might argue it's important that you can see whether the code has been tampered with in order to prove validity of the tests. I suggest you shake hands with the interviewer and call it a day if that happens.
2. Provides Annotation to identify the test methods.
Well, only since version 4. Before that we made do with a naming convention. Either works well so again, arguably not really important.
3. Provides Test runners for running tests.
You mean, it can run tests? Yes OK, that is important I suppose.
4. JUnit tests can be run automatically and they check their own results and provide immediate feedback.
Only from a build environment such as Maven or Jenkins, as can any arbitrary code. This is not a feature of JUnit.
5. JUnit tests can be organized into test suites containing test cases and even other test suites.
Might be useful. To be honest I rarely use this feature nor have I ever seen it used (even in a financial institution I worked in which were extremely hot on testing) so arguably not important.
6. JUnit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.
Err, no it doesn't. Your IDE may be helpful and provide this sort of feedback, running tests from a command line or maven build does not.
Q3 simply asks what a test case is. We should be able to answer this.
Q4. Why does JUnit only report the first failure in a single test?
Their answer:
Reporting multiple failures in a single test is generally a sign that the test does too much and it is too big a unit test. JUnit is designed to work best with a number of small tests. It executes each test within a separate instance of the test class. It reports failure on each test.
You might think this is a good answer until you start actually thinking about what they're really saying here.
Are they saying the only the reason you shouldn't have multiple tests in one is because JUnit wasn't really designed that way?
Surely, it makes no logical sense to carry on running a test that has already failed. What would a subsequent pass mean?
Q5. In Java, assert is a keyword. Won’t this conflict with JUnit’sassert() method?
Well, we're talking about JUnit 4 (annotations have already been mentioned) so this is a trick question. There is an Assert class with assertThat and assertTrue methods. And no mention of the Hamcrest extensions which are more expressive?
Always nice if you come across this in an interview. Another time when I would suggest shaking hands and moving on if it ever happens.
Q6. How do I test things that must be run in a J2EE container (e.g. servlets, EJBs)?
Their answer:
Refactoring J2EE components to delegate functionality to other objects that don’t have to be run in a J2EE container will improve the design and testability of the software. Cactus is an open source JUnit extension that can be used for unit testing server-side java code.
Discussion:
EJBs nowadays are standalone components therefore I think we're only talking about Servlets.
So, how do you test the delegation? Yes, maybe Cactus, maybe HTTP unit but what about integration testing? Soap UI is very good at testing HTTP calls in both Rest and Soap environments.
So again, we have a misleading answer. Not necessarily wrong but if you trot this out in an interview without offering discussion I'd be disappointed.
Q7. What are JUnit classes? List some of them?
This is typical of these Q and A sites. I've never come across a question like this in an interview and hope I never do. It's a sign of a lazy interviewer who's probably reading off a crib sheet.
- Anyway here's their answer:
- JUnit classes are important classes which are used in writing and testing JUnits. Some of the important classes are:
- Assert – A set of assert methods.
- Test Case – It defines the fixture to run multiple tests.
- Test Result – It collects the results of executing a test case.
- Test Suite – It is a Composite of Tests.
RunWith and Test are the two most important annotations. Without these you can neither define nor run a test in a Spring enabled environment.
Q8. Just reiterates we're talking about Junit 4 by asking about the annotations. I'll ignore.
Q9. What Is JunitTestCase?
Their answer:
JUnit Test Case is the base class, junit. framework.TestCase, that allows you to create a test case. (Although, TestCase class is no longer supported in JUnit 4.4.)
So yet again they are asking about out of date APIs. Admittedly this is a nice warning question. If you're asked in an interview about this stuff then don't bother taking the process any further. You'll be dealing with software that's out of date or worse, a refusal to update.
Q10. What is a Junit TestFixture.
Hurrah, huzzah! It might be useful to be able to truck out a decent answer to this question but only maybe.
It was actually a recruitment agent who shared the above link. I wonder if they read the article and didn't really understand it or read the title and thought this would be a good thing for potential candidates?
A little knowledge can be a dangerous thing.