Java JUnit get()方法在spring引导中不起作用
我试图将Junit测试用作spring boot中的练习,但eclipse或IntelliJ IDEA似乎无法识别连接测试方法中的get、status和view方法。每当我试图运行控制台时,它都会抛出一个java:'{'预期值。是否有办法解决此错误Java JUnit get()方法在spring引导中不起作用,java,spring-boot,maven,spring-mvc,junit,Java,Spring Boot,Maven,Spring Mvc,Junit,我试图将Junit测试用作spring boot中的练习,但eclipse或IntelliJ IDEA似乎无法识别连接测试方法中的get、status和view方法。每当我试图运行控制台时,它都会抛出一个java:'{'预期值。是否有办法解决此错误 package ca.sheridancollege.giljon.h2DemoWeek4; import org.junit.Test; import org.junit.runner.RunWith; import org.springfra
package ca.sheridancollege.giljon.h2DemoWeek4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestController(){
@Autowired
private MockMvc mockMvc;
@Test
public void testLoadingAddDrinkPage() throws Exception {
this.mockMvc.perform(get("/")) .andExpect(status().isOk()).andExpect(view().name("add.html"));
}
}
这是控制器类
package ca.sheridancollege.giljon.h2DemoWeek4.controller;
import ca.sheridancollege.giljon.h2DemoWeek4.beans.Drink;
import ca.sheridancollege.giljon.h2DemoWeek4.database.DatabaseAccess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class DrinkController {
@Autowired
private DatabaseAccess da;
@GetMapping("/")
public String goHome(){
return "home";
}
@GetMapping("/view.html")
public String goViewData(Model model){
model.addAttribute("drinks", da.getDrinks());
return "view";
}
@GetMapping("/add.html")
public String handleAddPage(Model model){
model.addAttribute("drink", new Drink());
return "add";
}
@GetMapping("/home.html")
public String processAdd(Model model, @ModelAttribute Drink drink){
da.addDrink(drink);
model.addAttribute("drink", new Drink());
return "home";
}
@GetMapping("/editLink/{id}")
public String editLink(Model model, @PathVariable int id){
Drink d = da.getDrinkById(id);
model.addAttribute("drink", d);
//in the database, the id 1. the reference without adding 1 is 0, resulting null
return "modify";
}
@GetMapping("/deleteLink/{id}")
public String deleteLink(@PathVariable int id, Model model){
da.deleteDrink(id);
model.addAttribute("myDrinks", da.getDrinks());
return "redirect:/home.html";
}
@GetMapping("/modify")
public String editDrink(Model model, @ModelAttribute Drink drink){
da.editDrink(drink);
return "redirect:/home.html";
}
}
依赖关系
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ca.sheridancollege.giljon</groupId>
<artifactId>h2DemoWeek4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>h2DemoWeek4</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
</project>
4.0.0
org.springframework.boot
spring启动程序父级
2.3.4.1发布
约谢里登学院
H2周末4
0.0.1-快照
H2周末4
SpringBoot的演示项目
11
org.springframework.boot
spring引导启动器数据jdbc
org.springframework.boot
弹簧启动装置
org.springframework.boot
SpringBootStarterWeb
com.h2数据库
氢
运行时
org.projectlombok
龙目
真的
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
类声明中不应包含“()”:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestController(){
应该是
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestController {
类声明中不应包含“()”:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestController(){
应该是
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestController {
你能粘贴整个堆栈跟踪吗?你没有添加所需的静态导入,这无疑会出现在你正在使用的任何教程中。IntelliJ可以根据方法名称为你找出它们;它们位于
MockMvcRequestBuilders
和MockMvcResultMatchers
,如果内存可用的话。是的,我发现是这样的这就是原因。谢谢!你能粘贴整个堆栈跟踪吗?你没有添加所需的静态导入,这无疑会出现在你正在使用的任何教程中。IntelliJ可以根据方法名称为你找出它们;它们位于MockMvcRequestBuilders
和MockMvcResultMatchers
,如果内存可用的话是的,我发现这就是原因。谢谢!