小编acm*_*acm的帖子

如何测试Jersey REST Web服务?

我编写了一个Restful Web服务,必须使用JUnit4进行测试.我已经使用Jersey Client编写了一个客户端.但是想知道我是否只能使用junit4来测试我的服务.至少有人可以帮我提供样品.

我的休息服务具有验证方法,该方法获取用户名,密码并返回令牌.

我已经为authenticate方法编写了测试用例.但我不确定如何使用url进行测试.

public class TestAuthenticate {
    Service service  = new Service();
    String username = "user";
    String password = "password";
    String token;

    @Test(expected = Exception.class)
    public final void testAuthenticateInputs() {
        password = "pass";
        service.authenticate(username, password);
    }

    @Test(expected = Exception.class)
    public final void testAuthenticateException(){
        username = null;
        String token = service.authenticate(username, password);
        assertNotNull(token);
    }

    @Test
    public final void testAuthenticateResult() {
        String token = service.authenticate(username, password);
        assertNotNull(token);
    }
}
Run Code Online (Sandbox Code Playgroud)

java rest junit unit-testing jersey

17
推荐指数
1
解决办法
4万
查看次数

如何重构XSD以便解组不返回JAXBElement

我有以下架构:

<xsd:schema xmlns:bar="http://www.foo.org/bar"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:annox="http://annox.dev.java.net"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        targetNamespace="http://www.foo.org/bar"
        jaxb:extensionBindingPrefixes="annox" jaxb:version="2.1" elementFormDefault="qualified">

    <xsd:element name="unit" type="bar:unit" />

    <xsd:complexType name="unit">
        <xsd:annotation>
            <xsd:appinfo>
                <annox:annotate>@javax.xml.bind.annotation.XmlRootElement(name="unit")</annox:annotate>
            </xsd:appinfo>
        </xsd:annotation>
            <xsd:sequence>
            <xsd:any processContents="skip" />
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

当我解组这个XML时

<unit xmlns="http://www.foo.org/bar">
    <text>Name</text>
</unit>
Run Code Online (Sandbox Code Playgroud)

返回的对象是javax.xml.bind.JAXBElement<Unit>,但我想org.foo.bar.Unit回来.我需要这个,因为在我的情况下解组是由JAX-RS提供者或SpringWeb隐式发生的.

观察:

  • 如果我删除/替换<xsd:any processContents="skip" />声明,JAXB开始返回org.foo.bar.Unit.
  • 如果我删除<xsd:element name="unit" type="bar:unit" />声明,JAXB开始返回org.foo.bar.Unit(虽然需要在解组时禁用验证).

因此,我会说,鉴于XSD是证明问题的最小XSD.

问题:为什么JAXB包装org.foo.bar.UnitJAXBElement以上的组合?从我看到的情况来看,XSD类型unit无法使标签名称unit与之不同,为什么JAXB需要这种工厂方法呢?

@XmlElementDecl(namespace = "http://www.foo.org/bar", name = "unit")
public JAXBElement<Unit> createUnit(Unit value) { ... }
Run Code Online (Sandbox Code Playgroud)

展示JAXB 2.2.7问题的项目就在这里 …

java xsd jaxb unmarshalling

9
推荐指数
1
解决办法
1224
查看次数

Groovy命名参数导致参数分配切换 - 任何解决方法?

Groovy会将所有命名参数收集到一个映射中,并将其作为第一个参数传递给方法.这似乎很整洁,但在尝试使其工作后,它似乎真的无法使用.

所以问题是这样的方法:

def method(paramMap, specificVar1 = 7, specificVar2 = 14)
Run Code Online (Sandbox Code Playgroud)

当你用这样的方法调用这个方法时:

method(12, extraValue: "hello")
Run Code Online (Sandbox Code Playgroud)

你得到了很多你期望的东西:

assert 12 == specificVar1
assert 14 == specificVar2
assert [extraValue:"hello"] == paramMap
Run Code Online (Sandbox Code Playgroud)

不错,有道理.问题是,如果你认为Mapparams是可选的,你可以得到这样的值:

method(12)
Run Code Online (Sandbox Code Playgroud)
assert paramMap == 12
assert specificVar1 == 7 // default values
assert specificVar2 == 14
Run Code Online (Sandbox Code Playgroud)

那个标量应该进入特定的变量 - 而不是地图.如果我在方法中专门输入地图:

def method(Map paramMap, specificVar1 = 7, specificVar2 = 14)
Run Code Online (Sandbox Code Playgroud)

然后method(12, extraValue: "hello")像以前一样工作,但method(12)抛出一个ClassCastException.这似乎没用.有没有办法让它变得Map"粘",如果没有Map参数它就会变成空的?

groovy named-parameters

5
推荐指数
1
解决办法
1906
查看次数

DuplicateServiceException JBoss服务器

尝试在Wildfly 10服务器上运行EAR时出现奇怪的错误。它说.session is already registered,哪些其他类似的错误据说是由使用同一应用程序的多个应用程序引起的context-root。但是我只是部署一个EAR,所以我很困惑。

09:57:30,654 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC000001: Failed to start service jboss.deployment.subunit."********.ear"."*********.war".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.subunit."***********.ear"."**********".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of subdeployment "******.war" of deployment "*******.ear"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:154)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)


Caused by: org.jboss.msc.service.DuplicateServiceException: Service jboss.undertow.deployment.default-server.default-host./******.session is already registered
Run Code Online (Sandbox Code Playgroud)

jboss java-ee wildfly-10

4
推荐指数
2
解决办法
7493
查看次数

如何显示PostgreSQL系统变量

我正在寻找一个bash实用程序,如mysqladmin,它可以列出Postgres运行实例上的所有系统变量值.

是否有可以用作mysqladmin的实用程序:mysqladmin -pxxxxx variables

postgresql-9.1

3
推荐指数
1
解决办法
8235
查看次数

使用h:commandButton传递参数 - 或等效参数

我在其他线程上读到这不起作用:

<h:commandButton value="Create New Account" 
                action="#{acctBean.doCreate}" >
   <f:param name="acctName" value="#{acctBean.handle}" />
   <f:param name="acctNo" value="#{acctBean.id}" />
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)

doCreate()如果创建帐户,该方法将返回导航到"祝贺"页面.然后,目标页面可以解析#{param.handle}#{param.id}.

我知道如果我使用它会有效h:commandLink,但我想要一个按钮,而不是一个链接.有没有普遍接受的方式呢?

更新:

基于@BalusC的第一个答案,我创建了以下测试代码:

<h:commandButton value="Push Me" action="goAcctCreated" >
    <f:param name="acctName" value="This Is Account Name" />
    <f:param name="acctNo" value="1234" />
</h:commandButton>
<h:button value="Push Me #2" outcome="newAcct" >
    <f:param name="acctName" value="This Is Account Name" />
    <f:param name="acctNo" value="1234" />
</h:button>
Run Code Online (Sandbox Code Playgroud)

在目标页面中,我有:

<p>You may now log in with the account you just created: <b>#{param['acctName']}</b>.</p>
<p>This is account number <b>#{param['acctNo']}</b>.</p> …
Run Code Online (Sandbox Code Playgroud)

jsf-2

2
推荐指数
1
解决办法
1万
查看次数