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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:07:33+00:00 2026-06-02T07:07:33+00:00

Is it possible to have a Junit rule only apply to specific tests? If

  • 0

Is it possible to have a Junit rule only apply to specific tests? If so, how do I do that?

The code below exemplifies what I want to do: each time I have @Rule, I want the method below that to have the specific rule that has been annotated to run with it. I only want that rule to run with the corresponding test. I don’t want anything other tests to be affected by the rule.

In this case, when I run these tests, I see that one of the tests the EmptyFileCheck, gives a File DNE does not exist, but I have used a separate annotation for that function, so I had thought that it would run with a different context, supplying the Empty, but instead DNE is till being used.

import static java.lang.System.in;
import static java.lang.System.setIn;
import static org.junit.Assert.fail;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.nio.channels.Pipe;

import static org.hamcrest.core.AllOf.allOf;

import org.hamcrest.Matcher;
import org.hamcrest.core.AllOf;
import static org.hamcrest.Matchers.*;
import org.hamcrest.text.StringContains;
import org.hamcrest.text.StringEndsWith;
import org.hamcrest.text.StringStartsWith;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.TextFromStandardInputStream;
import org.junit.runner.RunWith;

public class UnitTests {
    private Mockery context = new Mockery() {{
        setImposteriser(ClassImposteriser.INSTANCE);
    }};
    private main mn;
    private InputStream oldIn;
    private PrintStream oldOut;
    private InputStream mockIn;
    private InputStreamReader mockinputStream;
    private PrintStream mockOut;
    private BufferedReader reader;
    private Expectations exp;


    @Before
    public void setMinimalMockingExpectations() throws IOException {
        exp = new Expectations() {{ }};
        mn = context.mock(main.class);
        mockinputStream = context.mock(InputStreamReader.class);
        oldIn = System.in;
        oldOut = System.out;
        mockIn = context.mock(InputStream.class);
        mockOut = context.mock(PrintStream.class);
        System.setOut(mockOut);
    }

    public void configureExpectations(boolean fileOrInput, boolean verbosity) { 
        exp.one(mockOut).println("Do you want to process standard (I)nput, or a (F)ile? I/F");
        if (fileOrInput) { //it's a file
            exp.one(mockOut).println("Enter filename: ");
        } else { //it's not

        }
    }

    @After
    public void reset() {
        System.setOut(oldOut);
    }

    @Rule
    public final TextFromStandardInputStream FileNotFoundException 
        = new TextFromStandardInputStream("F\nDNE\n");
    @Test(expected=FileNotFoundException.class)
    public void EnsureFileCheckExists() throws IOException {
        final String fileName = "DNE";
        configureExpectations(true, false);
        exp.one(mn).checkFile(fileName); 
        context.checking(exp); 
        mn.main(null);
    }

    @Rule
    public final TextFromStandardInputStream FileReadAccessDenied
        = new TextFromStandardInputStream("F\nUnderPriviledged\n");:w

    @Test(expected=FileNotFoundException.class)
    public void FileReadAccessDenied() throws java.io.FileNotFoundException {
        final String fileName = "UnderPriviledged";
        configureExpectations(true, false);
        //exp.oneOf(mn).checkFile(with());  TODO: fix ME!
        context.checking(exp);
        mn.main(null);
    }

    @Rule
    public final TextFromStandardInputStream EmptyFileCheck 
        = new TextFromStandardInputStream("F\nEmpty\n");
    @Test
    public void EmptyFileCheck() throws java.io.FileNotFoundException {
        final String fileName = "Empty";
        configureExpectations(true, false);
        exp.one(mn).checkFile(fileName);
        context.checking(exp);
        mn.main(null);
    }
}
  • 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-02T07:07:35+00:00Added an answer on June 2, 2026 at 7:07 am

    You could have a setter in your Rule which is the first thing that gets called in the rule. Something like this, from ExpectedException:

    // These tests all pass.
    public static class HasExpectedException {
         @Rule
         public ExpectedException thrown= ExpectedException.none();
    
         @Test
         public void throwsNothing() {
             // no exception expected, none thrown: passes.
         }
    
         @Test
         public void throwsNullPointerException() {
             thrown.expect(NullPointerException.class);
             throw new NullPointerException();
         }
    
         @Test
         public void throwsNullPointerExceptionWithMessage() {
             thrown.expect(NullPointerException.class);
             thrown.expectMessage("happened?");
             thrown.expectMessage(startsWith("What"));
             throw new NullPointerException("What happened?");
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have multiple classes that inherit/extends same class in Google AppEngine
In SQL its possible to have fields that cannot contain duplicate data. How is
I have parametrized junit test that reads from several XML input files. In the
I have a JUnit 4 test suite with BeforeClass and AfterClass methods that make
Let's say you have some 3rd-party library class that you want to extend, simply
Is there any way to group tests in JUnit, so that I can run
Is it possible have two projects with the same name in flex builder? Here
is possible to have a separator between elements of a GridView? Thanks
Is it possible to have an association mapping a table to itself? e.g. Table:
Is it possible to have a relationship from a user table to a system

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.