我正在准备一个包含一些测试用例的应用程序,该应用程序正在使用JERSY api来提供REST服务。没有测试用例,它将是一个成功的构建,但当我用JUNIT编写测试用例时,我得到错误
控制台出现以下日志错误:
java.lang.AbstractMethodError: org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory.create(Ljava/net/URI;Lorg/glassfish/jersey/test/DeploymentContext;)Lorg/glassfish/jersey/test/spi/TestContainer;
at org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
at org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)
at com.verizon.uis.rest.mock.RestServiceTestBase.setUp(RestServiceTestBase.java:90)
at com.verizon.uis.rest.resources.AuthIdentityTest.setUp(AuthIdentityTest.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
扩展RestTestBaseClass
课程的 JerssyTest
。因此,当我们检查这个api进入JUNIT案例时,我们就会收到错误说明。这是RestTestBaseClass
类。
public abstract class RestServiceTestBase extends JerseyTest {
private static final Logger LOGGER = Logger
.getLogger(RestServiceTestBase.class.getName());
protected static MemoryHandler logHandler = new MemoryHandler();
public RestServiceTestBase() throws TestContainerException {
super();
}
public RestServiceTestBase(final TestContainerFactory testContainerFactory) {
super(testContainerFactory);
}
@Override
protected Application configure() {
return new RestApplicationBase();
}
@BeforeClass
public static void setupOnce() throws Exception {
System.setProperty(TestProperties.CONTAINER_PORT,
String.valueOf(getPort(0)));
System.setProperty(TestProperties.RECORD_LOG_LEVEL,
String.valueOf(Level.ALL.intValue()));
final LogManager lm = LogManager.getLogManager();
final Enumeration<String> loggers = lm.getLoggerNames();
while (loggers.hasMoreElements()) {
final String name = loggers.nextElement();
final Logger logger = lm.getLogger(name);
if (logger != null && logHandler != null) {
logger.addHandler(logHandler);
}
}
}
@Override
public void setUp() throws Exception {
super.setUp();
final ConfigurationManager cm = SingletonFactory
.getInstance(ConfigurationManager.class);
final InMemoryConfiguration apigee = new InMemoryConfiguration(
Configuration.CTX_APIGEE);
cm.registerConfiguration(apigee);
apigee.setSetting("API_MIN_VERSION", "1");
apigee.setSetting("API_MAX_VERSION", "6");
}
protected <R> R post(final WebTarget resource,
final Map<String, Object> headers, final String mediaType,
final Class<R> responseClass, final Object entity)
throws JAXBException {
final Response response = WebServiceUtil.post(resource, headers,
mediaType, entity);
assertNotNull(response);
assertEquals("Unexpected Media Type", mediaType, response
.getMediaType().toString());
final R postResponse = response.readEntity(responseClass);
assertNotNull(postResponse);
print(responseClass, postResponse, mediaType);
return postResponse;
}
protected <R> R post(final WebTarget resource,
final Map<String, Object> headers, final Class<R> responseClass,
final Object entity) throws JAXBException {
return post(resource, headers, MediaType.APPLICATION_XML,
responseClass, entity);
}
protected <R> R post(final WebTarget resource,
final Class<R> responseClass, final Object entity)
throws JAXBException {
return post(resource, null, MediaType.APPLICATION_XML, responseClass,
entity);
}
protected <R> R put(final WebTarget resource,
final Map<String, Object> headers, final String mediaType,
final Class<R> responseClass, final Object entity)
throws JAXBException {
final Response response = WebServiceUtil.put(resource, headers,
mediaType, entity);
assertNotNull(response);
assertEquals("Unexpected Media Type", mediaType, response
.getMediaType().toString());
final R putResponse = response.readEntity(responseClass);
assertNotNull(putResponse);
print(responseClass, putResponse, mediaType);
return putResponse;
}
protected <R> R put(final WebTarget resource,
final Map<String, Object> headers, final Class<R> responseClass,
final Object entity) throws JAXBException {
return put(resource, headers, MediaType.APPLICATION_XML, responseClass,
entity);
}
protected <R> R put(final WebTarget resource, final Class<R> responseClass,
final Object entity) throws JAXBException {
return put(resource, null, MediaType.APPLICATION_XML, responseClass,
entity);
}
@SuppressWarnings("unchecked")
protected <R> R get(final WebTarget resource,
final Map<String, Object> headers, final String mediaType,
final Class<R> responseClass) throws JAXBException {
final Response response = WebServiceUtil.get(resource, headers,
mediaType);
assertNotNull("Response is null!", response);
assertNotNull("Response media type is null!", response.getMediaType());
assertEquals("Unexpected Media Type", mediaType, response
.getMediaType().toString());
if (Response.class.equals(responseClass)) {
return (R) response;
}
final R getResponse = response.readEntity(responseClass);
if (mediaType != null && "application/pdf".equalsIgnoreCase(mediaType)) {
return getResponse;
}
assertNotNull(getResponse);
print(responseClass, getResponse, mediaType);
return getResponse;
}
protected <R> R get(final WebTarget resource, final Class<R> responseClass)
throws JAXBException {
return get(resource, null, MediaType.APPLICATION_XML, responseClass);
}
protected <R> R delete(final WebTarget resource,
final Map<String, Object> headers, final String mediaType,
final Class<R> responseClass, final Object entity)
throws JAXBException {
final Response response = WebServiceUtil.delete(resource, headers,
mediaType, entity);
assertNotNull(response);
assertEquals("Unexpected Media Type", mediaType, response
.getMediaType().toString());
final R deleteResponse = response.readEntity(responseClass);
assertNotNull(deleteResponse);
print(responseClass, deleteResponse, mediaType);
return deleteResponse;
}
protected <R> R delete(final WebTarget resource,
final Class<R> responseClass, final Object entity)
throws JAXBException {
return delete(resource, null, MediaType.APPLICATION_XML, responseClass,
entity);
}
protected <J> void print(final Class<J> jaxbClass, final J jaxbObject,
final String mediaType) throws JAXBException {
if (MediaType.APPLICATION_JSON.equalsIgnoreCase(mediaType)) {
printJson(jaxbClass, jaxbObject);
} else {
printXml(jaxbClass, jaxbObject);
}
}
protected <J> void printXml(final Class<J> jaxbClass, final J jaxbObject)
throws JAXBException {
final JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass);
final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(jaxbObject, System.out);
}
protected <J> void printJson(final Class<J> jaxbClass, final J jaxbObject)
throws JAXBException {
final JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass);
final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
final org.codehaus.jettison.mapped.Configuration config = new org.codehaus.jettison.mapped.Configuration();
final MappedNamespaceConvention con = new MappedNamespaceConvention(
config);
final Writer writer = new OutputStreamWriter(System.out);
final XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con,
writer);
jaxbMarshaller.marshal(jaxbObject, xmlStreamWriter);
}
protected static int getPort(final int defaultPort) {
ServerSocket server = null;
int port = -1;
try {
server = new ServerSocket(defaultPort);
port = server.getLocalPort();
} catch (final IOException e) {
// ignore
LOGGER.log(Level.FINEST, e.getMessage(), e);
} finally {
if (server != null) {
try {
server.close();
} catch (final IOException e) {
LOGGER.log(Level.FINEST, e.getMessage(), e);
// ignore
}
}
}
if (port != -1 || defaultPort == 0) {
return port;
}
return getPort(0);
}
protected void writeLogsToStdOut() {
final List<LogRecord> logs = logHandler.getLogs();
for (final LogRecord r : logs) {
LOGGER.log(Level.INFO, r.getMessage());
}
}
protected void assertResponseSuccess(
final IUISResponseStatus uisStatusResponse) {
assertResponseStatus(uisStatusResponse, 0, "SUCCESS");
}
protected void assertResponseFailure(
final IUISResponseStatus uisStatusResponse) {
assertResponseStatus(uisStatusResponse, 1, "FAILED");
}
protected void assertResponseStatus(
final IUISResponseStatus uisStatusResponse, final int code,
final String label) {
assertNotNull("UIS Response Status not provided", uisStatusResponse);
assertEquals("Request did not succeed -- bad status code", code,
uisStatusResponse.getStatusCd());
assertEquals("Request did not succeed -- bad status label", label,
uisStatusResponse.getStatusLabel());
}
protected void assertMessagesContain(final String messageType,
final List<IUISResponseStatusMessage> messages,
final String... codes) {
final Set<String> codeset = new HashSet<String>();
for (final IUISResponseStatusMessage m : messages) {
codeset.add(m.getMessageCode());
}
if (codes == null) {
return;
}
for (final String code : codes) {
if (StringUtils.isEmpty(code)) {
continue;
}
assertTrue("Missing " + messageType + " " + code,
codeset.contains(code));
}
}
}
有关如何解决此错误的任何想法?