使用JAX-RS Response进行JUnit测试

时间:2017-02-03 11:14:31

标签: java junit jax-rs

我正在尝试测试自己编写的方法RuleCheckerException。

方法:

@Override
public RuleCheckerService checkStartEndDateRule(String fieldStartDate, String fieldEndDate, Object o) throws RuleCheckerException {
    Date startDate = new Date();
    Date endDate = new Date();
    try {
        startDate = (Date) ObjectOperationUtil.getFieldValue(fieldStartDate, o);
        endDate = (Date) ObjectOperationUtil.getFieldValue(fieldEndDate, o);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();}
    if (startDate.getTime() >= endDate.getTime()) {
        CDMResponse response = new CDMResponse(ResponseMessageType.START_END_DATE.getMessageTypeId(), ResponseMessageType.START_END_DATE.getRuleDefinition(), null);
        String errorMessage = ResponseMessageType.START_END_DATE.getRuleDefinition() + ": " + fieldStartDate + ": " + startDate + " cannot be later than " + fieldEndDate + ": " + endDate;
        throw new RuleCheckerException(errorMessage, Response.status(Status.EXPECTATION_FAILED).entity(response).build());
    }
    return this;
}

测试方法:

@Test(expected = RuleCheckerException.class)
public void testCheckStartEndDateRule() throws RuleCheckerException {
    RuleCheckerService ruleCheckerService = new RuleCheckerService();
    RuleCheckerServiceTest.Vehicle inner = this.new Vehicle("N35", new Date(1486110893470L), new Date());
    ruleCheckerService.checkStartEndDateRule("createdDate", "lastLogResponse", inner);
}

和JUnit错误消息:

[junit] Testcase: testCheckStartEndDateRule took 0,027 sec
[junit]     Caused an ERROR
[junit] Unexpected exception, expected<pl.pixel.csip.cmn.rules.exceptions.RuleCheckerException> but was<java.lang.ExceptionInInitializerError>
[junit] java.lang.Exception: Unexpected exception, expected<pl.pixel.csip.cmn.rules.exceptions.RuleCheckerException> but was<java.lang.ExceptionInInitializerError>
[junit] Caused by: java.lang.ExceptionInInitializerError
[junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
[junit]     at java.lang.Class.newInstance(Class.java:442)
[junit]     at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:117)
[junit]     at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:165)
[junit]     at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:135)
[junit]     at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)
[junit]     at javax.ws.rs.core.Response$ResponseBuilder.newInstance(Response.java:843)
[junit]     at javax.ws.rs.core.Response.status(Response.java:585)
[junit]     at javax.ws.rs.core.Response.status(Response.java:596)
[junit]     at pl.pixel.csip.cmn.rules.service.RuleCheckerService.checkStartEndDateRule(Unknown Source)
[junit]     at pl.pixel.csip.cmn.rules.tests.RuleCheckerServiceTest.testCheckStartEndDateRule(Unknown Source)
[junit] Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
[junit]     at org.jboss.resteasy.logging.Logger.getLogger(Logger.java:104)
[junit]     at org.jboss.resteasy.spi.ResteasyProviderFactory.<clinit>(ResteasyProviderFactory.java:197)
[junit] Caused by: java.lang.reflect.InvocationTargetException
[junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
[junit]     at org.jboss.resteasy.logging.Logger.getLogger(Logger.java:92)
[junit] Caused by: java.lang.NoClassDefFoundError: org/jboss/logmanager/Level
[junit]     at org.jboss.resteasy.logging.impl.Log4jLogger.<init>(Log4jLogger.java:19)
[junit] Caused by: java.lang.ClassNotFoundException: org.jboss.logmanager.Level
[junit]     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

建立失败

所以最有问题的代码是:

Response.status(Status.EXPECTATION_FAILED).entity(response).build()

来自main方法。没有它,测试正在编译而没有任何错误。 我读了一些类似的问题,但没有人从Exception返回JAX-RS响应。

0 个答案:

没有答案