Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:22:47+00:00 2026-05-29T17:22:47+00:00

I am trying to find an approach that will allow me to run a

  • 0

I am trying to find an approach that will allow me to run a single test from a JUnit class using only command-line and java.

I can run the whole set of tests from the class using the following:

java -cp .... org.junit.runner.JUnitCore org.package.classname

What I really want to do is something like this:

java -cp .... org.junit.runner.JUnitCore org.package.classname.method

or:

java -cp .... org.junit.runner.JUnitCore org.package.classname#method

I noticed that there might be ways to do this using JUnit annotations, but I would prefer to not modify the source of my test classes by hand (attempting to automate this). I did also see that Maven might have a way to do this, but if possible I would like to avoid depending on Maven.

So I am wondering if there is any way to do this?


Key points I’m looking for:

  • Ability to run a single test from a JUnit test class
  • Command Line (using JUnit)
  • Avoid modifying the test source
  • Avoid using additional tools
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:22:49+00:00Added an answer on May 29, 2026 at 5:22 pm

    You can make a custom, barebones JUnit runner fairly easily. Here’s one that will run a single test method in the form com.package.TestClass#methodName:

    import org.junit.runner.JUnitCore;
    import org.junit.runner.Request;
    import org.junit.runner.Result;
    
    public class SingleJUnitTestRunner {
        public static void main(String... args) throws ClassNotFoundException {
            String[] classAndMethod = args[0].split("#");
            Request request = Request.method(Class.forName(classAndMethod[0]),
                    classAndMethod[1]);
    
            Result result = new JUnitCore().run(request);
            System.exit(result.wasSuccessful() ? 0 : 1);
        }
    }
    

    You can invoke it like this:

    > java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner 
        com.mycompany.product.MyTest#testB
    

    After a quick look in the JUnit source I came to the same conclusion as you that JUnit does not support this natively. This has never been a problem for me since IDEs all have custom JUnit integrations that allow you to run the test method under the cursor, among other actions. I have never run JUnit tests from the command line directly; I have always let either the IDE or build tool (Ant, Maven) take care of it. Especially since the default CLI entry point (JUnitCore) doesn’t produce any result output other than a non-zero exit code on test failure(s).

    NOTE:
    for JUnit version >= 4.9 you need hamcrest library in classpath

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find an example that has css rollover using sprites & sliding door
I am trying to create an app that will help to find nearby places
I'm trying to write an algorithm that will find the set of all vertices
I often find myself questioning whether I'm taking the right approach in trying to
Trying to find a standard. The CmdLet will process data - multiple input, defined
Trying to find a way to remove blank pages from a document I wrote
Trying to find a way to send a POST HTTPS request from Python to
I'm trying to write a tool that will take as input some C code
I'm trying find a way to have PHP to indicate to the browser that
I am trying to find all users with AD that have a birthday in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.