Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test method that uses external package to interact with DB
    text
    copied!<p>I am trying to test a function, which is part of the business logic layer of a struts application. I am facing problem because the code is dependent on external functions in a organization wide used jar. </p> <pre><code>public CustomObject getCustomObject(String id){ CustomObject customObject = new CustomObject(); QueryObject sql = createSqlStatement(id); // EXTERNAL jar Result result = execute(sql); // EXTERNAL jar ArrayList list = result.getResulList(); // EXTERNAL jar // Logic to use the list object to fill the customObject // I can see an error here, that could have been // caught in unit test return customObject ; } </code></pre> <p>Now the problem is testing the logic of filling up the object. <br> The Junit4 test that I have written is:</p> <pre><code> @Test public void testCustomObject() { CustomObject customObjectwActual = new CustomObject(); CustomObject customObjectExpected = new CustomObject(); // set properties of customObjectExpected here customObjectwActual = getCustomObject(id); // Exception here assertEquals(customObjectExpected , customObjectwActual ); } </code></pre> <p>The exception is thrown because "the external jar classes are loaded at the time of startup of struts application" as per the developers explanation. I am new to Java and struts. Is my approach wrong? Is there a way to "load" these external jar classes in the <code>setUpBeforeClass()</code> in someway? <br> Please let me know if anything is unclear.</p> <p>EDIT 2: Sorry, my question is unclear. I have these external jars in my classpath. It compiles fine, and it actually loads the external jar's classes. The SQL queries are stored in an xml file. These external jars have their own xml file with SQL statements as well. It is failing to load one of these two xml files. <br> Further, even if they load correctly, I actually dont want to call the database. Is there some way to mock these calls?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload