In this spring-boot project the following WebMvcTest
fails because the GET /items
mapping from the ItemController
is not found
@RunWith(SpringRunner.class)
@WebMvcTest(ItemController.class)
@AutoConfigureMockMvc
public class Test_ItemController {
@Autowired
private MockMvc mvc;
@MockBean
private ItemDao dao;
@MockBean
private CartDao cartDao;
@Test
public void test() throws Exception {
// setting up mock response
Cart cart = new Cart();
cart.setId(1);
Item item = new Item();
item.setCart(cart);
item.setItemName("toothbrush");
item.setId(1);
//---------------------
List<Item> items = new ArrayList<>();
items.add(item);
given(this.dao.findAll()).willReturn(items);
this.mvc.perform(get("items").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
The error is:
2018-02-26 12:10:45.816 WARN 12252 --- [ main] o.s.web.servlet.PageNotFound :
No mapping found for HTTP request with URI [items] in DispatcherServlet with name ''
MockHttpServletRequest:
HTTP Method = GET
Request URI = items
Parameters = {}
Headers = {Accept=[application/json]}
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = null
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 404
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
Here is the controller (just a little experiment, so don't focus on missing ResponseEntity and missing service layer)
@RestController
public class ItemController
{
private static final Logger LOG = LoggerFactory.getLogger(ItemController.class);
@Autowired ItemDao dao;
@GetMapping("items")
public List<Item> getAll()
{
List<Item> res = new ArrayList<>();
dao.findAll().forEach(res::add);
return res;
}
@PostMapping("items")
public Item addItem(@RequestBody Item item)
{
return dao.save(item);
}
@GetMapping("items/{item_id}")
public Item getItemById(@PathVariable("item_id") long item_id)
{
Item item = dao.findById(item_id).get();
LOG.info(" ---------------- Retrieved item: {}", item.toString());
return item;
}
}
This is the pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RC1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Thanks for the help
Yes, ok I forgot the slash in the endpoint
this.mvc.perform(get("/items").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
simple comment instead of downvote would suffice imho
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With