Publish Your Article Here

Program: Simple JUnit test using @Test annotation.

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. We are using Junit 4 for examples shown here. Here is a simple example. MyEvenOdd class provides a method isEvenNumber() to test whether the given number is even or odd number. We have written MyEvenOddTest class to test the functionality of MyEvenOdd class.

@org.junit.Test annotation: The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

assertEquals(String message, Object expected, Object actual): Asserts that two objects are equal. If they are not, an AssertionError is thrown with the given message. If expected and actual are null, they are considered equal.

In Eclipse, run MyEvenOddTest class as JUnit Test.

package com.java2novice.junit.samples;

public class MyEvenOdd {

	public boolean isEvenNumber(int number){
		boolean result = false;
		if(number%2 == 0){
			result = true;
		return result;

package com.java2novice.junit.tests;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.java2novice.junit.samples.MyEvenOdd;

public class MyEvenOddTest {

	public void testEvenOddNumber(){
		MyEvenOdd meo = new MyEvenOdd();
		assertEquals("10 is a even number", true, meo.isEvenNumber(10));
 Next Program >>
blog comments powered by Disqus

Java JUnit Examples

  1. Simple JUnit test using @Test annotation.
  2. List of JUnit annotations.
  3. Assertion method Assert.assertArrayEquals() example.
  4. How to do JUnit test for comapring two list of user defined objects?
  5. Assertion method Assert.assertEquals() example.
  6. Assertion method Assert.assertFalse() example.
  7. Assertion method Assert.assertTrue() example.
  8. Assertion method Assert.assertNotNull() example.
  9. Assertion method Assert.assertNull() example.
  10. Assertion method Assert.assertNotSame() example.
  11. Assertion method Assert.assertSame() example.
Knowledge Centre
Difference between while and do-while
The while statement verifies the condition before entering into the loop to see whether the next loop iteration should occur or not. The do-while statement executes the first iteration without checking the condition, it verifies the condition after finishing each iteration. The do-while statement will always execute the body of a loop at least once.
Famous Quotations
We know what we are, but know not what we may be.
-- William Shakespeare

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to or you can comment on the page.

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.