Git Product home page Git Product logo

Comments (12)

nymanjens avatar nymanjens commented on April 28, 2024 1

I didn't have time this week, but I'll try to do it next week.

from testparameterinjector.

nymanjens avatar nymanjens commented on April 28, 2024

The class under test should have a default constructor, so you could check that.

If there is no obvious error there, we don't explicitly support Powermock. If you think there is a fix that we could do on our side to support Powermock, I'm happy to consider it.

from testparameterinjector.

ArvindJoshi-okta avatar ArvindJoshi-okta commented on April 28, 2024

Constructor present or absent in the class under test doesn't seem to make a difference. The powermock delegation seems to be creating an additional constructor.
I haven't yet looked at a possible solution, but wondering what is the purpose of this check for a single constructor?

https://github.com/google/TestParameterInjector/blob/main/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParametersMethodProcessor.java#L447

from testparameterinjector.

ArvindJoshi-okta avatar ArvindJoshi-okta commented on April 28, 2024

One potential fix would be check for the public constructor and use that to get the annotations?

  private ImmutableList<Optional<TestParametersValues>>
      getConstructorParametersOrSingleAbsentElement(Class<?> testClass) {
    Constructor<?> constructor = getOnlyConstructor(testClass);
    return hasRelevantAnnotation(constructor)
        ? FluentIterable.from(getConstructorParameters(constructor))
            .transform(Optional::of)
            .toList()
        : ImmutableList.of(Optional.absent());
  }

from testparameterinjector.

ArvindJoshi-okta avatar ArvindJoshi-okta commented on April 28, 2024

Here's a fix that I tried locally and works, change is in getOnlyConstructor. Thoughts? I can create a PR if needed.

if (constructors.size() > 1 && testClass.isAnnotationPresent(PowerMockRunnerDelegate.class)) {
      for (Constructor<?> constructor : constructors) {
        if (Modifier.isPublic(constructor.getModifiers())) {
          return getOnlyElement(ImmutableList.of(constructor));
        }
      }
    }

from testparameterinjector.

ArvindJoshi-okta avatar ArvindJoshi-okta commented on April 28, 2024

Upon further investigation, it looks like the issue happens only when PowerMockRunner and EasyMockSupport is used. For example, I was able to repro the issue with the following class in this repo.

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TestParameterInjector.class)
public class PowerMockDelegateTest extends EasyMockSupport {

  public PowerMockDelegateTest() {

  }

  @Test
  public void test(@TestParameter boolean parameter) {
    assertThat(this.getClass().getAnnotation(PowerMockRunnerDelegate.class).annotationType())
            .isEqualTo(PowerMockRunnerDelegate.class);
  }
}

from testparameterinjector.

nymanjens avatar nymanjens commented on April 28, 2024

Looks the test runner itself is using getTestClass().getOnlyConstructor(), which is probably modified by Powermock to do the right thing. But then later, the following custom method is called by TestParameterInjector:

  private static Constructor<?> getOnlyConstructor(Class<?> testClass) {
    ImmutableList<Constructor<?>> constructors =
        ImmutableList.copyOf(testClass.getDeclaredConstructors());
    checkState(
        constructors.size() == 1, "Expected exactly one constructor, but got %s", constructors);
    return getOnlyElement(constructors);
  }

Ideally, we'd use getTestClass().getOnlyConstructor() throughout the codebase, but that's a big refactor. Instead, I'm thinking of being more lenient and filtering out non-public constructors first and let getTestClass().getOnlyConstructor() do the validation.

from testparameterinjector.

nymanjens avatar nymanjens commented on April 28, 2024

Could you check locally if this works?

import static com.google.common.collect.MoreCollectors.onlyElement;

constructors.stream().filter(c -> Modifier.isPublic(c.getModifiers())).collect(onlyElement())

from testparameterinjector.

ArvindJoshi-okta avatar ArvindJoshi-okta commented on April 28, 2024

yes, this works, @nymanjens

from testparameterinjector.

ArvindJoshi-okta avatar ArvindJoshi-okta commented on April 28, 2024

Are you planning to push your change? @nymanjens

from testparameterinjector.

nymanjens avatar nymanjens commented on April 28, 2024

I'll make a release (1.14) for this so you can test the updated code

from testparameterinjector.

ArvindJoshi-okta avatar ArvindJoshi-okta commented on April 28, 2024

great, thank you so much!

from testparameterinjector.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.