У меня проблема с запуском Controller Tests с CompletableFeature,
Я создаю StockController !
@Component
public class StockController implements StockResource {
private UseCaseExecutor useCaseExecutor;
private GetAllStocksUseCase getAllStocksUseCase;
@Autowired
public StockController(UseCaseExecutor useCaseExecutor,
GetAllStocksUseCase getAllStocksUseCase) {
this.useCaseExecutor = useCaseExecutor;
this.getAllStocksUseCase = getAllStocksUseCase;
}
@Override
public CompletableFuture<List<StockResponse>> getAllStocks(
@RequestParam(value="materialNumber", required=false) String materialNumber,
@RequestParam(value="storageLocation", required=false) String storageLocation,
@RequestParam(value="plant", required=false) String plant) {
return useCaseExecutor.execute(
getAllStocksUseCase,
GetAllStocksInputMapper.map(
materialNumber,
storageLocation,
plant
),
(outputValues) -> StockResponse.from(outputValues.getStockList()));
}
И создаю StockControllerTest
@RunWith(SpringRunner.class)
@WebMvcTest(value = StockController.class, secure = false)
public class StockControllerTest {
@MockBean
private GetAllStocksUseCase getAllStocksUseCase;
@SpyBean
private UseCaseExecutorImpl useCaseExecutor;
@Autowired
private MockMvc mockMvc;
@Configuration
@ComponentScan(basePackages = {"com.example.presenter.rest.api.stock"})
static class Config {
}
@Test
public void deliveryOrderReturnsOk() throws Exception {
Stock stock = TestEntityGenerator.randomStock();
GetAllStocksUseCase.InputValues input = GetAllStocksInputMapper.map(
stock.getMaterialNumber(),
stock.getStorageLocation(),
stock.getPlant());
GetAllStocksUseCase.OutputValues output = new GetAllStocksUseCase.OutputValues(Collections.singletonList(stock));
doReturn(output)
.when(getAllStocksUseCase)
.execute(input);
final String request =
"/api/stocks?materialNumber=" +
stock.getMaterialNumber() + "&plant=" + stock.getPlant() + "&storageLocation=" + stock.getStorageLocation();
RequestBuilder payload = asyncGetRequest(request);
mockMvc.perform(payload)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.[0].materialNumber", is(stock.getMaterialNumber())))
.andExpect(jsonPath("$.[0].plant", is(stock.getPlant())))
.andExpect(jsonPath("$.[0].storageLocation", is(stock.getStorageLocation())))
.andExpect(jsonPath("$.[0].availableQuantity", is(stock.getAvailableQuantity())))
.andExpect(jsonPath("$.[0].quantity", is(stock.getQuantity())));
}
protected RequestBuilder asyncGetRequest(String url) throws Exception {
return asyncDispatch(mockMvc.perform(get(url))
.andExpect(request().asyncStarted())
.andReturn());
}
}
И иметь UseCaseExecutor для запуска useCase с некоторым вводом для генерации какого-либо вывода.
GetAllStocksUseCase
@Service
public class GetAllStocksUseCase extends UseCase<GetAllStocksUseCase.InputValues, GetAllStocksUseCase.OutputValues> {
private StockDAO stockDAO;
@Autowired
public GetAllStocksUseCase(StockDAO stockDAO) {
this.stockDAO = stockDAO;
}
@Override
public OutputValues execute(InputValues input) {
return new OutputValues(stockDAO.getAll(input.getStock()));
}
public static class InputValues implements UseCase.InputValues {
private Stock stock;
public InputValues(Stock stock) {
this.stock = stock;
}
public Stock getStock() {
return stock;
}
}
public static class OutputValues implements UseCase.OutputValues {
List<Stock> stockList;
public OutputValues(List<Stock> stockList) {
this.stockList = stockList;
}
public List<Stock> getStockList() {
return stockList;
}
}
}
UseCaseExecutor
@Service
public class UseCaseExecutorImpl implements UseCaseExecutor {
@Override
public <RX, I extends UseCase.InputValues, O extends UseCase.OutputValues> CompletableFuture<RX> execute(
UseCase<I, O> useCase, I input, Function<O, RX> outputMapper) {
return CompletableFuture
.supplyAsync(() -> input)
.thenApplyAsync(useCase::execute)
.thenApplyAsync(outputMapper);
}
}
Когда я запускаю тест, он дает NullPointException для OutputValues
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1013)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:182)
at com.example.presenter.rest.api.stock.StockControllerTest.deliveryOrderReturnsOk(StockControllerTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
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)
Caused by: java.lang.NullPointerException
at com.example.presenter.rest.api.stock.StockController.lambda$getAllStocks$0(StockController.java:37)
at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602)
at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
at java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:443)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Может кто-нибудь помочь мне с этой проблемой?Похоже, что он не запускает выполнение useCase до outputMapper, поэтому значения OutputValues равны нулю