HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

JUnit JUnitCore Example

By Lokesh Gupta | Filed Under: Junit

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

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

7
Leave a Reply

This comment form is under antispam protection
2 Comment threads
5 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
walid

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

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

}

Vote Up0Vote Down  Reply
5 years ago
Jain

i also got the same error. Could you find a solution for this?

Vote Up0Vote Down  Reply
4 years ago
Lokesh Gupta

Could you please give error message or any screen shot of error. That would help.

Vote Up0Vote Down  Reply
4 years ago
Mar

List testCases = new ArrayList();

Vote Up0Vote Down  Reply
1 year ago
krishna jayanth (@urstrulyjayanth)

I Used Junit Core to run specific tests now i need to get a junit report for the same, how can this be achieved? WIll be greats if u can help me

Vote Up0Vote Down  Reply
6 years ago
Lokesh Gupta

Have you tried any tool like ANT or maven to get this report generated. OR, you want any of yours own custom solution?

Vote Up0Vote Down  Reply
6 years ago
Lokesh Gupta

I tried with “mvn surefire-report:report” on a maven project from command prompt and it works like heaven. Produces a very well formatted html report in seconds.

http://maven.apache.org/surefire/maven-surefire-report-plugin/usage.html

Vote Up0Vote Down  Reply
6 years ago

Search Tutorials

JUnit 5 Tutorial

  • JUnit 5 – Introduction
  • JUnit 5 – Test LifeCycle
  • JUnit 5 – @BeforeAll
  • JUnit 5 – @BeforeEach
  • JUnit 5 – @AfterEach
  • JUnit 5 – @AfterAll
  • JUnit 5 – @RepeatedTest
  • JUnit 5 – @Disabled
  • JUnit 5 – @Tag
  • JUnit 5 – Expected Exception
  • JUnit 5 – Assertions Examples
  • JUnit 5 – Assumptions
  • JUnit 5 – Test Suites
  • JUnit 5 – Gradle Dependency
  • JUnit 5 – Maven Dependency
  • JUnit 5 – Execute Test in Eclipse
  • JUnit 5 – Eclipse Test Templates
  • JUnit 5 vs JUnit 4

JUnit 4 Tutorial

  • JUnit – Introduction
  • JUnit – Test Suite
  • JUnit – Execute with JUnitCore
  • JUnit – Execute with Maven
  • JUnit – org.junit.Assume
  • JUnit – Expected Exceptions
  • JUnit – Listeners
  • JUnit – Force Timeout
  • JUnit – Ordered Tests
  • JUnit – Parameterized Tests
  • Junit – @Theory And @DataPoints
  • JUnit – TemporaryFolder @Rule

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz