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

Introduce support for class-scoped parameters #1006

Closed
1 task
kcooney opened this issue Aug 5, 2017 · 4 comments
Closed
1 task

Introduce support for class-scoped parameters #1006

kcooney opened this issue Aug 5, 2017 · 4 comments

Comments

@kcooney
Copy link
Member

kcooney commented Aug 5, 2017

Overview

Provide a way to have values injected into a class that are created once before any test methods are created.

If your test class has constructor with a parameter annotated with @PerClass, then JUnit Jupiter will create it once per run of the test class, using the default constructor of the class (in Kotlin, the primary constructor). That class can have non-static methods annotated with @BeforeAll, @AfterAll, @BeforeEach, or @AfterEach.

Here's an example of a class that creates a local database:

    @PerClass
    public class Database {
        @BeforeAll void createDatabase() {
        }
 
        @AfterAll void dropDatabase() {
        }

        @BeforeEach void clearDatabase() {
        }

        public void insert(long id, Account) }{
       }
    }

    class DatabaseTestCase {
      private final Database db;

      DatabaseTestCase(@PerClass Database db) {
        this.db = db;
      }

      @Test
      public void insertAccount() {
        Account account = ...
        db.insert(1, account);
        ...
      }
    }

You could think of this as a better version of JUnit4's ExternalResource rule.

This could also be used to support @BeforeAll in Kotlin tests

    class SimpleKotlinTestCase constructor(@PerClass db: Database) {
      class Database {
        @BeforeAll fun createDatabase() {
        }
 
        @AfterAll fun dropDatabase() {
        }

        @BeforeEach fun clearDatabase() {
        }

        fun insert(id: Int, account: Account) {
        }
      }

      @Test
      fun insertAccount() {
        val account = ...
        db.insert(1, account);
        ...
      }
    }

This could also be used to support integration tests.

Deliverables

  • ?
@sbrannen sbrannen changed the title Allow class-scope parameters to be passed via constructors Introduce support for class-scoped parameters Aug 5, 2017
@sbrannen
Copy link
Member

sbrannen commented Aug 5, 2017

Updated title and description.

@sbrannen
Copy link
Member

sbrannen commented Aug 5, 2017

FYI: developers and third parties can already achieve comparable functionality via a custom extension that implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, and ParameterResolver.

With that in mind, I'm not convinced that JUnit Jupiter should implement such a feature itself.

@stale
Copy link

stale bot commented May 13, 2021

This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. Thank you for your contribution.

@stale stale bot added the status: stale label May 13, 2021
@stale
Copy link

stale bot commented Jun 3, 2021

This issue has been automatically closed due to inactivity. If you have a good use case for this feature, please feel free to reopen the issue.

@stale stale bot closed this as completed Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants