为什么我的测试没有通过并且我得到 org.opentest4j.AssertionFailedError

问题描述 投票:0回答:1

我正在参加 spring 学院的构建 RESTFUL CashCard 系列 API 的 spring boot 课程。 但是当我写测试的时候却得到了意想不到的结果

这是我的CashCard实体类

package com.stephane.cashcard.Entity;

public class CashCard {
    public CashCard(Long cardId, Double amount){}
}

我的 CashCardController 类

package com.stephane.cashcard.Controller;

import com.stephane.cashcard.Entity.CashCard;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cashcards")
public class CashCardController {
    @GetMapping("/{requestedId}")
    public ResponseEntity<CashCard> findById() {
        CashCard cashCard = new CashCard(99L, 0.0);
        return ResponseEntity.ok(cashCard);
    }
}

最后是我的 CashCardApplicationTests 类

package com.stephane.cashcard;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.stephane.cashcard.Entity.CashCard;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Cela démarrera notre application Spring Boot
 * et la rendra disponible pour que notre test
 * puisse y effectuer des requêtes.
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class CashCardApplicationTests {

    /**
     * Injection Du module TestRestTemplate qui permettra
     * de faire des requêtes HTTP à l'application en cours d'execution
     *
     */
    @Autowired
    TestRestTemplate restTemplate;

    @Test
    void returnCashCardWhenDataIsSaved(){
        /**
         * Here we use restTemplate to make an HTTP GET request to our application endpoint /cashcards/99.
         *
         * restTemplate will return a ResponseEntity, which we've captured in a variable we've named response.
         * ResponseEntity is another helpful Spring object that provides valuable information
         * about what happened with our request. We will use this information throughout out tests in this course.
         */
        ResponseEntity<String> response = restTemplate.getForEntity("/cashcards/99", String.class);

        /**
         * We can inspect many aspects of the response, including the HTTP
         * Response Status code, which we expect to be 200 OK.
         */
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

        //Convertir la reponse qui est un String en un objet JSON
        DocumentContext documentContext = JsonPath.parse(response.getBody());



        /**
         * Nous attendons à ce que lorsque nous demandons
         * un cashCard avec l'identifiant 99, un Objet
         * JSON soit retourné avec quelque chose dans le champs id.
         *
         * Nous nous assurons par la suite que l'identifiant n'est pas nul
         */
        Number id = documentContext.read("$.id");
        assertThat(id).isEqualTo(99);
    }

}
 

这就是我上测试课时得到的

预期:200 OK 但是是:406 NOT_ACCEPTABLE org.opentest4j.AssertionFailedError: 预期:200 OK 但是是:406 NOT_ACCEPTABLE ...

spring spring-boot junit spring-test
1个回答
0
投票

开始购买更改通话中的响应类:

    ResponseEntity< CashCard > response = restTemplate.getForEntity("/cashcards/99", CashCard.class);
© www.soinside.com 2019 - 2024. All rights reserved.