Last active
October 5, 2021 07:51
-
-
Save 4sskick/0aa9057669b64e6bf40706b9048fec98 to your computer and use it in GitHub Desktop.
unit test with parameter input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
as regular test using unit test we can use jUnit roboelectric Mockito etc. There are some condition which class with parameter more than 1 | |
ex. inteface class | |
`double getPriceAfterSurcharge(double baseCost, double surchargeFixedPrice, double surchargePercentage, Integer quantity);` | |
implementation class: | |
@Override | |
public double getPriceAfterSurcharge(double baseCost, double surchargeFixedPrice, | |
double surchargePercentage, Integer quantity) { | |
double productPrice; | |
if (surchargeFixedPrice > 0) { | |
productPrice = (baseCost + surchargeFixedPrice); | |
} else if (surchargePercentage > 0) {//5000 + ((20/100) * 5000) = 5000 + 1000 = 6000 | |
productPrice = Utils.getRoundedNum(baseCost + ((surchargePercentage / 100) * baseCost)); | |
} else { | |
productPrice = baseCost; | |
} | |
if (quantity != null) {//600.000 | |
productPrice *= quantity; | |
} | |
return productPrice; | |
} | |
for this class could make some condition with variation value as input to make it more concise to test and more robust to get output value as expected. | |
example, in unit test class we can pass value | |
case 1: 5000d, 300d, 0d, 100 -> expected return 530000 | |
case 2: 5000, 0, 20, 100 -> expected return 600000 | |
case 3: 0, 0, 20, 100 -> expected return 0 | |
in unit test class would be like this, | |
@Test | |
public void testGetPriceAfterSurcharge() { | |
double baseCost = 5000; | |
double surchargeFixedPrice = 300; | |
double surchargePercentage = 20; | |
Integer quantity = 100; | |
//surcharge fix price | |
double expected = 530000; | |
double actual = cartItemRepository.getPriceAfterSurcharge(baseCost, surchargeFixedPrice, 0d, quantity); | |
assertEquals(expected, actual, 0d); | |
//surcharge percentage | |
expected = 600000; | |
actual = cartItemRepository.getPriceAfterSurcharge(baseCost, 0d, surchargePercentage, quantity); | |
assertEquals(expected, actual, 0d); | |
//base cost 0 | |
expected = 0d; | |
actual = cartItemRepository.getPriceAfterSurcharge(0d, 0d, surchargePercentage, quantity); | |
assertEquals(expected, actual, 0d); | |
} | |
didn't it take some times to add more use case? | |
By using library `testImplementation 'pl.pragmatists:JUnitParams:1.1.1'` in gradle android would help save tima | |
- sync gradle after add that one. | |
- create class and give annotation `@Runwith(JUnitParamsRunner.class)` | |
- create method to test then along parameters needed, by this case would be | |
@Test | |
@Parameters(method = "inputParams") | |
public void testGetPriceAfterSurcharge(double baseCost | |
, double surchargeFixedPrice | |
, double surchargePercentage | |
, Integer quantity | |
, double expectedReturn) { | |
} | |
- did you see annotation `@Parameters`? that gonna tell IDE which method gonna use as parameter input. | |
- create method which place all case input parameter, by this case would be | |
private Object inputParams() { | |
return new Object[][]{ | |
//baseCost, surchargeFixedPrice, surchargePercentage, quantity, expectedReturn | |
{5000d, 300d, 0d, 100, 530000} | |
, {5000, 0, 20, 100, 600000} | |
, {0, 0, 20, 100, 0} | |
//more input use case here | |
}; | |
} | |
clear enough? | |
- implement assert by calling method on real class to test along with assert | |
- run the test class as usual |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment