Spring Controller测试失败并出现404错误

时间:2019-05-23 17:50:00

标签: kotlin spring-test-mvc spring-restdocs

我正在使用Spring Rest文档来创建文档,我的问题是,当我同时将测试和控制器放在同一文件夹中时,该测试正在运行:

enter image description here

但是当我尝试从此软件包中更改控制器并将其移至控制器时,我遇到了下一个问题:

import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.lang.reflect.Array;
import java.util.Random;

import javax.swing.*;

public class SimpleWindow extends JPanel implements KeyListener {
    public static final int DRAWING_WIDTH = 800;
    public static final int DRAWING_HEIGHT = 600;

    private Runner runner;
    private MovingPlatform platform, platform2, platform3, platform4, platform5;

    private boolean leftKeyPressed, rightKeyPressed, upKeyPressed;
    private int width = getWidth();
    private int height = getHeight();

    public SimpleWindow() {
        super();
        runner = new Runner(380, 0);
        platform = new MovingPlatform("bricks.png", 200, 500, 200, 50);
        platform2 = new MovingPlatform("bricks.png", 600, 400, 200, 50);
        platform3 = new MovingPlatform("bricks.png", 200, 300, 200, 50);
        platform4 = new MovingPlatform("bricks.png", 400, 200, 200, 50);
        platform5 = new MovingPlatform("bricks.png", 100, 100, 200, 50);
        setBackground(Color.WHITE);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g); // Call JPanel's paintComponent method to paint the background

        int width = getWidth();
        int height = getHeight();

        double ratioX = (double) width / DRAWING_WIDTH;
        double ratioY = (double) height / DRAWING_HEIGHT;

        Graphics2D g2 = (Graphics2D) g;
        AffineTransform at = g2.getTransform();
        g2.scale(ratioX, ratioY);

        platform.draw(g, this);
        platform2.draw(g, this);
        platform3.draw(g, this);
        platform4.draw(g, this);
        platform5.draw(g, this);
        runner.draw(g, this);

        g2.setTransform(at);
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            leftKeyPressed = true;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            rightKeyPressed = true;
        } else if (e.getKeyCode() == KeyEvent.VK_UP) {
            runner.jump();
            upKeyPressed = true;
        }
    }

    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            leftKeyPressed = false;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            rightKeyPressed = false;
        } else if (e.getKeyCode() == KeyEvent.VK_UP) {
            upKeyPressed = false;
        }
    }

    public void keyTyped(KeyEvent e) {

    }

    public void run() {
        while (true) {
            // MAKE A CHANGE
            // change runner
            if (leftKeyPressed) {
                runner.walk(-1);
            } else if (rightKeyPressed) {
                runner.walk(1);
            } else if (upKeyPressed) {
                runner.jump();
            }
            //System.out.println("before fall");
            runner.fall(platform, platform2, platform3, platform4, platform5);
            // runner.fall(platform2);
            checkrunner();
            checkplatform();
            checkplatform2();
            checkplatform3();
            checkplatform4();
            checkplatform5();
            // change platform
            platform.moveByAmount(-6, 0);
            platform2.moveByAmount(-6, 0);
            platform3.moveByAmount(-6, 0);
            platform4.moveByAmount(-6, 0);
            platform5.moveByAmount(-6, 0);
            // SHOW THE CHANGE
            repaint();

            // WAIT
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    public void checkrunner() {
        int x = runner.getX() + runner.getWidth() / 2;
        int y = runner.getY() + runner.getHeight() / 2;
        if (x < 0 || x > DRAWING_WIDTH /* || y < 0 */|| y > DRAWING_HEIGHT )
            runner = new Runner(380, 0);
    }

    public void checkplatform() {
        int x = platform.getX() + platform.getWidth() / 2;
        int y = platform.getY() + platform.getHeight() / 2;
        if (x < 0 || x > DRAWING_WIDTH || y < 0 || y > DRAWING_HEIGHT)
            platform = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);

    }

    public int randomNumY() {
        int numY = (int) Math.floor((Math.random() * 8)) * 100;

        return numY;
    }

    public void checkplatform2() {

        int x2 = platform2.getX() + platform2.getWidth() / 2;
        int y2 = platform2.getY() + platform2.getHeight() / 2;
        if (x2 < 0 || x2 > DRAWING_WIDTH || y2 < 0 || y2 > DRAWING_HEIGHT) {
            platform2 = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
        }

    }

    public void checkplatform3() {
        int x2 = platform3.getX() + platform3.getWidth() / 2;
        int y2 = platform3.getY() + platform3.getHeight() / 2;
        if (x2 < 0 || x2 > DRAWING_WIDTH || y2 < 0 || y2 > DRAWING_HEIGHT) {
            platform3 = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
        }

    }

    public void checkplatform4() {
        int x2 = platform4.getX() + platform4.getWidth() / 2;
        int y2 = platform4.getY() + platform4.getHeight() / 2;
        if (x2 < 0 || x2 > DRAWING_WIDTH || y2 < 0 || y2 > DRAWING_HEIGHT) {
            platform4 = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
        }
    }

    public void checkplatform5() {
        int x2 = platform5.getX() + platform5.getWidth() / 2;
        int y2 = platform5.getY() + platform5.getHeight() / 2;
        if (x2 < 0 || x2 > DRAWING_WIDTH || y2 < 0 || y2 > DRAWING_HEIGHT) {
            platform5 = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
        }

    }

    public void makeChange() {


    }

    public void showChange() {

        repaint();

    }

    public static void main(String[] args) {
        JFrame w = new JFrame("Simple Window");
        w.setBounds(100, 100, 1000, 600);
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SimpleWindow panel = new SimpleWindow();
        w.addKeyListener(panel);
        w.add(panel);
        w.setResizable(true);
        w.setVisible(true);

        panel.run();
    }
}

MY控制器类如下:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /
       Parameters = {}
          Headers = {}
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

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 = []

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /
       Parameters = {}
          Headers = {}
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

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 = []

java.lang.AssertionError: Status 
Expected :200
Actual   :404

我的测试班:

package com.espn.csemobile.espnapp.test.units.controllers

import java.util.Collections

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class HomeController {

    @GetMapping("/")
    fun greeting(): Map<String, Any> {
        return Collections.singletonMap<String, Any>("message", "Hello World")
    }
}

仅当文件位于其他位置时,测试才会失败。 有什么想法吗?

0 个答案:

没有答案