Junit Interview Questions: 5. Can You Provide A List of Assertion Methods Supported by Junit 4.4?
Junit Interview Questions: 5. Can You Provide A List of Assertion Methods Supported by Junit 4.4?
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.
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.
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.
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.
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)
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());
}
}
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;
}
}
FAILURES!!!
Tests run: 1, Failures: 1
import org.junit.Assert;
...
Assert.assertEquals(12, message.length());
With a static import statement, assertion method names can be used directly like this:
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.
To run "@Suite.SuiteClasses" class, you can use the core runner: org.junit.runner.JUnitCore.
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
// by FYICenter.com
If you define create "@Suite.SuiteClasses" class as described in the previous question, you run it
with the core runner: org.junit.runner.JUnitCore:
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
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.
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.*;