eliostvs / clean-architecture-delivery-example Goto Github PK
View Code? Open in Web Editor NEWA example of clean architecture in Java 8 and Spring Boot 2.0
A example of clean architecture in Java 8 and Spring Boot 2.0
Hi, I have an issue running Controller Tests,
I create a StockController just like the ProductController you have!
@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()));
}
And create a 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());
}
}
When I run the test it gives a NullPointException on 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)
Could you help me with this problem? It seems that it does not run the execute of useCase before the outputMapper so the OutputValues are null
Hello, I found small mistake here:
Thinking about
new ApiResponse(false, ex.getMessage()), HttpStatus.NOT_FOUND
Whats meaning false in ApiResponse constructor?
For fix this we can implements builder design pattern.
In class APIresponse add methods:
public void setSuccess(boolean bool)
public boolean getSuccess()
For the @controllerAdvice we make:
@ExceptionHandler(value = {DomainException.class})
ResponseEntity<ApiResponse> handleDomainException(DomainException ex) {
ApiReponse response = new ApiResponse(ex.getMessage())
.setSuccess(false)
return new ResponseEntity<>(reponse, HttpStatus.NOT_FOUND);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.