AX, temporary table, Unit Test and DuplicateKeyException

Why do I link AX, temporary table, unit test and DuplicateKeyException? Because if I put them together, it behaves quite strangely.

I have a temporary table, I insert some data into it and in an exceptional case, when a duplicity occurs, I catch the DuplicateKeyException and simply ignore such insert. And it works correctly.

The problem arises if I want to test this case by a unit test. In that moment, AX doesn’t throw DuplicateKeyException but Info (Exception::Info) instead – which I naturally don’t handle and the test ends with an unexpected and unhandled exception. Why does it happen? I have no idea, but I’m going to ask Microsoft.

Sure, that’s not a problem to bypass it, e.g. by doing a select before trying to insert data or to give up the test – but no one pleases me too much.

I tried it in AX2009 as well as in AX2012 (it’s valid for InMemory only, not for TempDB) with the same result. If you are interested in further details, you can download a demonstration code.