mercredi 7 février 2018

Exception test for service layer

I have a class that looks like this

@Service
@Transactional
public class BookServiceImpl implements BookService {

 @Autowired
    private BookRepository bookRepository;

  public void removeOne(Long id) throws DataAccessException {
        bookRepository.delete(id);
    }
}

My exception tests looks like this

@Rule
    public final ExpectedException exception = ExpectedException.none();

    @Test
    public void removeOneThrowsNullPointerException() {
        BookService foo = new BookServiceImpl();

        exception.expect(NullPointerException.class);
        foo.removeOne(1L);
    }

Now this tests passes but first why must the BookService class be instantiated and normally this test would pass if was a simple unit test because we have a book with id 1L but now its a null , I have read articles on @Rule , what is actually going on ? and is this a proper test for finding exception in the method removeOne() ?

Aucun commentaire:

Enregistrer un commentaire