0

我正在尝试遵循 Java EE 和 Arquillian 的教程。我正在运行 java sdk 8, Glassfish5,并且正在使用 NetBeans。修改我的 POM 后,我可以轻松地工作,我能够部署和运行打包的测试,但我现在遇到以下错误。

错误信息

createBook(com.tutorial.bookstore.rest.BookEndpointTest)  Time elapsed: 0.396 sec  <<< ERROR!
javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request
    at com.tutorial.bookstore.rest.BookEndpointTest.createBook(BookEndpointTest.java:86)
Caused by: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/json type: com.tutorial.bookstore.model.Book
    at com.tutorial.bookstore.rest.BookEndpointTest.createBook(BookEndpointTest.java:86)

我的测试

package com.tutorial.bookstore.rest;


import com.tutorial.bookstore.model.Book;
import com.tutorial.bookstore.model.Language;
import com.tutorial.bookstore.repository.BookRepository;
import com.tutorial.util.IsbnGenerator;
import com.tutorial.util.NumberGenerator;
import com.tutorial.util.TextUtil;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.extension.rest.client.ArquillianResteasyResource;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.util.Date;
import javax.ws.rs.core.MediaType;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.Status.*;
import static org.junit.Assert.*;


@RunWith(Arquillian.class)
@RunAsClient
public class BookEndpointTest {

    // ======================================
    // =             Attributes             =
    // ======================================

    private static String bookId;
    private Response response;

    // ======================================
    // =             Deployment             =
    // ======================================

    @Deployment(testable = false)
    public static Archive<?> createDeploymentPackage() {

        return ShrinkWrap.create(WebArchive.class)
            .addClass(Book.class)
            .addClass(Language.class)
            .addClass(BookRepository.class)
            .addClass(NumberGenerator.class)
            .addClass(IsbnGenerator.class)
            .addClass(TextUtil.class)
            .addClass(BookEndpoint.class)
            .addClass(JAXRSConfiguration.class)
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
            .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml");
    }

    // ======================================
    // =            Test methods            =
    // ======================================

    @Test
        public void createBook(@ArquillianResteasyResource("api/books") WebTarget webTarget) {

         // Creates a book
        Book book = new Book("isbn", "a  title", 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description");
        response = webTarget.request(APPLICATION_JSON).post(Entity.entity(book, MediaType.APPLICATION_JSON));
        assertEquals(CREATED.getStatusCode(), response.getStatus());


    }



}

我当前的 POM 如下所示

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tutorial</groupId>
    <artifactId>bookstore-back</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>bookstore-back</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Test -->
        <version.junit>4.12</version.junit>
        <version.arquillian>1.1.13.5</version.arquillian>
        <version.shrinkwrap>1.2.6</version.shrinkwrap>
        <version.arquillian.rest>1.0.0.Alpha4</version.arquillian.rest>
        <version.resteasy>3.1.2.Final</version.resteasy>
        <!-- Plugins -->
        <version.surefire.plugin>2.19.1</version.surefire.plugin>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.arquillian</groupId>
                <artifactId>arquillian-universe</artifactId>
                <version>${version.arquillian}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>




    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- TEST -->
        <!--START ==> Needed for arquillian remote testing on glassfish-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${version.junit}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.arquillian.universe</groupId>
            <artifactId>arquillian-junit</artifactId>
            <!--  <version>${version.arquillian}</version> -->
            <scope>test</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap</groupId>
            <artifactId>shrinkwrap-api</artifactId>
            <version>${version.shrinkwrap}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.container</groupId>
            <artifactId>arquillian-glassfish-remote-3.1</artifactId>
            <version>1.0.0.CR4</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-rest-client-impl-3x</artifactId>
            <version>${version.arquillian.rest}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.jboss.resteasy</groupId>
                    <artifactId>resteasy-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>${version.resteasy}</version>
            <scope>test</scope>
        </dependency> 
        <!--END ==> Needed for arquillian rest client testing-->
    </dependencies>

    <build>
        <finalName>bookstore-back</finalName>

        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${version.surefire.plugin}</version>
                <configuration>
                    <systemPropertyVariables>
                        <!-- <arquillian.launch>arquillian-wildfly-remote</arquillian.launch> -->
                        <arquillian.launch>arquillian-glassfish-remote-3.1</arquillian.launch> 
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>src</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>
4

0 回答 0