You can use RuleChain.
@Rule
public TestRule chain= RuleChain.outerRule(new HadoopSingleNodeCluster())
.around(new PigExternalResource());
Pergunta
I want to use multiple external resources in my test class, but I have a problem with ordering of external resources.
Here is code snippet :
public class TestPigExternalResource {
// hadoop external resource, this should start first
@Rule
public HadoopSingleNodeCluster cluster = new HadoopSingleNodeCluster();
// pig external resourcem, this should wait until hadoop external resource starts
@Rule
public PigExternalResource pigExternalResource = new PigExternalResource();
...
}
The problem is it tries to start pig before hadoop started, therefore I could not connect local hadoop single node cluster.
Is there any way to order rules of junit?
thanks
Solução
You can use RuleChain.
@Rule
public TestRule chain= RuleChain.outerRule(new HadoopSingleNodeCluster())
.around(new PigExternalResource());
Outras dicas
Why don't you wrap these two ExternalResources
in your own ExternalResource
that calls the before
and after
methods in the order you require from within the new resource's before
and after
methods.
Example:
public class MyResource extends ExternalResource{
private final List<ExternalResource> beforeResources;
private final List<ExternalResource> afterResources;
public MyResource(List<ExternalResource> beforeResources,
List<ExternalResource> beforeResources){
}
public void before(){
for (ExternalResource er : beforeResources)
er.before();
}
public void after(){
for (ExternalResource er : afterResources)
er.after();
}
}
public class TestPigExternalResource {
// hadoop external resource, this should start first
public HadoopSingleNodeCluster cluster = new HadoopSingleNodeCluster();
// pig external resourcem, this should wait until hadoop external resource starts
public PigExternalResource pigExternalResource = new PigExternalResource();
@Rule
public MyResource myResource = new MyResource(
newArrayList(cluster, pigExternalResource),
newArrayList(cluster, pigExternalResource));
...
}
There's a new order attribute in @Rule
and @ClassRule
as of JUnit 4.13-beta-1.
See @Rule
code :
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Rule {
int DEFAULT_ORDER = -1;
/**
* Specifies the order in which rules are applied. The rules with a higher value are inner.
*
* @since 4.13
*/
int order() default DEFAULT_ORDER;
}
See also this PR for reference : PR-1445.