Anda di halaman 1dari 7

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 Junit.jar file to a fo lder . set the class path, and to confirm Junit running successfully run the com mand java cp junit.jar org.junit.runner.junitcore 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 HelloTestPrivate.java import org.junit.Test; import static org.junit.Assert .*; // by FYICenter.com public class HelloTestPrivate { @Test private void testH ello() { String message = "Hello World!"; assertEquals(12, message.length()); } } javac -cp junit-4.4.jar HelloTestPrivate.java java -cp .;junit-4.4.jar org.jun it.runner.JUnitCore HelloTestPrivate JUnit version 4.4 .E Time: 0 There was 1 fa ilure: 1) initializationError0(HelloTestPrivate) java.lang.Exception: Method tes tHello should be public at org.junit.internal.runners.MethodValidator.validateTe stMethod at org.junit.internal.runners.MethodValidator.validateInstanceMe at org .junit.internal.runners.MethodValidator.validateMethodsFor at org.junit.internal .runners.JUnit4ClassRunner.validate(JUnit4C at org.junit.internal.runners.JUnit4 ClassRunner.<init>(JUn at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeC at sun. reflect.DelegatingConstructorAccessorImpl.newInstance(Del at java.lang.reflect.C onstructor.newInstance(Constructor.java:51 at org.junit.internal.requests.ClassR equest.buildRunner(ClassReq at org.junit.internal.requests.ClassRequest.getRunne r(ClassReque at org.junit.internal.requests.ClassesRequest.getRunner(ClassesR at org.junit.runner.JUnitCore.run(JUnitCore.java:109) at org.junit.runner.JUnitCor e.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:8 1) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) 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 HelloTestNonVoid.java import org.junit.Test; import static org.junit.Assert .*; // by FYICenter.com public class HelloTestNonVoid { @Test public String test Hello() { String message = "Hello World!"; assertEquals(12, message.length()); r eturn message; } } javac -cp junit-4.4.jar HelloTestNonVoid.java java -cp .;juni t-4.4.jar org.junit.runner.JUnitCore HelloTestNonVoid JUnit version 4.4 .E Time: 0 There was 1 failure: 1) initializationError0(HelloTestNonVoid) java.lang.Exce ption: Method testHello should be void at org.junit.internal.runners.MethodValid ator.validateTe at org.junit.internal.runners.MethodValidator.validateIn at org. junit.internal.runners.MethodValidator.validateMe at org.junit.internal.runners. JUnit4ClassRunner.validate at org.junit.internal.runners.JUnit4ClassRunner.<init >(J at sun.reflect.NativeConstructorAccessorImpl.newInstance at sun.reflect.Nati veConstructorAccessorImpl.newInstance at sun.reflect.DelegatingConstructorAccess orImpl.newInst at java.lang.reflect.Constructor.newInstance(Constructor at org.j unit.internal.requests.ClassRequest.buildRunner( at org.junit.internal.requests. ClassRequest.getRunner(Cl at org.junit.internal.requests.ClassesRequest.getRunne r( at org.junit.runner.JUnitCore.run(JUnitCore.java:109) at org.junit.runner.JUn itCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.j ava:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) FAILURES!!! Tests run: 1, Failures: 1 8. Why Does Poeple Import org.junit.Assert Statically?

Poeple use the static import statement on org.junit.Assert 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 org.junit.Assert; ... Assert.assertEquals(12, message.length()); With a static import statement, assertion method names can be used directly like this: import static org.junit.Assert.*; ... assertEquals(12, message.length()); 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 org.junit.runners.Suite class and two annotations to help you to build test suite. org.junit.runners.Suite - JUnit 4.4 runner class that runs a group of test classes. org.junit.runner.RunWith - JUnit 4.4 class annotation that specify runner class to run the annotated class. org.junit.runner.Suite.Sui teClasses - JUnit 4.4 class annotation that specify an array of test classes for the Suite.class to run. The annotated class should be an empty class. To run "@ Suite.SuiteClasses" class, you can use the core runner: org.junit.runner.JUnitCo re. Here is a good example of "@Suite.SuiteClasses" class: import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.juni t.runners.Suite.SuiteClasses; // by FYICenter.com // specify a runner class: Sui te.class @RunWith(Suite.class) // specify an array of test classes @Suite.SuiteC lasses({ HelloTest.class, ExpectedExceptionTest1.class, ExpectedExceptionTest2.c lass, UnexpectedExceptionTest1.class, UnexpectedExceptionTest2.class} )

// the actual class is empty public class AllTests { } 10. How To Run a "@Suite.SuiteClasses" Class in JUnit 4.4? If you define create "@Suite.SuiteClasses" class as described in the previous question, you run it wi th the core runner: org.junit.runner.JUnitCore: java -cp .;junit-4.4.jar org.junit.runner.JUnitCore AllTests JUnit version 4.4 . ...E.E Time: 0.016 There were 2 failures: 1) testGet(UnexpectedExceptionTest1) j ava.lang.AssertionError: Unexpected exception at org.junit.Assert.fail(Assert.ja va:74) at UnexpectedExceptionTest1.testGet(UnexpectedExceptionTest1.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ... at org.junit.run ner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUni tCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) 2) testGet( UnexpectedExceptionTest2) java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.ge t(ArrayList.java:322) at UnexpectedExceptionTest2.testGet(UnexpectedExceptionTes t2.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ... a t org.junit.runner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCo re.runMain(JUnitCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java: 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 org.junit.runners. Suite.SuiteClasses. 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 org.junit.runners.Suite class (Suite.html). 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(Suite.class) and @SuiteClasses(TestClass1.class, ...). When you run this class, it will run all the tests in all the suite classes. "@SuiteC lasses(TestClass1.class, ...)" should be changed to "@Suite.SuiteClasses({TestCl ass1.class, ...})". 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 : org.junit.runner.TextListener.run(TestClass1.class, ...); 12. 13. Import jave.org.junit.*;

Anda mungkin juga menyukai