Gradle 使用 Quarkus 框架进行测试

Gradle test with Quarkus Framework

给出以下代码:User/UserResources/UserService 我写道:Schritt3.java

我要写一个测试,但是我完全迷路了。我在谷歌上搜索了很多,但没有接近解决方案。

第一次尝试得到这个错误:RESTEASY003320:publicjava.util.Listde.hse.swa.jaxquarkus.[=47= 的处理参数失败](java.lang.String,java.lang.String,java.lang.String,java.lang.Boolean)

第二个:预期状态代码 <200> 但实际为 <400>。

如您所见,我尝试了不同的编码类型和解析数据的方式。但是用谷歌搜索错误消息对我一点帮助都没有。不知道是我走错了路还是哪里出了问题。

所以问题是:如何正确传递数据。

User.java

import javax.ws.rs.FormParam;

public class User {

 private Long id;
 
 @FormParam("username")
 private String username;
 
 @FormParam("password") 
 private String password;
 
 @FormParam("firstName")
 private String fullName;
 
 @FormParam("isAdmin")
 private boolean isAdmin = false;

 public User() {
 }

 public User(String username, String password, String fullName, boolean isAdmin) {
     this.username = username;
     this.password = password;
     this.fullName = fullName;
     this.isAdmin = isAdmin;
 }

 public Long getId() {
     return id;
 }

 public void setId(Long id) {
     this.id = id;
 }

 public String getUsername() {
     return username;
 }

 public void setUsername(String username) {
     this.username = username;
 }

 // @JsonIgnore()
 public String getPassword() {
     return password;
 }

 // @JsonProperty()
 public void setPassword(String password) {
     this.password = password;
 }
 public String getFullName() {
     return fullName;
 }

 public void setFullName(String fullName) {
     this.fullName = fullName;
 }

 public boolean isAdmin() {
     return isAdmin;
 }

 public void setAdmin(boolean admin) {
     isAdmin = admin;
 }

}

UserResource.java

import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.annotations.Form;

import io.vertx.core.http.HttpServerRequest;

@RequestScoped
@Path("/step3/users")
public class UserResource {

   @Inject
   UserService service;
   
   @Context
   HttpServerRequest request;

   @GET
   @Produces("application/json")
   public List<User> greeting() {
       return service.getUsers();
   }

   @PUT
   @Produces("application/json")
   @Consumes("application/json")
   public List<User> addUser(
       @FormParam("username") String username,
       @FormParam("password") String password,
       @FormParam("fullName") String fullName,
       @FormParam("isAdmin") Boolean isAdmin) {
       User user = new User(username, password, fullName, isAdmin);
       return service.addUser(user);
   } 
   
   @POST
   @Consumes("application/json")
   @Produces("application/json")
   public List<User> updateUser(@Form User form) {
       User user = new User(form.getUsername(), form.getPassword(), 
                            form.getFullName(), form.isAdmin());
       return service.addUser(user);
   }  
}

用户Service.java

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class UserService {

   public static List<User> users = new ArrayList<User>();
   public static Long id = 1L;
   
   public List<User> getUsers() {
       return users;
   }
   
   public List<User> addUser(User user) {
       user.setId(id++);
       users.add(user);
       return users;
   }
   
   public List<User> updateUser(User user) {
       for (int index = 0; index < users.size(); ++index) {
           if (users.get(index).getId().equals(user.getId())) {
               users.set(index, user);
               break;
           }
       }
       return users;
   }
   
   public List<User> removeUser(User user) {
       for (int index = 0; index < users.size(); ++index) {
           if (users.get(index).getId().equals(user.getId())) {
               users.remove(index);
               break;
           }
       }
       return users;
   }

}

Schritt3.java

package de.hse.swa.jaxquarkus;
import de.hse.swa.jaxquarkus.step3.User;
import static org.hamcrest.CoreMatchers.containsString;

import javax.ws.rs.core.MediaType;

import io.quarkus.test.junit.QuarkusTest;
//import io.restassured.RestAssured;
import io.restassured.response.Response;
//import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

import static io.restassured.RestAssured.given;

 
@QuarkusTest
public class Schritt3 {
    
    String username = "test";
    String password = "123";
    String fullName = "testing";
    Boolean isAdmin = false;
    
    private static String requestBody = "{\n" +
            "  \"username\": \"123\",\n" +
            "  \"password\": \"456\",\n" +
            "  \"fullName\": \"789\",\n" +
            "  \"isAdmin\": \"true\" \n}";

    @Test
    public void postRequest() 
    {
        /* First Try
        Response response = given()
                .header("Content-type", "application/json")
                .contentType(ContentType.JSON)
                //.body(requestBody)
                .body(username + password + fullName + isAdmin)
                .when()
                .put("/step3/users")
                .then()
                .extract().response();
        System.out.println("Respone from Step3 is:");
        System.out.println(response.asString());
        // Assertions.assertEquals("1234", response.jsonPath().getString("username"));
        */
        
        /* Second Try
         */
         RestAssured.baseURI = "http://localhost:8080";
         given().urlEncodingEnabled(true)
         .param("username", "user@site.com")
         .param("password", "Pas54321")
         .param("fullName", "MAtze")
         .param("isAdmin", "true")
         //.header("Accept", ContentType.JSON.getAcceptHeader())
        // .header("Content-type", "application/json")
         .contentType("application/json")
         .put("/step3/users")
         .then().statusCode(200);
       
    }
}

长话短说:

我收到的代码不符合预期。为了解决问题,我不得不将 Consume 更改为:

  @Consumes("application/x-www-form-urlencoded")

然后我可以简单地通过 formParam 发送请求:

Response response =
            given()
            .formParam("username", "username")
            .formParam("password", "password")
            .formParam("fullName", "fullName")
            .formParam("isAdmin", true)
            .when()
            .put("/step3/users")
            .then()   
            .extract().response();

然后可以通过断言完成测试。

  Assertions.assertEquals("username", response.jsonPath().getString("username"));

剩下的唯一问题是,返回的 Json 对象以 [] 开头和结尾。因此,如果我要检查用户名,我必须为测试添加方括号,这样才能成功。