Codegass opened a new issue, #6660:
URL: https://github.com/apache/cloudstack/issues/6660

   ##### ISSUE TYPE
   * Improvement Request
   ##### COMPONENT NAME
   unit-test
   ##### OS / ENVIRONMENT
   N/A
   ##### SUMMARY
   The test case 
[searchForNonExistingLoadBalancer](https://github.com/apache/cloudstack/blob/ddb11b1b966cc2b3443ef4d2eeb55c1d64ff3fb9/server/src/test/java/org/apache/cloudstack/network/lb/ApplicationLoadBalancerTest.java#L189-L205)
 checks that when retrieving a non-existing lb, an 
InvalidParameterValueException should be raised. 
   
   However, the intention is not explicit in the current implementation of the 
test case. 
   See below the current implementation:
   ```java
           try {
               rule = _appLbSvc.getApplicationLoadBalancer(nonExistingLbId);
               if (rule != null) {
                   notFound = false;
               }
           } catch (InvalidParameterValueException ex) {
               notFound = true;
           }
   
           assertTrue("Found non-existing load balancer; no invalid parameter 
value exception was thrown", notFound);
   ```
   
   The test case relies on a parameter named notFound, being asserted in line 
[204](https://github.com/apache/cloudstack/blob/ddb11b1b966cc2b3443ef4d2eeb55c1d64ff3fb9/server/src/test/java/org/apache/cloudstack/network/lb/ApplicationLoadBalancerTest.java#L204),
 and a try-catch block with if condition between lines [195 to 
204](https://github.com/apache/cloudstack/blob/ddb11b1b966cc2b3443ef4d2eeb55c1d64ff3fb9/server/src/test/java/org/apache/cloudstack/network/lb/ApplicationLoadBalancerTest.java#L195-#L204)
 to achieve the testing goal.
   
   It can be simplified to 4 lines with a more explicit intention for easy 
understanding, as follows:
   
   <!--
   In this test case, the 
`_appLbSvc.getApplicationLoadBalancer(nonExistingLbId)` method is the testing 
target, and if we expand the method, we will find out that this method is rely 
on the `_lbDao.findById(ruleId)` method, which is actually mocked in the setup 
of the test class at line [125 - 
126](https://github.com/apache/cloudstack/blob/ddb11b1b966cc2b3443ef4d2eeb55c1d64ff3fb9/server/src/test/java/org/apache/cloudstack/network/lb/ApplicationLoadBalancerTest.java#L125-L126),
 and it will always return `null`.
   ```java
       @Override
       public ApplicationLoadBalancerRule getApplicationLoadBalancer(long 
ruleId) {
           ApplicationLoadBalancerRule lbRule = _lbDao.findById(ruleId);
           if (lbRule == null) {
               throw new InvalidParameterValueException("Can't find the load 
balancer by id");
           }
           return lbRule;
       }
   ```
   
   So here the `rule != null` check is not necessary. This test case can be 
simplified by replacing the if condition and try-catch with the Expected 
Exception Annotation from JUnit. And to make sure the mocked function executed 
properly, I also suggest adding the `verify` function from Mockito.
   -->
   After refactoring
   ```java
    //Negative test - try to retrieve non-existing lb
   public void searchForNonExistingLoadBalancer() {
      assertThrows(InvalidParameterValueException.class, 
_appLbSvc.getApplicationLoadBalancer(nonExistingLbId));
      verify(_lbDao).findById(1L);
     }
   ```
   With the given input nonExistingLbID, the target function 
`_appLbSvc.getApplicationLoadBalancer(nonExistingLbId)` is expected to throw an 
`InvalidParameterValueException`, as specified in the expected annotation. The 
try-catch block and the if condition are removed. I added 
`verify(_lbDao).findById(1L)` since the `_lbDao.findById(ruleId)` method is 
mocked to always return null for testing the target function 
`_appLbSvc.getApplicationLoadBalancer(nonExistingLbId)`. We just need to verify 
that it is executed, when the Exception does not throw, no need to check the 
value as in the original [line 
197](https://github.com/apache/cloudstack/blob/ddb11b1b966cc2b3443ef4d2eeb55c1d64ff3fb9/server/src/test/java/org/apache/cloudstack/network/lb/ApplicationLoadBalancerTest.java#L197).
  
   
   ###### Operational impact
   
   The testing refactoring won’t affect any part of the production code 
behavior. Also, only the original cases are replaced with unit tests.
   
   ###### Note
   Please let me know if this makes sense. Any comments are welcome and 
appreciated.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to