Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RunListener.testStarted should not be called before a test's @BeforeClass #8768

Open
agrieve opened this issue Jan 24, 2024 · 0 comments
Open

Comments

@agrieve
Copy link

agrieve commented Jan 24, 2024

Description

When using a junit RunListener, it is expected that the order of events for a test with two @Test methods be:

  1. RunListener.testSuiteStarted
  2. Test.beforeClass
  3. RunListener.testStarted
  4. Test.before
  5. Test.after
  6. RunListener.testFinished
  7. RunListener.testStarted
  8. Test.before
  9. Test.after
  10. RunListener.testFinished
  11. Test.afterClass
  12. RunListener.testSuiteFinished

However, with Robolectric (tested with 4.11.1) the first RunListener.testStarted is called before Test.setUp:

  1. RunListener.testSuiteStarted
  2. RunListener.testStarted
  3. Test.beforeClass
  4. Test.before
  5. Test.after
  6. RunListener.testFinished
  7. RunListener.testStarted
  8. Test.before
  9. Test.after
  10. RunListener.testFinished
  11. Test.afterClass
  12. RunListener.testSuiteFinished

Steps to Reproduce

Add a RunListener that logs all methods, and run a test that contains a @BeforeClass that logs as well.

Robolectric & Android Version

Robolectric 4.11.1

Link to a public git repo demonstrating the problem:

A bit hard since my normal environment is a chrome checkout... Here's a paste of the test I used:

import java.io.FileOutputStream;
import java.io.PrintStream;
import org.junit.runner.Description;
import org.junit.runner.notification.RunListener;

// Mark this as doNotAcquire() so the PrintStream will work.
public class ExampleTestListener extends RunListener {
    public static PrintStream PS;
    static {
        try {
            PS = new PrintStream(new FileOutputStream("/tmp/ffoo"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void testSuiteStarted(Description description) throws Exception {
        PS.println("testSuiteStarted " + description);
    }

    @Override
    public void testSuiteFinished(Description description) throws Exception {
        PS.println("testSuiteFinished " + description);
    }

    @Override
    public void testStarted(Description description) throws Exception {
        PS.println("testStarted " + description);
    }

    @Override
    public void testFinished(Description description) throws Exception {
        PS.println("testFinished " + description);
    }
}
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class ExampleTest {
    @BeforeClass
    public static void setUpClass() {
        ExampleTestListener.PS.println("setUpClass");
    }

    @AfterClass
    public static void tearDownClass() {
        ExampleTestListener.PS.println("tearDownClass");
    }

    @Before
    public void setUp() {
        ExampleTestListener.PS.println("setUp");
    }

    @After
    public void tearDown() {
        ExampleTestListener.PS.println("tearDown");
    }

    @Test
    public void test1() {
        ExampleTestListener.PS.println("test1");
    }

    @Test
    public void test2() {
        ExampleTestListener.PS.println("test2");
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant