Setup:
- java 1.8
- JUnit 4.8.1
Java is not really my thing. But still I'm playing around with JUnit. I wish to write a test case of which he outcome is an exception.
I followed some examples I found with no luck
import biblioteca.exception.InvalidAuthorException;
import biblioteca.util.Validator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Test
*/
public class MyTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testValidator()
{
assertTrue(Validator.isAlphaNumeric("A1"));
assertFalse(Validator.isAlphaNumeric("><"));
assertTrue(Validator.validateLength("vasile", 2, 64));
assertFalse(Validator.validateLength("vasile", 24, 64));
assertTrue(Validator.containsAtLeastOneLetter("1234a"));
assertFalse(Validator.containsAtLeastOneLetter("1234"));
}
@Test
public void testException() {
Validator.validateAuthor("123");
exception.expect(InvalidAuthorException.class);
}
}
This approach throws an error
This approach does not result in the desired outcome (I want the test to pass as I'm expecting the error)
What am I missing?