簡體   English   中英

澤西島客戶單元測試

[英]Jersey Client unit test

我已經設置了Jersey資源,該資源在瀏覽器中完美地返回了列表。 但是我不能讓它在我的單元測試中運行。 我確實收到錯誤消息:

A message body writer for Java class java.util.ArrayList, and Java type java.util.List<at.sunfinder.dto.Location>, and MIME media type application/json was not found

我在下面添加我的課程

資源:

@GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public List<Location> getWeather(@QueryParam("latitude") double latitude, @QueryParam("longitude") double longitude,
            @QueryParam("radius") int radius, @QueryParam("countryCode") String countryCode /* Datum */) {

        setWeatherController(new WeatherControllerImpl());      
        return weatherController.getWeather(latitude, longitude, radius, countryCode);
    }

位置DTO:

@XmlRootElement
public class Location {

    private String location;
    private double latitude;
    private double longitude;
    private Weather weather;

    @XmlTransient
    private Date lastUpdate;

    public Location(){}

    public Location(double latitude, double longitude, String location, Weather weather, Date lastUpdate){
        this.latitude = latitude;
        this.longitude = longitude;
        this.location = location;
        this.weather = weather;
        this.lastUpdate = lastUpdate;
    }

    @XmlElement(name="location")
    public String getLocation() {
        return location;
    }

    @XmlElement(name="latitude")
    public double getLatitude() {
        return latitude;
    }

    @XmlElement(name="longitude")
    public double getLongitude() {
        return longitude;
    }

    @XmlElement(name="weather")
    public Weather getWeather(){
        return weather;
    }   
}

而我的單元測試:

@Test
    public void testGetWeather(){       

        Client client = Client.create();         


        WebResource webResource = client.resource(url); 
        ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);

        List<Location> locations = null;

        if(response.getClientResponseStatus() == ClientResponse.Status.OK){
            locations = response.getEntity(new GenericType<List<Location>>(){});
        }

        assertTrue(locations.size()> 0);
    }

我的pom xml具有以下依賴性:com.sun.jersey jersey-server 1.18.1

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.18.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.18.1</version>
        </dependency>

似乎您的測試請求的是應用程序/ json格式,但是您的位置DTO無法解析它。 嘗試將測試更改為請求application / xml ,即:

ClientResponse response = webResource.accept("application/xml").get(ClientResponse.class);

另外,請確保您的DTO也可以解析json(通過提供@JsonProperty批注)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM