@Test public void testSomething() { ... try { // Some code that is expected to // throw OperationNotAllowedException assertFalse(libApp.adminLoggedIn()); libApp.addBook(b); fail("Expected OperationNotAllowedException to be thrown"); } catch (OperationNotAllowedException e) { // Check, e.g., that the error message is correctly set assertEquals(expected, e.getMessage()); } }An alternative version is to use an annotation to indicate that it is expected that the test method throws an exception. The disadvantage is, that is not possible to check if the exception has the correct structure, e.g., has the correct error message.
@Test(expected=OperationNotAllowedException.class) public void testSomething() {...}