1. Overview
1.概述
Let’s imagine we have a test for some code that depends on the Operating System and should run only if our test machine is running on Linux. If it’s running on any other OS, we want the test not to fail, but to be ignored at runtime.
让我们想象一下,我们有一个依赖于操作系统的代码测试,只有当我们的测试机器运行在Linux上时才应该运行。如果它运行在任何其他操作系统上,我们希望测试不会失败,而是在运行时被忽略。
A first approach could be using a couple of if statements to check for this condition using System class properties. This works, of course, but JUnit has a cleaner, more elegant method.
第一种方法是使用几个if语句,使用System类属性来检查这个条件。这当然可行,但JUnit有一个更干净、更优雅的方法。
In this short tutorial, we’re going to look at how we can conditionally run or ignore tests in JUnit 4 using the Assume class.
在这个简短的教程中,我们将看看如何在JUnit4中使用Assume类有条件地运行或忽略测试。
2. The Assume Class
2.假设类
This class provides a set of methods to support conditional test execution based on certain conditions. Our test will only run if all these conditions are met. If not, JUnit will just skip its execution and mark it as passed in the test report. The latter is the main difference with the Assert class, in which a failing condition leads the test to end as failing.
该类提供了一组方法来支持基于某些条件的测试执行。我们的测试只有在所有这些条件都满足时才会运行。如果没有,JUnit将直接跳过其执行,并在测试报告中标记为通过。后者是与Assert类的主要区别,其中一个失败的条件导致测试以失败结束。
An important thing to note is that the behavior we described for the Assume class is exclusive to the default JUnit runner. With custom runners, things may be different.
需要注意的一件事是,我们为Assume类描述的行为是默认JUnit运行器所独有的。对于custom runners,情况可能有所不同。
Finally, in the same way as with Assert, we can call the Assume methods either in the @Before or @BeforeClass annotated methods or within the @Test method itself.
最后,与Assert的方式相同,我们可以在@Before或@BeforeClass注释的方法中或在@Test方法本身中调用Assume方法。
Let’s now go through the most useful methods of the Assume class by showing some examples. For all the following examples, let’s assume getOsName() returns Linux.
现在让我们通过展示一些例子来了解Assume类中最有用的方法。对于以下所有的例子,我们假设getOsName()返回Linux.。
2.1. Using assumeThat
2.1.使用assumeThat
The assumeThat() method checks that the state – in this case, getOsName() – satisfies the conditions of the matcher passed in:
assumeThat()方法检查状态–在这种情况下,getOsName()–是否满足传递进来的匹配器的条件。
@Test
public void whenAssumeThatAndOSIsLinux_thenRunTest() {
assumeThat(getOsName(), is("Linux"));
assertEquals("run", "RUN".toLowerCase());
}
In this example, we checked whether getOsName() equals to Linux. As getOsName() returns Linux, the test will be run. Note, we’re using the Hamcrest matcher method is(T) as the matcher here.
在这个例子中,我们检查了getOsName()/em>是否等于Linux。由于getOsName()返回Linux,测试将被运行。注意,我们使用Hamcrest匹配器方法is(T) 作为这里的匹配器。
2.2. Using assumeTrue
2.2.使用assumeTrue
Similarly, we can use the assumeTrue() method to specify a boolean expression that must evaluate to true in order for the test to run. If it evaluates to false, the test will be ignored:
同样,我们可以使用assumeTrue()方法来指定一个布尔表达式,该表达式必须评估为true,以使测试能够运行。如果它的值为false,测试将被忽略。
private boolean isExpectedOS(String osName) {
return "Linux".equals(osName);
}
@Test
public void whenAssumeTrueAndOSIsLinux_thenRunTest() {
assumeTrue(isExpectedOS(getOsName()));
assertEquals("run", "RUN".toLowerCase());
}
In this case, isExpectedOs() returns true. Therefore, the conditions for the test to run have been met, and the test will be run.
在这种情况下,isExpectedOs()返回true。因此,该测试运行的条件已经满足,测试将被运行。
2.3. Using assumeFalse
2.3.使用assumeFalse
Finally, we can use the opposite assumeFalse() method to specify a boolean expression that must evaluate to false in order for the test to run. If it evaluates to true, the test will be ignored:
最后,我们可以使用相反的assumeFalse()方法来指定一个布尔表达式,该表达式必须评估为false,以使测试运行。如果它评估为true,测试将被忽略。
@Test
public void whenAssumeFalseAndOSIsLinux_thenIgnore() {
assumeFalse(isExpectedOS(getOsName()));
assertEquals("run", "RUN".toLowerCase());
}
In this case, as isExpectedOs() also returns true, the conditions for the test to run have not been met, and the test will be ignored.
在这种情况下,由于isExpectedOs() 也返回true,测试运行的条件没有被满足,测试将被忽略。
2.4. Using assumeNotNull
2.4.使用assumeNotNull
When we want to ignore a test if some expression is null, we can use the assumeNotNull() method:
当我们想忽略一个测试,如果某些表达式是null,我们可以使用 assumeNotNull()方法。
@Test
public void whenAssumeNotNullAndNotNullOSVersion_thenRun() {
assumeNotNull(getOsName());
assertEquals("run", "RUN".toLowerCase());
}
As getOsName() is returning a non-null value, the condition for the test to run has been satisfied and the test will run.
由于getOsName()正在返回一个非空值,测试运行的条件已经满足,测试将运行。
2.5. Using assumeNoException
2.5.使用assumeNoException
Finally, we could want to ignore a test if an exception is thrown. We can use assumeNoException() for this purpose:
最后,我们可能想在抛出异常时忽略一个测试。我们可以使用assumeNoException()来达到这个目的。
@Test
public void whenAssumeNoExceptionAndExceptionThrown_thenIgnore() {
assertEquals("everything ok", "EVERYTHING OK".toLowerCase());
String t=null;
try {
t.charAt(0);
} catch(NullPointerException npe){
assumeNoException(npe);
}
assertEquals("run", "RUN".toLowerCase());
}
In this example, as t is null, a NullPointerException exception is thrown, therefore the conditions for the test to run have not been met, and the test will be ignored.
在这个例子中,由于t是空的,一个NullPointerException异常被抛出,因此测试运行的条件没有被满足,测试将被忽略。
3. Where Should We Put the assumeXXX Call?
3.我们应该把assumeXXX调用放在哪里?
It’s important to note that the behavior of the assumeXXX methods depends on where we put them in our tests.
需要注意的是,assumeXXX方法的行为取决于我们在测试中把它们放在哪里。
Let’s slightly modify our assumeThat example so the assertEquals() call goes first. Also, let’s make the assertEquals() fail:
让我们稍微修改一下我们的assumeThat例子,使assertEquals()调用先进行。同时,让我们让 assertEquals()失败。
@Test
public void whenAssumeFalseAndOSIsLinux_thenIgnore() {
assertEquals("run", "RUN");
assumeFalse(isExpectedOS(getOsName()));
}
When we run this example, we’ll have:
当我们运行这个例子时,我们会有。
org.junit.ComparisonFailure:
Expected :run
Actual :RUN
In this case, our test is not ignored because it has failed before we reached the assumeThat() call. The same happens with all of the assumeXXX methods. So, we need to make sure we put them in the right place inside our test method.
在这种情况下,我们的测试不会被忽略,因为在我们到达 assumeThat()调用之前,它已经失败了。所有的assumeXXX方法都会发生同样的情况。因此,我们需要确保我们将它们放在测试方法中的正确位置。
4. Conclusion
4.总结
In this short tutorial, we’ve seen how we can conditionally decide whether or not a test should run, using the Assume class in JUnit 4. In case we are using JUnit 5, it’s also available in version 5.4 or later.
在这个简短的教程中,我们看到了如何使用JUnit 4中的Assume类,有条件地决定是否应该运行一个测试。如果我们使用的是JUnit 5,它在5.4或更高版本中也可用。
As always, the source code of the examples we’ve been through can be found over on GitHub.
一如既往,我们所经历的例子的源代码可以在GitHub上找到。