JUnit-Java Tutorials by Examples

Software environments:
JDK
Eclipse
JUnit jars or library

Let’s discusses in details about Java, JUnit implementations step by step:

How to crate java project in Eclipse?
Open the Eclipse
File > New > Java Project
Enter the:
Project name: JUnit-TutorialByExample-Java
Select the JRE
JavaSE-1.7
Click on the Next
Click on the Finish.

How to add JUnit library in JUnit-TutorialByExample-Java project?
Right click on the JUnit-TutorialByExample-Java
Build path
Add Librarys
Select JUnit
Click Next
Select JUnit 3 or JUnit4
Click Finish.

junit.jar will added in JUnit-TutorialByExample-Java.



How to add JavaJUnit.java in JUnit-TutorialByExample-Java project?
Right click on the src folder under JUnit-TutorialByExample-Java
Click on the New
Click on Class
Enter the package name: com.tutorialbyexample
Enter the Name: JavaJUnit
Click on the Finish.

JavaJUnit.java
package com.tutorialbyexample;

/****************************************************************/
/* This class is used for just doing the junit implementations */
/* Created by: Vinod Kumar
/* Created on:*/
/***************************************************************/

public class JavaJUnit {

/*
* This method accept String args and return the String.
*/
public String whyJavaJUnit(String sArg) {
return sArg;
}
}

How to add the JUnit Test class JavaJUnitTest.java in JUnit-TutorialByExample-Java project?
Right click on the JavaJUnit.java file under JUnit-TutorialByExample-Java
Click on the New
Click on junit Test Case
Enter the package name: com.tutorialbyexample
Enter the Name: JavaJUnitTest
Select JUnit TestFixtures:
setUpBeforeClass()
tearDownAfterClass()
setUp()
tearDown()

Click on the Finish.

Or

Right click on the src folder under JUnit-TutorialByExample-Java
Click on the New
Click on Class
Enter the package name: com.tutorialbyexample
Enter the Name: JavaJUnitTest
Click on the Finish.

JavaJUnitTest.java

package com.tutorialbyexample;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* @author Vinod Kumar
*
*/
public class JavaJUnitTest {

private JavaJUnit objJavaJUnit;

/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {

}

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
objJavaJUnit = new JavaJUnit();
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
objJavaJUnit = null;
}

/**
* Test method for
* {@link com.tutorialbyexample.JavaJUnit#whyJavaJUnit(java.lang.String)}.
*/
@Test
public void testWhyJavaJUnit() {
assertEquals(objJavaJUnit.whyJavaJUnit("Object Oriented"),
"Object Oriented");
}

/**
* Test method for
* {@link com.tutorialbyexample.JavaJUnit#whyJavaJUnit(java.lang.String)}.
*/
@Test
public void testWhyJavaJUnit_NotEquals() {
assertNotEquals(new Integer("1"), objJavaJUnit.whyJavaJUnit("Object Oriented"));
}

}

How to add JUnit Test Suite in JUnit-TutorialByExample-Java project?
Right click on the JUnit-TutorialByExample-Java
Click on New
Click other
Type JUnit
Select JUnit Test Suit
Click Next
Select Test Methods > JavaJUnitTest
Click Finish.

AllTests.java Test Suit will be created:

package com.tutorialbyexample;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ JavaJUnitTest.class })
public class AllTests {
}

How to run Test classes by Test Suit?
Right click on the AllTests.java
Click on the Run As
Click on the JUnit Test


Project structure:

Done JUnit for Java step by step in http://www.tutorialbyexample.com!!!

References:
http://junit.org/

No comments:

Post a Comment