Мне нужно протестировать метод publi c в классе Spring Controller. К сожалению, мое понимание Spring очень незначительно. Все, что я знаю, это то, что есть 2 класса репозитория, подключенные к базе данных, и метод, который я хочу протестировать, извлекает запись из обеих баз данных, как в примере:
@Controller
public class CalculateController {
static double ZERO = 0.00;
static double PROFIT_BOUND = 0.01;
static int DECIMAL_PLACES = 2;
static double MARGIN_PERCENT = 10;
static double PERCENT_DIVIDER = 100;
static boolean renderTable = false;
@Autowired (required = false)
private StateRepository stateRepository;
@Autowired (required = false)
private ProductRepository productRepository;
@Autowired (required = false)
private InternationalTransportCostsRepository internationalTransportCostsRepository;
// some other code unrelated to topic
public Double[] calculateProfitUSA(State state, Product product, Double finalPrice) {
Double[] tab;
if (getTax(state.getId(), product.getCategory()) == ZERO) {
tab = calculateMarginWithoutTaxUSA(state, product, finalPrice);
} else {
tab = calculateMarginWithTaxUSA(state, product, finalPrice);
}
return tab;
}
private Double[] calculateMarginWithTaxUSA(State state, Product product, Double finalPrice) {
Double[] tab = new Double[3];
double profit = calcUSA(state, product, finalPrice);
double estimatedPrice = (product.getCurrentPrice() + state.getTransportCost());
estimatedPrice = estimatedPrice + (estimatedPrice * (getTax(state.getId(), product.getCategory()) / PERCENT_DIVIDER));
double margin = estimatedPrice * MARGIN_PERCENT / PERCENT_DIVIDER;
estimatedPrice = estimatedPrice + (estimatedPrice * MARGIN_PERCENT / PERCENT_DIVIDER);
double currentProfit = calcUSA(state, product, estimatedPrice);
while (currentProfit < margin) {
estimatedPrice += PROFIT_BOUND;
currentProfit = calcUSA(state, product, estimatedPrice);
}
tab[0] = Precision.round(profit, DECIMAL_PLACES);
tab[1] = Precision.round(estimatedPrice, DECIMAL_PLACES);
tab[2] = Precision.round(margin, DECIMAL_PLACES);
return tab;
}
public Double getTax(Long id, String categoryName) {
if (categoryName.equals("Groceries")) {
return stateRepository.findById(id).get().getGroceriesTax();
} else if (categoryName.equals("Prepared-food")) {
return stateRepository.findById(id).get().getPreparedFoodTax();
} else if (categoryName.equals("Prescription-drug")) {
return stateRepository.findById(id).get().getPrescriptionDrugTax();
} else if (categoryName.equals("Non-prescription-drug")) {
return stateRepository.findById(id).get().getNonPrescriptionDrugTax();
} else if (categoryName.equals("Clothing")) {
return stateRepository.findById(id).get().getClothingTax();
} else if (categoryName.equals("Intangibles")) {
return stateRepository.findById(id).get().getIntangiblesTax();
}
return null;
}
Я пытался найти решение на гуглить 2 дня у меня не получилось. Все, что мне удалось придумать, это:
@WebMvcTest(CalculateController.class)
public class CalculateAlgorithmsTests {
@InjectMocks
private CalculateController calc;
@Mock
private StateRepository stateRepository;
@Mock
private ProductRepository productRepository;
private List<State> states;
private List<Product> products;
@Test
public void calculateProfitUSATest(){
states = CSVReader.readStatesFromCSV("csvFiles/states.csv");
products = CSVReader.readProductsFromCSV("csvFiles/products.csv");
productRepository.saveAll(CSVReader.readProductsFromCSV("csvFiles/products.csv"));
stateRepository.saveAll(CSVReader.readStatesFromCSV("csvFiles/states.csv"));
State Connecticut = states.get(5);
Product Apple = products.get(0);
double [] expected = {25,12,32};
Assertions.assertEquals(expected,calc.calculateProfitUSA(Connecticut,Apple,30.23));
}
}
И я получаю ответ с консоли:
java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:141)
at com.taxcalc.controllers.CalculateController.getTax(CalculateController.java:215)
at com.taxcalc.controllers.CalculateController.calculateProfitUSA(CalculateController.java:167)
at com.taxcalc.CalculateAlgorithmsTests.calculateProfitUSATest(CalculateAlgorithmsTests.java:41)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:117)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:184)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:180)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:127)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)