Comments (10)
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.
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.
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.
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.
Thanks for implementing this @nymanjens. Would you be publishing a new version for us to consume? Thanks!
from testparameterinjector.
Yes, I plan to release a new version shortly
from testparameterinjector.
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.
Great, thanks, we'll try it out this week
from testparameterinjector.
@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.
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)
- Run a single test in IntelliJ HOT 12
- Consider keeping a changelog? HOT 1
- Can't use more than one `TestParameter` annotation with Android Instrumentation tests. HOT 8
- Build fails with firebase dependency HOT 3
- Support JUnit 5 HOT 27
- Support non-number floating point values HOT 2
- Support tests written in kotlin with parameter types that are `@JvmInline` value classes HOT 4
- Wrong transitive dependency protobuf HOT 3
- Feature request: shade protobuf dependency HOT 6
- Test naming changed from 1.8 to 1.9 for `float`s HOT 6
- JUnit 5: Support `@Nested` classes HOT 5
- Run in Firebase Test Lab HOT 2
- Feature request: Expose `PluggableTestRunner` HOT 2
- Trying to run the test but nothing happened
- How to be inject parameter into the test class constructor HOT 2
- How to set custom name for `@TestParameter` HOT 2
- Running a single test using maven verify HOT 2
- Does this work with Powermock libraries? HOT 12
- test execution time is not accurate HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testparameterinjector.