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

TestFeatureManager in JUnit 5 nested class is empty #1135

Open
rrockx-trifork opened this issue Nov 7, 2023 · 1 comment
Open

TestFeatureManager in JUnit 5 nested class is empty #1135

rrockx-trifork opened this issue Nov 7, 2023 · 1 comment

Comments

@rrockx-trifork
Copy link

When using the org.togglz.testing.TestFeatureManager in a org.junit.jupiter.api.Nested test class, the feature manager is empty.

For example:

package example;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import example.TestFeatureManagerTest.SomeFeatures;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.togglz.core.Feature;
import org.togglz.junit5.AllEnabled;

@AllEnabled(SomeFeatures.class)
class TestFeatureManagerTest {

  @Test
  void someTest(org.togglz.testing.TestFeatureManager featureManager) {
    assertThat(featureManager, is(notNullValue()));
  }

  @Nested
  class someNestedClass {

    @Test
    void someTest(org.togglz.testing.TestFeatureManager featureManager) {
      assertThat(featureManager, is(notNullValue()));
    }

  }

  public enum SomeFeatures implements Feature {

  }

}

The method example.TestFeatureManagerTest#someTest succeeds, however example.TestFeatureManagerTest.someNestedClass#someTest fails because featureManager is null.

I would expect that this would work in a nested class as well.

@ThomasvdWerff
Copy link

ThomasvdWerff commented Nov 30, 2023

The general solution to this problem is to use the annotation on ALL the classes that make use of the functionality in their root, including the nested classes. So the code would have to be something like this:

package example;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import example.TestFeatureManagerTest.SomeFeatures;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.togglz.core.Feature;
import org.togglz.junit5.AllEnabled;

@AllEnabled(SomeFeatures.class)
class TestFeatureManagerTest {

  @Test
  void someTest(org.togglz.testing.TestFeatureManager featureManager) {
    assertThat(featureManager, is(notNullValue()));
  }

  @Nested
  @AllEnabled(SomeFeatures.class)
  class someNestedClass {

    @Test
    void someTest(org.togglz.testing.TestFeatureManager featureManager) {
      assertThat(featureManager, is(notNullValue()));
    }

  }

  public enum SomeFeatures implements Feature {

  }

}

Specific solution

And since you only want to use someTest in the nested class, this would be the specific solution for this case (notice how I removed the annotation on the most outer class):

package example;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.togglz.core.Feature;
import org.togglz.junit5.AllEnabled;

class TestFeatureManagerTest {

  // Other nested classes

  @Nested
  @AllEnabled(SomeFeatures.class)
  class someNestedClass {

    @Test
    void someTest(org.togglz.testing.TestFeatureManager featureManager) {
      assertThat(featureManager, is(notNullValue()));
    }

  }

  public enum SomeFeatures implements Feature {

  }

}

General structure

And for clarity here is another example that should fit most cases:

package example;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.togglz.core.Feature;
import org.togglz.junit5.AllEnabled;

// AllEnabled annotation is only needed if you also want to use the TestFeatureManager in the root of this class
class TestFeatureManagerTest {

  // Only test in root of the class and doesn't use the TestFeatureManager
  @Test
  void someTest() {
    assertThat("Check this out, I'm being asserted!", is(notNullValue()));
  }

  @Nested
  @AllEnabled(SomeFeatures.class)
  class someNestedClassOne {

    @Test
    void someNestedTest(org.togglz.testing.TestFeatureManager featureManager) {
      assertThat(featureManager, is(notNullValue()));
    }

  }

  @Nested
  @AllEnabled(SomeFeatures.class)
  class someNestedClassTwo {

    @Test
    void someNestedTest(org.togglz.testing.TestFeatureManager featureManager) {
      assertThat(featureManager, is(notNullValue()));
    }
  }

  @Nested
  class someNestedClassThree {

    // Doesn't use the TestFeatureManager
    @Test
    void someNestedTest() {
      assertThat("Me too!", is(notNullValue()));
    }
  }

  public enum SomeFeatures implements Feature {

  }

}

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

2 participants