I have a Java method that returns an Optional. I\'d like to write an easy-to-read unit test for it that asserts that
the returned Optional has a value (i.e.
I use Hamcrest Optional for that:
import static com.github.npathai.hamcrestopt.OptionalMatchers.hasValue;
import org.junit.Test;
public class MyUnitTests {
@Test
public void testThatOptionalHasValue(){
String expectedValue = "actual value";
assertThat(testedMethod(), hasValue(expectedValue));
}
}
You can add Hamcrest Optional to your dependencies by including it in your build.gradle
:
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'com.github.npathai:hamcrest-optional:1.0'
}