Unit testing best practices

3 posts / 0 new
Last post
Last seen: 2 years 4 months ago
Joined: 02/06/2020
Posts: 4
Unit testing best practices

(Using traditional C++ API)

We have some user created classes acting as an abstraction layer on top of DDS Connext. There are corresponding unit tests for these objects in which the goal is to make sure they are working properly.

While testing those user created classes, essentially the test cases create a single participant with subscriber/reader and publisher/writer pairs. Using an arbitrary test message and topic, the DataReader is created with a corresponding listener. A message is simply sent through the writer/publisher on this same participant and the test checks if the custom classes are working or not by whether or not the listener ballback was triggered. The transport used is the default shared memory & UPDv4. The reader and writer QoS is set to reliable reliability, transient local durability, keep all history, and resource limits for this sample max 32.

Is testing in this manner a bad idea? The DDS actions between the reader and writer happen asynchronously, so testing in this way seems to lead to timing issues, where we end up having to wait with timeouts for 1) the subscription between the local reader and writer to be matched and 2) the message to be received. Is there a more deterministic way of testing? One idea that came to mind was using a custom transport to get in between the sending and receiving of messages so that tests could be fully synchronized.

Last seen: 1 week 3 days ago
Joined: 04/02/2013
Posts: 195

1) If you use transient local durability your test shouldn't need to wait until the entities match, since the data will be sent to late joiners.

2) You may want to use the DataReader's StatusCondition and a WaitSet to deterministically wait until data has been received.



Last seen: 3 years 9 months ago
Joined: 07/19/2020
Posts: 1

explains the problem correctly. I believe the issue that you have @Muhammad Quanit is that you are not understanding what your query is asking for and what you're trying to pass into the query. To break it down for you, your query

"Insert into Calculation_Info (prodID,item_name,net_amount,tax_rate,Amt_Of_Product,Grand_Total) values(?,?,?,?,?,?)"

Takes 6 values that you have to provide (Please NOTE the type of each they take in):

  1. prodID (int)
  2. item_name(String)
  3. net_amount(double)
  4. tax_rate(BigDecimal)
  5. Amt_Of_Product(int)
  6. Grand_Total(BigDecimal)

In your program, you have 6 text boxes that will obtain these values to put in HOWEVER when you call the method: getText() it returns a String type. So when we try to put String prodID = "3" into our SQL query, you would receive the error you originally received: datatype mismatch. Now your current problem is: java.lang.numberFormatException : for input String. This means that you are trying to convert a String value into a number; however it is FAILING. So what could be possible reasons for this? Well...

/* Lets say I have a string with value 4.7*/String s ="4.7";/* Lets try to parse this using parseInt method*/Integer.parseInt(s);//NumberFormatException -- because 4.7 cannot be converted into an int/* How about if my string was a word/phrase?*/String s ="foo";/* Attempt to parse...*/Integer.parseInt(s);//NumberFormatException -- because "foo" cannot be converted into integerDouble.parseDouble(s);//NumberFormatException -- because "foo" cannot be converted into 

So as you can see, you have to be very careful and keep track of what you are converting and passing into your query (and vice versa).

As @YCF_L mentioned, perhaps your String contains white spaces at the start or end. There is a method called trim() in the String class that will help you with that.


/* Let's call the getText() method to obtain our string */String s = item_name.getText();// s = "    Foo Bar " /* NOTE: the white spaces (' ') BEFORE and AFTER the String we want*/System.out.println(s.trim());// Prints out "Foo Bar"

So the String OBJECT has a method called trim() which gets rid of TRAILING and LEADING white spaces, but NOT white spaces in between. So instead of getting " Foo Bar ", we will get "Foo Bar".

I upvoted @YCF_L 's answer -- he deserves the credit. I just wanted you to understand  this concept since you were still confused after his explanation followed by receiving the NumberFormatException.

Editted to include the trim() method that was mentioned in the comments.