JUnit Flashcards
What is JUnit?
JUnit is a unit testing framework for the Java application. It allows you to unit test your code at the method level. You can also use JUnit for test-driven development e.g. first writing test and then writing actual code. Most of the Java IDE like Eclipse, Netbeans and IntelliJ Idea provides out-of-box JUnit integration for TDD. Even build tools like Maven, Ant, and Gradle can run the JUnit test at compile and built time.
What is the difference between JUnit 3.X and JUnit 4.x?
The main difference between JUnit 3 and JUnit 4 is annotation. Earlier you need to start your method name as testXXX() but from JUnit 4.0 onwards you can use @Test annotation, which removes this dependency to start your Junit test methods as a test.
What do @Before and @After annotation does in JUnit 4?
@Before and @After annotation is used for setup and cleanup jobs. They are actually a replacement of the setUp() and tearDown() method of JUnit 3.X. Remember, a method annotated with @Before will be called before calling a test method and @After will be called after executing the test method.
So if you have 5 test methods in one JUnit test class, they will be called five times. You will be glad to know that JUnit 5 has renamed @Before to @After to @BeforeEach and @AfterEach to make it more readable.
What is the difference between @Before and @BeforeClass annotation in JUnit?
@Before is called per test method while @BeforeClass is called per-test-class level. Also, static methods are annotated with @BeforeClass and you can use them to initialize shared resources e.g. which should be shared by every instance of JUnit class like Database connection.
How does JUnit achieve the isolation of tests?
JUnit creates a separate instance of the test class for calling test methods. For example, if your test class contains 5 tets then JUnit will create 5 instances of the test class for executing those test methods. That’s why constructor is called before executing @Test methods. In JUnit 3.X, It used to first create instances and then call the methods but in JUnit 4.1, it first creates an instance and then calls them the method and then goes on to create the second method. This way, it keeps one test isolated from the other.
What is difference between setUp() and tearDown() method?
They are methods from JUnit 3.X which is replaced by @Before and @After annotated method. They are used for setting up resources before the test method and for doing clean-up after-test methods.
How do you check if a test method throws Exception in JUnit?
In JUnit 4, you can use @Test annotation with an expected parameter to specify the exception thrown by a test method. If the test method throws that Exception then JUnit will mark your test passed, otherwise, it will mark them as failed.
For example, if you want to verify your method should throw IllegalArgumentException if the given parameter is bad then you can annotate your test method as @Test(expected = IllegalArgumentException.class). In earlier versions e.g. on JUnit 3.X, you could have used catch block to verify if the method under test throws a particular exception or not.
How do you ignore certain test methods in the JUnit Test class?
Well, from JUnit 4 onwards you can use @Ignore annotation to instruct JUnit runtime to not execute any particular test or disable any test as shown in this JUnit ignore example. You can also use this method for annotating unimplemented test methods. Btw, you should use @Ignore only temporarily and remove it as soon as possible by either implementing the test methods or removing it if not required.
Can you test a Java method for timeout using JUnit?
Yes, JUnit provides a timeout parameter to @Test annotation which can be used to instruct JUnit to pass or fail a test method based upon execution time. For example, a JUnit test method annotated with @Test(timeout= 50) will be failed it doesn’t complete in 50 milliseconds (timeout is specified in millisecond). This is the standard way to verify the SLA of a Java method.
What are some best practices you follow while writing code to make it more testable?
Here are some of the best practices you can follow to make your code more testable :
1) Use interface instead of concrete class, this allows the tester to replace actual class with Mock or Stub for testing purpose.
2) Use Dependency injection, it makes it easy to test individual parts and supply dependency from test configuration. You can create an actual object which is dependent on the Mock and stub for testing. This also allows you to test a class even if its dependency is not yet coded.
3) Avoid static methods, they are difficult to test as they cannot be called polymorhpically.
What does @RunWith annotation do?
The @RunWith annotation allows you to run your JUnit test with another, custom JUnit Runner. For example, you can do some parameterized testing in JUnit by using @RunWith(Parameterized. class), this Runner is required for implementing parameterized tests in JUnit.
Some of the popular third-party implementations of JUnit runners include SpringJUnit4ClassRunner and MockitoJUnitRunner, which you can use with @RunWith annotation to test Spring beans and Mockito library.
how do you test a method which doesn’t return a value?
Well, you can test a method even if it doesn’t return a value by checking for its side effects, for example, add() method of ArrayList doesn’t return anything but it has a side effect of the increasing size of Collection by 1, so you can check whether add() is successful or not by checking the size() of List before and after calling this method as shown below:
What is the difference between Stub and Mock in Unit testing?
Though both Stub and Mock are tools for unit testing and used in place of actual class to facilitate unit testing there is some subtle difference between them. Mocks are generally more powerful becuase they can record method calls and there are also good frameworks like Mockito and PowerMock available, which can create Mock for you.
Stubs are easier to use but require more coding than Mocks becuase you need to create your own Stub implementation. Many Java developers prefer mocks over stubs becuase of this reason only.
What happens when your JUnit test method throws an exception?
When a JUnit method throws an uncaught exception, which is also not declared as expected using the @Test(expected) parameter then the JUnit test will fail, but if it’s declared and expected then the JUnit test will pass. If the method under test throws an exception that is caught by you inside the JUnit test method then nothing will happen and the test will be passed
How do you test protected Java methods using JUnit?
Since protected methods are accessible to everyone inside the same package they are declared, you can put your JUnit test class also on the same package to test them. But, don’t mix your test code with real code, instead, put them on a different source tree. You can use a Maven-like structure to organize your test and source classes in different folders.