0% found this document useful (0 votes)
32 views7 pages

JUnit Interview Questions for Java Devs

1. JUnit is a testing framework for unit testing Java applications. It features test runners, test fixtures, and assertions. 2. JUnit makes unit testing faster and easier. Tests should be done at the unit level to improve productivity. 3. To use JUnit, copy the JUnit JAR file to your classpath and run tests with the JUnit runner.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

JUnit Interview Questions for Java Devs

1. JUnit is a testing framework for unit testing Java applications. It features test runners, test fixtures, and assertions. 2. JUnit makes unit testing faster and easier. Tests should be done at the unit level to improve productivity. 3. To use JUnit, copy the JUnit JAR file to your classpath and run tests with the JUnit runner.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

JUNIT INTERVIEW QUESTIONS 1. What is JUnit?

It is a software testing framework for unit testing and design ed to test java applications. JUNIT features include test runners for running te sts, test fixtures for sharing test data, assertions for testing expected result s. 2. Why Do You Use JUnit to Test Your Code? JUNIT makes unit testing faster an d easier. I believe that most of the tests should be done at unit level to make it more productive. 3. How Do You Install JUnit? Copy the [Link] file to a fo lder . set the class path, and to confirm Junit running successfully run the com mand java cp [Link] [Link] it should display the junit vers ion successfully. 4. How To Wirte a Simple JUnit Test Class? I believe that writ ing more tests will make me more productive not less productive. I believe that tests should be done as soon as possible at the code unit level. I believe that tests should be done as soon as possible at the code unit level. 5. Can You Provide a List of Assertion Methods Supported by JUnit 4.4? y y y y y y y y y y y y y y y y y assertEquals(expected, actual) assertEquals(message, ex pected, actual) assertEquals(expected, actual, delta) assertEquals(message, expe cted, actual, delta) assertFalse(condition) assertFalse(message, condition) asse rtNotNull(object) assertNotNull(message, object) assertNotSame(expected, actual) assertNotSame(message, expected, actual) assertNull(object) assertNull(message, object) assertSame(expected, actual) assertSame(message, expected, actual) asse rtTrue(condition) assertTrue(message, condition) fail()

y y y y fail(message) failNotEquals(message, expected, actual) failNotSame(message, expe cted, actual) failSame(message) 6. What Happens If a JUnit Test Method Is Declared as "private"? If a JUnit test method is declared as "private", the compilation will pass ok. But the executio n will fail. This is decause JUnit requires that all test methods must be declar ed as "public". For example: type [Link] import [Link]; import static [Link] .*; // by [Link] public class HelloTestPrivate { @Test private void testH ello() { String message = "Hello World!"; assertEquals(12, [Link]()); } } javac -cp [Link] [Link] java -cp .;[Link] [Link] [Link] HelloTestPrivate JUnit version 4.4 .E Time: 0 There was 1 fa ilure: 1) initializationError0(HelloTestPrivate) [Link]: Method tes tHello should be public at [Link] stMethod at [Link] at org .[Link] at [Link] .[Link](JUnit4C at [Link].JUnit4 ClassRunner.<init>(JUn at [Link].newInstance0 (Native at [Link](NativeC at sun. [Link](Del at [Link].C [Link]([Link] at [Link] [Link](ClassReq at [Link] r(ClassReque at [Link](ClassesR at [Link]([Link]) at [Link] [Link]([Link]) at [Link]([Link] 1) at [Link]([Link]) FAILURES!!! Tests run: 1, Failures: 1

