Java Reference
In-Depth Information
A.5.2
Assumptions
As a developer, you always strive to execute your tests against various scenarios. But
sometimes you have no control over the environment the tests are run in, as in the case
when the test depends on the operating system path-separator character (in Windows
it is \ and in UNIX it is /). This may mandate that your test cases run in Windows boxes
and not in Linux boxes. It would be good if you could make an assumption as to what
the character separator is and execute the tests only if your assumption is correct.
With the 4.4 release of JU nit, assumptions were introduced. assumeThat is the nat-
ural way in JU nit to run a given test against assumptions you make. You can assume
that a given condition is set and then assert that the tests pass. Consider listing A.12.
Listing A.12
Assumptions in JUnit
[...]
import java.io.File;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.hamcrest.CoreMatchers.is;
public class PathSeparatorTest {
@Test
public void pathSeparatorIsOK() {
assumeThat(File.separatorChar, is('/'));
assertThat((new FileManager()).getDBConfFile().getPath(),
is("conf/db.cfg"));
}
}
As you can see, we use the assumeThat method to make sure the file separator is UNIX
style B and then assert that the database configuration file path is correct C . What
happens if the assumption is wrong, and we're running the tests on a Windows box?
Our tests are marked as passing, regardless of the assertions we make!
B
C
A.5.3
New assertions
The assert XXX methods in JU nit 3.x are thorough enough, but they lack any method
to compare equality of arrays. Two methods with these signatures
assertEquals(Object[] expected, Object[] actual)
assertEquals(String message, Object[] expected, Object[] actual)
were added to the JU nit 4.0 framework to help with that task. These methods, given
two arrays, start by checking the sizes of the arrays. If the sizes are the same, they will
call the equals() method on each of the elements.
In version 4.3 these two methods were deprecated in the way they're used, and a
whole new bunch of methods named assertArrayEquals() were introduced.
 
 
 
Search WWH ::




Custom Search