我是Java EE的新手,所以在用junit和Arquillian运行单元和集成测试时遇到错误。下面是我的运行配置的一些屏幕截图:
对于Maven库,在我的pom下方:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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.pluralsight.javaee-getting-started.javaee-getting-started-m8</groupId>
<artifactId>bookstore-back</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>Getting Started :: javaee-getting-started-m8 :: Injecting Beans :: 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.arquillian.wildfly>2.0.2.Final</version.arquillian.wildfly>
<version.shrinkwrap>1.2.6</version.shrinkwrap>
<!-- 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 -->
<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>
<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.wildfly.arquillian</groupId>
<artifactId>wildfly-arquillian-container-remote</artifactId>
<version>${version.arquillian.wildfly}</version>
<scope>test</scope>
</dependency>
</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>
</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>
我的类用于测试bean验证和CRUD操作:
import com.pluralsight.bookstore.model.Book;
import com.pluralsight.bookstore.model.Language;
import com.pluralsight.bookstore.util.IsbnGenerator;
import com.pluralsight.bookstore.util.NumberGenerator;
import com.pluralsight.bookstore.util.TextUtil;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
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.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.inject.Inject;
import java.util.Date;
import static org.junit.Assert.*;
@RunWith(Arquillian.class)
public class BookRepositoryTest {
// ======================================
// = Attributes =
// ======================================
private static Long bookId;
// ======================================
// = Injection Points =
// ======================================
@Inject
private BookRepository bookRepository;
@Inject
private IsbnGenerator isbnGenerator;
@Inject
private TextUtil textUtil;
// ======================================
// = Deployment =
// ======================================
@Deployment
public static Archive<?> createDeploymentPackage() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(Book.class)
.addClass(Language.class)
.addClass(BookRepository.class)
.addClass(NumberGenerator.class)
.addClass(IsbnGenerator.class)
.addClass(TextUtil.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml");
}
// ======================================
// = Test methods =
// ======================================
@Test
@InSequence(1)
public void shouldBeDeployed() {
assertNotNull(bookRepository);
assertNotNull(isbnGenerator);
assertNotNull(textUtil);
}
@Test
@InSequence(2)
public void shouldGetNoBook() {
// Count all
assertEquals(Long.valueOf(0), bookRepository.countAll());
// Find all
assertEquals(0, bookRepository.findAll().size());
}
@Test
@InSequence(3)
public void shouldCreateABook() {
// Creates a book
Book book = new Book("isbn", "a title", 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description");
book = bookRepository.create(book);
// Checks the created book
assertNotNull(book);
assertNotNull(book.getId());
bookId = book.getId();
}
@Test
@InSequence(4)
public void shouldFindTheCreatedBook() {
// Finds the book
Book bookFound = bookRepository.find(bookId);
// Checks the found book
assertNotNull(bookFound.getId());
assertTrue(bookFound.getIsbn().startsWith("13-84356-"));
assertEquals("a title", bookFound.getTitle());
}
@Test
@InSequence(5)
public void shouldGetOneBook() {
// Count all
assertEquals(Long.valueOf(1), bookRepository.countAll());
// Find all
assertEquals(1, bookRepository.findAll().size());
}
@Test
@InSequence(6)
public void shouldDeleteTheCreatedBook() {
// Deletes the book
bookRepository.delete(bookId);
// Checks the deleted book
Book bookDeleted = bookRepository.find(bookId);
assertNull(bookDeleted);
}
@Test
@InSequence(7)
public void shouldGetNoMoreBook() {
// Count all
assertEquals(Long.valueOf(0), bookRepository.countAll());
// Find all
assertEquals(0, bookRepository.findAll().size());
}
@Test(expected = Exception.class)
@InSequence(10)
public void shouldFailCreatingANullBook() {
bookRepository.create(null);
}
@Test(expected = Exception.class)
@InSequence(11)
public void shouldFailCreatingABookWithNullTitle() {
bookRepository.create(new Book("isbn", null, 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description"));
}
@Test(expected = Exception.class)
@InSequence(12)
public void shouldFailCreatingABookWithLowUnitCostTitle() {
bookRepository.create(new Book("isbn", "title", 0F, 123, Language.ENGLISH, new Date(), "imageURL", "description"));
}
@Test
@InSequence(13)
public void shouldNotFailCreatingABookWithNullISBN() {
Book bookFound = bookRepository.create(new Book(null, "title", 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description"));
assertTrue(bookFound.getIsbn().startsWith("13-84356-"));
}
@Test(expected = Exception.class)
@InSequence(14)
public void shouldFailInvokingFindByIdWithNull() {
bookRepository.find(null);
}
@Test
@InSequence(15)
public void shouldNotFindUnknownId() {
assertNull(bookRepository.find(99999L));
}
@Test(expected = Exception.class)
@InSequence(16)
public void shouldFailInvokingDeleteByIdWithNull() {
bookRepository.delete(null);
}
@Test(expected = Exception.class)
@InSequence(17)
public void shouldNotDeleteUnknownId() {
bookRepository.delete(99999L);
}
}
当我运行测试用例类时,出现此错误:
找不到类:“ com.pluralsight.bookstore.repository.BookRepositoryTest”空测试套件。
请多多帮助。
预先感谢您的回答。