开发者

Unit testing exception handling for third party exceptions with internal constructors

开发者 https://www.devze.com 2023-02-21 17:25 出处:网络
We use a third party library to communicate with their server software, which we can’t modify.Calls to the library can fail, by throwing exceptions similar to the following:

We use a third party library to communicate with their server software, which we can’t modify. Calls to the library can fail, by throwing exceptions similar to the following:

class LibraryException : Exception {
    internal LibraryException() {};
    string ExceptionDetails {public get; internal set; }
}

Depending on the contents of ‘ExceptionDetails’, our calling code has to perform different actions (some errors are fatal, others are not), and I would like to test this code. The problem I have is that because ‘LibraryException’ has an internal constructor, I can’t find a way of creating one. I’ve tried new’ing one up, deriving a child class and using Activator.CreateInstance. Is there any way of working around this?

I’ve thought about changing the build, so that for a subset of tests it开发者_开发技巧 would link in a different binary, but this seems like it would be a maintenance nightmare, so at the moment the area is only covered by integration tests.

Any suggestions?


it should work with Activator.CreateInstance when using the overload that accepts BindingFlags:

Activator.CreateInstance(typeof(LibraryException),
    BindingFlags.NonPublic | BindingFlags.Instance,
    null,
    null,
    null);
0

精彩评论

暂无评论...
验证码 换一张
取 消