Git Product home page Git Product logo

Comments (10)

nymanjens avatar nymanjens commented on April 28, 2024

Hi!

Coincidentally, we had a similar feature request internally in google last month. In their use case, they wanted to call a method from an abstract base class:

abstract class TestBase {
  abstract List<String> fooList();
}

class MyTest extends TestBase {
  @TestParameter(provider=MyProvider.class)
  String foo;

  @Override List<String> fooList() { ... }
}

where MyProvider needs to know which subclass is calling so the right fooList() can be used.

Is inheritance also your use case?

Note that it is virtually impossible to rely on anything that is provided by JUnit, such as @Before and @Rule because the test instances can only be constructed AFTER the test parameters are known. Inheritance is an exception because you don't need any of the JUnit stuff to make it work.

The plan for the internal feature request would be to add a Context type to TestParameterValuesProvider.provideValues():

abstract class ContextAwareTestParameterValuesProvider implements TestParameterValuesProvider {
  
  abstract List<?> provideValues(Context context); // new

  @Override public final List<?> provideValues() {
    throw new UnsupportedOperationException("Should never be called");
    // because TestParameterInjector checks for this subtype first
  }

  // Pre-existing method for changing the string representation of the value
  @Override public final TestParameterValue value(@Nullable Object wrappedValue) { ... }
}

  @AutoValue
  static abstract class Context {
    abstract List<Annotation> otherAnnotationsOnType();

    abstract Class<?> testClass();
  }

In the above example, context.testClass() can then be used to create a new instance of it and call fooList().

Would this solution also work for your use case?

from testparameterinjector.

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

Yes, inheritance is in our use case as well. The above solution might work. What's the best way to test it locally?

from testparameterinjector.

nymanjens avatar nymanjens commented on April 28, 2024

What's the best way to test it locally?

If you want to test the above solution, that's not possible yet because it hasn't yet been implemented. But this thread is a good extra reason to not wait too long implementing it.

from testparameterinjector.

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

Thanks, we are migrating all our tests to use TestParameterInjector and this would help us wrap up the last few remaining test suites. Let us know when you would have it ready. Thanks!

from testparameterinjector.

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

Thanks for implementing this @nymanjens. Would you be publishing a new version for us to consume? Thanks!

from testparameterinjector.

nymanjens avatar nymanjens commented on April 28, 2024

Yes, I plan to release a new version shortly

from testparameterinjector.

nymanjens avatar nymanjens commented on April 28, 2024

Yesterday, I released v1.15, which contains this change. It is showing up on the Maven repository now: https://mvnrepository.com/artifact/com.google.testparameterinjector/test-parameter-injector

So you should be able to start using this.

from testparameterinjector.

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

Great, thanks, we'll try it out this week

from testparameterinjector.

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

@nymanjens This solution worked for us, thanks for getting this in!

Based on the javadoc, are you planning to deprecate the previous version of the provider?https://google.github.io/TestParameterInjector/docs/latest/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.html

from testparameterinjector.

nymanjens avatar nymanjens commented on April 28, 2024

Based on the javadoc, are you planning to deprecate the previous version of the provider?

Correct, but since this is an open source project, it'll probably stay around (deprecated) forever / a very long time.

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.