External resource Junit @Rule

I want to use several external resources in my test class, but I have a problem with ordering external resources.

Here is the 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 that he is trying to start the pigs before he started the haop, so I could not connect the oneoop single node local cluster.

Is there a way to arrange junit rules?

thanks

+6
source share
2 answers

You can use RuleChain .

 @Rule public TestRule chain= RuleChain.outerRule(new HadoopSingleNodeCluster()) .around(new PigExternalResource()); 
+10
source

Why don't you wrap these two ExternalResources in your own ExternalResource , which calls the before and after methods in the order you need, in the methods of the new before and after resource.

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)); ... } 
+1
source

Source: https://habr.com/ru/post/955232/


All Articles