A few rules in JUnit

I define two rules defined in one of the test classes, but the strange thing is that only one of them works at a time - the one that was defined last.

@Rule public ExpectedException exception = ExpectedException.none(); @Rule public TemporaryFolder folder= new TemporaryFolder(); 

I cannot understand how much I can define two or more rules and use them separately

+6
source share
1 answer

I had the same problem and I found that in this case you can use RuleChain, for example:

 public TemporaryFolder temp; public ExpectedException thrown; @Rule public TestRule chain = RuleChain.outerRule(temp = new TemporaryFolder()) .around(thrown = ExpectedException.none()); 

In another example, you can see here , and RuleChain javadoc can also help.

+10
source

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


All Articles