0% found this document useful (0 votes)
6K views

Junit Interview Questions: 5. Can You Provide A List of Assertion Methods Supported by Junit 4.4?

The document discusses various aspects of using JUnit for unit testing in Java, including: - What JUnit is and why it is used for faster and easier unit testing - How to install JUnit by adding the JAR file to the classpath - Requirements for writing JUnit test classes and methods, such as they must be public and return void - Common assertion methods in JUnit and why static imports are used - How to group multiple test classes into a test suite using annotations in JUnit 4.4 - Running the test suite class with JUnitCore - The @SuiteClasses annotation for defining test classes in a suite

Uploaded by

Bindu Sindu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6K views

Junit Interview Questions: 5. Can You Provide A List of Assertion Methods Supported by Junit 4.4?

The document discusses various aspects of using JUnit for unit testing in Java, including: - What JUnit is and why it is used for faster and easier unit testing - How to install JUnit by adding the JAR file to the classpath - Requirements for writing JUnit test classes and methods, such as they must be public and return void - Common assertion methods in JUnit and why static imports are used - How to group multiple test classes into a test suite using annotations in JUnit 4.4 - Running the test suite class with JUnitCore - The @SuiteClasses annotation for defining test classes in a suite

Uploaded by

Bindu Sindu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JUNIT INTERVIEW QUESTIONS

1. What is JUnit?

It is a software testing framework for unit testing and designed to test java applications. JUNIT
features include test runners for running tests, test fixtures for sharing test data, assertions for
testing expected results.

2. Why Do You Use JUnit to Test Your Code?

JUNIT makes unit testing faster and 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 folder . set the class path, and to confirm Junit running successfully run
the command java –cp junit.jar org.junit.runner.junitcore it should display the junit version
successfully.

4. How To Wirte a Simple JUnit Test Class?

I believe that writing 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?

 assertEquals(expected, actual)
 assertEquals(message, expected, actual)
 assertEquals(expected, actual, delta)
 assertEquals(message, expected, actual, delta)
 assertFalse(condition)
 assertFalse(message, condition)
 assertNotNull(object)
 assertNotNull(message, object)
 assertNotSame(expected, actual)
 assertNotSame(message, expected, actual)
 assertNull(object)
 assertNull(message, object)
 assertSame(expected, actual)
 assertSame(message, expected, actual)
 assertTrue(condition)
 assertTrue(message, condition)
 fail()
 fail(message)
 failNotEquals(message, expected, actual)
 failNotSame(message, expected, 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
execution will fail. This is decause JUnit requires that all test methods must be declared
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 testHello() {
String message = "Hello World!";
assertEquals(12, message.length());
}
}

javac -cp junit-4.4.jar HelloTestPrivate.java

java -cp .;junit-4.4.jar org.junit.runner.JUnitCore


HelloTestPrivate
JUnit version 4.4
.E
Time: 0
There was 1 failure:
1) initializationError0(HelloTestPrivate)
java.lang.Exception: Method testHello should be public
at org.junit.internal.runners.MethodValidator.validateTestMethod
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.JUnit4ClassRunner.<init>(JUn
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeC
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Del
at java.lang.reflect.Constructor.newInstance(Constructor.java:51
at org.junit.internal.requests.ClassRequest.buildRunner(ClassReq
at org.junit.internal.requests.ClassRequest.getRunner(ClassReque
at org.junit.internal.requests.ClassesRequest.getRunner(ClassesR
at org.junit.runner.JUnitCore.run(JUnitCore.java:109)
at org.junit.runner.JUnitCore.run(JUnitCore.java:100)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81)
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 JUnit 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 must 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 testHello() {
String message = "Hello World!";
assertEquals(12, message.length());
return message;
}
}

javac -cp junit-4.4.jar HelloTestNonVoid.java

java -cp .;junit-4.4.jar org.junit.runner.JUnitCore


HelloTestNonVoid

JUnit version 4.4


.E
Time: 0
There was 1 failure:
1) initializationError0(HelloTestNonVoid)
java.lang.Exception: Method testHello should be void
at org.junit.internal.runners.MethodValidator.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.NativeConstructorAccessorImpl.newInstance
at sun.reflect.DelegatingConstructorAccessorImpl.newInst
at java.lang.reflect.Constructor.newInstance(Constructor
at org.junit.internal.requests.ClassRequest.buildRunner(
at org.junit.internal.requests.ClassRequest.getRunner(Cl
at org.junit.internal.requests.ClassesRequest.getRunner(
at org.junit.runner.JUnitCore.run(JUnitCore.java:109)
at org.junit.runner.JUnitCore.run(JUnitCore.java:100)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java: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 on 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 is
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.SuiteClasses - 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.JUnitCore.

Here is a good example of "@Suite.SuiteClasses" class:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
// by FYICenter.com

// specify a runner class: Suite.class


@RunWith(Suite.class)

// specify an array of test classes


@Suite.SuiteClasses({
HelloTest.class,
ExpectedExceptionTest1.class,
ExpectedExceptionTest2.class,
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
with 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)
java.lang.AssertionError: Unexpected exception
at org.junit.Assert.fail(Assert.java:74)
at UnexpectedExceptionTest1.testGet(UnexpectedExceptionTest1.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
at org.junit.runner.JUnitCore.run(JUnitCore.java:100)
at org.junit.runner.JUnitCore.runMain(JUnitCore.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.get(ArrayList.java:322)
at UnexpectedExceptionTest2.testGet(UnexpectedExceptionTest2.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
at org.junit.runner.JUnitCore.run(JUnitCore.java:100)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81)
at org.junit.runner.JUnitCore.main(JUnitCore.java:44)

FAILURES!!!
Tests run: 5, Failures: 2

11. What Is the "@SuiteClasses" Annotation?

"@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
previous 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 you 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.

"@SuiteClasses(TestClass1.class, ...)" should be changed to


"@Suite.SuiteClasses({TestClass1.class, ...})".

Someone provided wrong information on build test suite in JUnit 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.*;

You might also like