gradle测试

sshcrbum  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(375)

下面的代码是give:user/userresources/userservice我写的:schritt3.java
我得写一个测试,但我完全迷路了。我在google上搜索了那么多,没有找到解决办法。
第一次尝试´s此错误:resteasy003320:处理public java.util.list de.hse.swa.jaxquarkus.step3.userresource.adduser的参数失败(java.lang.string,java.lang.string,java.lang.string,java.lang.boolean)
第二个打开:预期状态代码<200>,但为<400>。
如您所见,我尝试了不同的编码类型和方法来解析数据。但是谷歌搜索错误信息对我一点帮助都没有。我不知道´我不知道我是不是走错了路,还是有什么不对劲。
所以问题是:如何正确传递数据。
用户.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;
 }

}

用户资源.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);
   }  
}

用户服务.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;
   }

}

施里特3.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);

    }
}
8aqjt8rx

8aqjt8rx1#

长话短说:
我得到的代码并没有达到预期的效果。为了解决这个问题,我不得不将消耗改为:

@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();

然后可以通过Assert来完成测试。

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

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

相关问题