JUnit JUnitCore Example

In any application, which is being built incrementally, often it is desired that we should be able to run only certain tests whenever a new feature is introduced. This can be achieved using JUnitCore class of JUnit framework.

JUnitCore is an inbuilt class in JUnit package and it is based on facade design pattern. JUnitCore class is used to run only specified test classes.

Read More : JUnit 5 Test Suites

1. JUnitCore Example

Suppose, in application release, there are two new features. These features are exposed through two interfaces. Let’s assume interface names are FeatureOne and FeatureTwo.

1.1. Features to be tested

JUnit tests for both features can be as follows:

package com.howtodoinjava.junit;

import junit.framework.Assert;

import org.junit.Test;

public class TestFeatureOne {
	@Test
	public void testFirstFeature()
	{
		Assert.assertTrue(true);
	}
}
package com.howtodoinjava.junit;

import junit.framework.Assert;

import org.junit.Test;

public class TestFeatureTwo {
	@Test
	public void testSecondFeature()
	{
		Assert.assertTrue(true);
	}
}

1.2. Run tests with JUnitCore

To run tests for above features only, we can write a suite like this.

package com.howtodoinjava.junit.suite;

import java.util.ArrayList;
import java.util.List;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

import com.howtodoinjava.junit.TestFeatureOne;
import com.howtodoinjava.junit.TestFeatureTwo;

@SuppressWarnings("rawtypes")
public class WithJUnitCore
{
	public static void main(String[] args)
	{
		List testCases = new ArrayList();

		//Add test cases
		testCases.add(TestFeatureOne.class);
		testCases.add(TestFeatureTwo.class);

		for (Class testCase : testCases)
        {
            runTestCase(testCase);
        }
	}

    private static void runTestCase(Class testCase)
    {
        Result result = JUnitCore.runClasses(testCase);

        for (Failure failure : result.getFailures())
        {
            System.out.println(failure.toString());
        }
    }
}

2. JUnitCore run tests from command prompt

To run test classes from command prompt manually, we can run following command from console. Give the name of all test classes separated by space.

$ java org.junit.runner.JUnitCore TestFeatureOne TestFeatureTwo 

3. JUnitCore run all tests

It is highly advisable to create JUnit suites and execute application wide all test cases. This will require a little work but still it’s best way to execute all tests in JUnit.

@RunWith(Suite.class)
@SuiteClasses({ TestFeatureOne.class, TestFeatureTwo.class })
public class TestFeatureSuite {
	//
}

Happy Learning !!

Reference:

JUnitCore Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

7 thoughts on “JUnit JUnitCore Example”

  1. I got an error here (test Cases) and Compiler in Eclipse doesn’t suggest me anything

    for (Class testCase : testCases)
    {
    runTestCase(testCase);
    }

    }

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.