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 9022407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:30:10+00:00 2026-06-16T05:30:10+00:00

Is it possible to run a JUnit @Test method in a class that has

  • 0

Is it possible to run a JUnit @Test method in a class that has a method annotated with @Before, but to ignore the @Before method only for this test?

Edit: I am interested if JUnit supports this functionality, not workarounds. I am aware of workarounds like moving the test(s) in another class or removing the annotation and manually calling setUp() in each test method.

Suppose in a class there are 30 tests, and for 29 of them @Before really simplifies the testing initialization, but for one (or more than one) of them is useless/it complicates things.

public class MyTestClass {

    @Before
    public void setUp() {
        //setup logic
    }

    @Test
    public void test1() {
        //[...]
    }

    @Test
    public void test2() {
        //[...]
    }

    //more tests here

    @Test(ignoreBefore = true, ignoreAfter = true //false by default)
    //something equivalent to this
    public void test20() {
        //[...]
    }

}
  • 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-06-16T05:30:12+00:00Added an answer on June 16, 2026 at 5:30 am

    You can do this with a TestRule. See my answer to Exclude individual test from ‘before’ method in JUnit. Basically, implement ExternalResource, and in the apply method, check if there is a specific annotation on the method, and if there is, don’t run the before/after method. You’ll have to specifically call the before/after from your rule though.

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

Sidebar

Related Questions

with ant it is possible to run JUnit tests and generate test reports in
I am attempting to run a JUnit Test from a Java Class with: JUnitCore
We have a JUnit test suite that we usually run from Eclipse. We have
I have a Groovy unit test class that has many unit tests. In the
Is it possible to run JUnit tests from inside my Java application? Are there
Is it possible to run nose test generators inside custom classes? I am trying
Is it possible to run Tornado such that it listens to a local port
Is it possible to run a specified script before any commit in Pycharm/Intellij Idea?
I apologize if this question was asked already before but I could not find
I'd like to create a generic JUnit 4 test that can be used as

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.