Junit
Junit
Testing is the process of checking the functionality of the application whether it fulfills the requirement or
not.
2) What is JUnit?
JUnit is the testing framework, it is used for unit testing of Java code.
1. JUnit = Java + Unit Testing
12) If the JUnit method's return type is 'string', what will happen?
JUnit test methods are designed to return 'void'. So the execution will fail.
1. 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.
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.
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.
8. 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.
10. 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 purposes.
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.
@Test
public void testListAdd() {
List<String> mylist = new ArrayList<>();
assertEquals(0, mylist.size());
collection.add("Java");
assertEquals(1, mylist.size());
collection.add("JUnit");
assertEquals(2, mylist.size());
}
If a method doesn't return any value and doesn't have any side effects which means it's not doing anything
and doesn't require testing.
10. 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.
14. 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
17. What are some Unit testing libraries you know other than JUnit for Java programs?
Java is very lucky to have many great unit testing libraries including JUnit. Here are a couple of the most
popular ones:
At a bare minimum, you should JUnit and Mockito for writing unit tests in Java.
18. Does JUnit support Parameterized tests, where tests can be executed multiple times with
different parameter values?
Yes, JUnit supports parameterized tests by using a custom Runner called Parameterized
and @Parameters annotation. You use this annotation to annotate the method which provides parametric
values and run the Test class with @RunWith(Parameterized. class) runner.