JUnit - Using timeout and expected elements of @Test

[Updated: May 4, 2017, Created: May 4, 2017]

@Test has two optional elements: expected and timeout. In this quick examples we will see their purpose and how to use them.

Using 'expected' element

This element declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one we have specified, the test fails.

Following test fails because it's throwing ArithmeticException and we are not using 'expected' element.

public class MyTest {

  @Test
  public void testMethod1() {
      int i = 1 / 0;
      System.out.println(i);
  }
    .............
}
mvn -q test -Dtest=MyTest#testMethod1

Output

D:\example-projects\junit\test-annotation-elements>mvn -q test -Dtest=MyTest#testMethod1

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.037 sec <<< FAILURE!
testMethod1(com.logicbig.example.MyTest) Time elapsed: 0.004 sec <<< ERROR!
java.lang.ArithmeticException: / by zero
at com.logicbig.example.MyTest.testMethod1(MyTest.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:242)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:137)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error:
testMethod1(com.logicbig.example.MyTest): / by zero

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project test-annotation-elements: There are test failures.
[ERROR]
[ERROR] Please refer to D:\example-projects\junit\test-annotation-elements\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Following test passes because it's throwing ArithmeticException and we are using 'expected' with the same exception class value.

public class MyTest {
    .............
  @Test(expected = ArithmeticException.class)
  public void testMethod2() {
      int i = 1 / 0;
      System.out.println(i);
  }
    .............
}
mvn -q test -Dtest=MyTest#testMethod2

Output

D:\example-projects\junit\test-annotation-elements>mvn -q test -Dtest=MyTest#testMethod2

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Following test fails because it's not throwing the exception which we have specified with 'expected' element.

public class MyTest {
    .............
  @Test(expected = ArithmeticException.class)
  public void testMethod3() {
      System.out.println("running testMethod3");
  }
}
mvn -q test -Dtest=MyTest#testMethod3

Output

D:\example-projects\junit\test-annotation-elements>mvn -q test -Dtest=MyTest#testMethod3

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyTest
running testMethod3
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec <<< FAILURE!
testMethod3(com.logicbig.example.MyTest) Time elapsed: 0.005 sec <<< FAILURE!
java.lang.AssertionError: Expected exception: java.lang.ArithmeticException
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:242)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:137)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Failed tests: testMethod3(com.logicbig.example.MyTest): Expected exception: java.lang.ArithmeticException

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project test-annotation-elements: There are test failures.
[ERROR]
[ERROR] Please refer to D:\example-projects\junit\test-annotation-elements\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Using 'timeout' element

This causes a test to fail if it takes longer than a specified amount of milliseconds time.

public class MyTest2 {

  @Test(timeout = 200)
  public void testMethod() {
      boolean b = true;
      if(b){
          try {
              TimeUnit.MILLISECONDS.sleep(300);
          } catch (InterruptedException e) {
              System.out.println(e.getMessage());
          }
      }
  }
}
mvn -q test -Dtest=MyTest2#testMethod

Output

D:\example-projects\junit\test-annotation-elements>mvn -q test -Dtest=MyTest2#testMethod

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyTest2
sleep interrupted
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.236 sec <<< FAILURE!
testMethod(com.logicbig.example.MyTest2) Time elapsed: 0.207 sec <<< ERROR!
org.junit.runners.model.TestTimedOutException: test timed out after 200 milliseconds
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at com.logicbig.example.MyTest2.testMethod(MyTest2.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:745)


Results :

Tests in error:
testMethod(com.logicbig.example.MyTest2): test timed out after 200 milliseconds

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project test-annotation-elements: There are test failures.
[ERROR]
[ERROR] Please refer to D:\example-projects\junit\test-annotation-elements\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Example Project

Dependencies and Technologies Used :

  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck..
  • JDK 1.8
  • Maven 3.3.9

Using Test Annotation Elements Select All Download
  • test-annotation-elements
    • src
      • main
      • test
        • java
          • com
            • logicbig
              • example

See Also