JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

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 {

	@Test
	public void testEvenOddNumber(){
		MyEvenOdd meo = new MyEvenOdd();
		assertEquals("10 is a even number", true, meo.isEvenNumber(10));
	}
}
 Next Program >>

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
Stream and types of Streams
A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:

Byte Streams: Provide a convenient means for handling input and output of bytes. Byte Streams classes are defined by using two abstract classes, namely InputStream and OutputStream.

Character Streams: Provide a convenient means for handling input & output of characters. Character Streams classes are defined by using two abstract classes, namely Reader and Writer.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

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 [email protected].

Most Visited Pages

Other Interesting Sites

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