JUnit get() 方法在 Spring Boot 中不起作用

Posted

技术标签:

【中文标题】JUnit get() 方法在 Spring Boot 中不起作用【英文标题】:JUnit get() method not working in spring boot 【发布时间】:2021-03-05 01:36:29 【问题描述】:

我正在尝试在 Spring Boot 中使用 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>

【问题讨论】:

你能粘贴整个堆栈跟踪吗? 您没有添加所需的静态导入,这无疑存在于您使用的任何教程中。 IntelliJ 可能仅根据方法名称就可以为您找出它们;他们在MockMvcRequestBuildersMockMvcResultMatchers,如果没记错的话。 是的,我发现这就是原因。谢谢! 【参考方案1】:

你的类声明中不应该有 '()':

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

应该是

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

【讨论】:

以上是关于JUnit get() 方法在 Spring Boot 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

JUnit Assert方法总结

Spring整合junit

spring整合junit问题

计算通过 JUnit 调用函数的次数

08-spring整合 junit

404-not-found-while-running-spring-boot-rest-api