JUnit get()方法在春季启动中不起作用



我正试图将Junit测试作为春季启动中的一项练习,但eclipse或IntelliJ IDEA似乎无法识别连接测试方法中的get、status和view方法。每当我尝试运行控制台时,它都会抛出一个java:"{"。有办法解决这个错误吗?

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>

类声明中不应该有"((":

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestController(){

应该是

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestController {

相关内容

  • 没有找到相关文章

最新更新