7. What Happens If a JUnit Test Method Is Declared to Return "String"? If a JUni t test method is declared to return "String", the compilation will pass ok. But the execution will fail. This is decause JUnit requires that all test methods mu st be declared to return "void". For example: type [Link] import [Link]; import static [Link] .*; // by [Link] public class HelloTestNonVoid { @Test public String test Hello() { String message = "Hello World!"; assertEquals(12, [Link]()); r eturn message; } } javac -cp [Link] [Link] java -cp .;juni [Link] [Link] HelloTestNonVoid JUnit version 4.4 .E Time: 0 There was 1 failure: 1) initializationError0(HelloTestNonVoid) [Link] ption: Method testHello should be void at [Link] [Link] at [Link] at org. [Link] at [Link]. [Link] at [Link].JUnit4ClassRunner.<init >(J at [Link] at [Link] [Link] at [Link] [Link] at [Link](Constructor at org.j [Link]( at [Link]. [Link](Cl at [Link] r( at [Link]([Link]) at [Link] [Link]([Link]) at [Link](JUnitCore.j ava:81) at [Link]([Link]) FAILURES!!! Tests run: 1, Failures: 1 8. Why Does Poeple Import [Link] Statically?

Poeple use the static import statement on [Link] to save coding time o n calling its assetion methods. With a normal import statement, assertion method names must qualified with the class name like this: import [Link]; ... [Link](12, [Link]()); With a static import statement, assertion method names can be used directly like this: import static [Link].*; ... assertEquals(12, [Link]()); 9. How To Group Multiple Test Classes into a Suite in JUnit 4.4? JUnit 4.4 stops using the "public static Test suite()" method to build a test suite class. It i s now provides the [Link] class and two annotations to help you to build test suite. [Link] - JUnit 4.4 runner class that runs a group of test classes. [Link] - JUnit 4.4 class annotation that specify runner class to run the annotated class. [Link] teClasses - JUnit 4.4 class annotation that specify an array of test classes for the [Link] to run. The annotated class should be an empty class. To run "@ [Link]" class, you can use the core runner: [Link] re. Here is a good example of "@[Link]" class: import [Link]; import [Link]; import [Link] [Link]; // by [Link] // specify a runner class: Sui [Link] @RunWith([Link]) // specify an array of test classes @[Link] lasses({ [Link], [Link], ExpectedExceptionTest2.c lass, [Link], [Link]} )

// the actual class is empty public class AllTests { } 10. How To Run a "@[Link]" Class in JUnit 4.4? If you define create "@[Link]" class as described in the previous question, you run it wi th the core runner: [Link]: java -cp .;[Link] [Link] AllTests JUnit version 4.4 . ...E.E Time: 0.016 There were 2 failures: 1) testGet(UnexpectedExceptionTest1) j [Link]: Unexpected exception at [Link]([Link] va:74) at [Link]([Link]) at [Link].invoke0(Native Method) ... at [Link] [Link]([Link]) at [Link](JUni [Link]) at [Link]([Link]) 2) testGet( UnexpectedExceptionTest2) [Link]: Index: 1, Size: 0 at [Link]([Link]) at [Link] t([Link]) at [Link](UnexpectedExceptionTes [Link]) at [Link].invoke0(Native Method) ... a t [Link]([Link]) at [Link] [Link]([Link]) at [Link]([Link]: 44) FAILURES!!! Tests run: 5, Failures: 2 11. What Is the "@SuiteClasses" Annota tion? "@SuiteClasses" is a class annotation defined in JUnit 4.4 in [Link]. [Link]. It allows you to define a suite class as described in the pr evious question. By the way, the API document of JUnit 4.4 has a major typo for the [Link] class ([Link]). Using Suite as a runner allows y ou to manually build a suite containing tests from many classes. It is the JUnit 4 equivalent of the JUnit 3.8.x static Test suite() method. To use it, annotate a

class with @RunWith([Link]) and @SuiteClasses([Link], ...). When you run this class, it will run all the tests in all the suite classes. "@SuiteC lasses([Link], ...)" should be changed to "@[Link]({TestCl [Link], ...})". Someone provided wrong information on build test suite in JU nit 4.4. Do not follow this: JUnit provides tools to define the suite to be run and to display its results. To run tests and see the results on the console, run : [Link]([Link], ...); 12. 13. Import [Link].*;

You might also like