Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 322591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:57:10+00:00 2026-05-12T08:57:10+00:00

I have the following class in C# which creates an object from a propriatery

  • 0

I have the following class in C# which creates an object from a propriatery .DLL that requires a license in a reachable directory at the initialization .

public CplexServices{
    private Cplex _cplex;
    public Cplex Cplex { get { return _cplex; } }
    public CplexServices()
    {
        try
        {
            _cplex = new Cplex();
        }
        catch
        {
            throw new Exception("Path or license file is wrong");
        }
    }
}

“new Cplex()” might fail if the Windows system path is wrong or the license file is not correct. I want to write a test to assert if the correct exception is thrown when the path and/or license file is wrong.

How can I write this test without changing the path or the license file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:57:11+00:00Added an answer on May 12, 2026 at 8:57 am

    What are you testing? I believe that you are testing that the error handling path for the call to new Cplex() is correct.

    public CplexServices()
    {
        try
        {
            _cplex = new Cplex();
        }
        catch
        {
            throw new Exception("Path or license file is wrong");
        }
    }
    

    You are not testng new Cplex() itself, that it throws exceptions when the licence file is missing rather, you are testing that if it throws exceptions you do the right thing. In this case it’s so trivially correct that I probably would not care too much. However if this were rather more complex, some serious recovery processing to do for example, then we’d much prefer to be able to test this.

    An approach, pass in a factory.

    public CplexServices(CplexFactory myFactory)
    {
        try
        {
            _cplex = myFactory.makeCplex();
        }
        catch
        {
            throw new Exception("Path or license file is wrong");
        }
    }
    

    Now in your tests you can pass a factory which can articficially throw the exception triggering the path you want to test.

    This still leaves the question of how do you might test the Cplex() constructor itself. Arguably this is not your problem, the authors should have their own unit tests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which creates a dynamic object that is assigned to
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
I have a windows service that references an assembly which contains the following class.
In a Web Service context, I have the following class which inherit from the
In Java I have created the following class which extends ImageView to create a
I have the following class which handles my user logged in / logged out
I have the following asynctask class which is not inside the activity. In the
I have the following javascript class which I am trying to pass to an
I have the following tricky problem: I have implemented a (rather complicated) class which
I have a class which must implement the following property public ICollection<IType> Items {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.