What is the correct way for unit test areas around exceptions

By looking at our code coverage of our unit tests, we are quite tall. But the last few percent are tricky because many of them catch things like database exceptions that just don't happen under normal circumstances. For example, the code prevents fields that are too long, etc. Therefore, the only possible exceptions to the database are if the database is broken / omitted, or if the layout changes under our feet.

So, the only way for Mock objects so that an exception can be thrown? It seems a little pointless. Perhaps it’s better to just accept not to get 100% code coverage?

Thanks Dan

+6
exception unit-testing
source share
3 answers

Usually, when you run low-level exceptions, such as an IOException or SQLException in Java, I translate them into an exception that extends RuntimeException. I feel that testing this behavior is very important, because otherwise there is a very unpleasant possibility of accidentally swallowing an exception.

Therefore, I recommend testing them if you really do something when a low-level exception is thrown.

Edit: added an example.

public void store(User user) { try { userDao.store(user); } catch (IOException e) { // Logging, perhaps some logic. throw new ServiceException(e); } } @Test(expected = ServiceException.class) public void Store_Fail() { UserDao userDaoMock = createMock(UserDao.class); User user = // Create test user. userDaoMock.store(user); replay(userDaoMock); userService.store(user); verify(userDaoMock); } 

There is not much to check here, but if the logic requires a ServiceException, why not test it?

+1
source share

A common practice when setting a goal of 100% coverage is to cover as much code as possible by testing and cover the remaining few percent by checking the code.

+1
source share
So, the only way for Mock objects so that an exception can be thrown?

I believe that this will be the easiest way, but you can also make a stub (otherwise an object that extends the real object and makes it look like throwing an exception every time). Or you can use AOP, but I think using a library like easymock or jmock would be the easiest way.

It seems a little pointless. Perhaps it's better not to accept 100% code coverage?

Whenever I talk about this topic, I like to shift people's mentality from worrying about a certain percentage of coverage, and instead use percentage as a tool to make you a better developer. In other words, using 100% coverage or 50% coverage does not necessarily mean that your code is well written or even works, but using Code Coverage as a key indicator when you develop code regarding what you are weakening when writing tests, etc .... good idea.

My personal opinion about your question is that if this is the logic that your application makes, then it is worth checking. Therefore, if you catch and exclude and reconfigure a lie from a method, you should pass a test for this. If you catch the exception and end it in another exception, you should check this out. If you catch an exception and do nothing, then it should be the smell of code that needs to be fixed, because it can lead to all kinds of uncontrollable side effects.

As for the fact that 100% did not anger him, I would say that it is not worth it. You must find a good level of comfort for yourself (maybe 80%, maybe 90%) and stick to it. But I would not base it on the types of tests (for example, on testing the exception logic), it should be based only on the general scope and is considered as an indicator that you are not writing your tests when fixing the code.

0
source share

All Articles