简体   繁体   中英

JUnit get() method not working in spring boot

I am trying to use the Junit testing as an exercise in spring boot, but it seems like either eclipse or IntelliJ IDEA cannot seem to recognize the get, status, and view method in the test method for connection. the console throws a java: '{' expected whenever I try to run it. Is there a way to solve this error?

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"));
    }
}


this is the Controller class

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";
    }

}

dependencies

<?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>

You shouldn't have '()' in your class declaration:

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

should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM