相关疑难解决方法(0)

如何测试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万
查看次数

泽西岛 - 如何模拟服务

我使用"Jersey测试框架"对我的webservice进行单元测试.

这是我的资源类:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

// The Java class will be hosted at the URI path "/helloworld" 

@Path("/helloworld") 
public class class HelloWorldResource {

    private SomeService service;

    @GET 
    @Produces("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        String responseFromSomeService = service.getSomething();
        return responseFromSomeService;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在单元测试中模拟SomeService?

java unit-testing web-services jax-rs jersey

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

使用Mockito模拟在Grizzly上运行的Jersey REST Api

我正在测试我使用Jersey设置的RESTful api.我想用JUnit测试它,同时用Grizzly运行它以提供Http容器.

使用服务器的一般测试工作正常,即发送请求和接收响应等.

api被称为CMSApi,它有一个名为skyService的依赖项,我想模拟出来,所以我只测试了api.所以问题是我如何使用Mockito创建的mockSkyService对象注入CMSApi?相关代码如下:

Grizzly Server启动:

public static HttpServer startServer() {
    final ResourceConfig rc = new ResourceConfig().packages("com.sky");
    rc.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
Run Code Online (Sandbox Code Playgroud)

}

我的JUnit调用上面的start方法:

@Before
public void setUpServer() throws Exception {

    // start the server and create the client
    server = Main.startServer();

    Client client = ClientBuilder.newClient();
    target = client.target(Main.BASE_URI);
}
Run Code Online (Sandbox Code Playgroud)

我用的是运行测试@RunWith(MockitoJUnitRunner.class).

以下是测试中的相关对象:

//System Under Test
private CMSApi cmsApi;

//Mock
@Mock private static SkyService mockSkyService;
Run Code Online (Sandbox Code Playgroud)

这是调用的测试方法:

  @Test
public void testSaveTile() {

    //given
    final Invocation.Builder invocationBuilder = target.path(PATH_TILE).request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);

    Tile tile …
Run Code Online (Sandbox Code Playgroud)

java tdd jersey mockito grizzly

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

标签 统计

java ×3

jersey ×3

unit-testing ×2

grizzly ×1

jax-rs ×1

junit ×1

mockito ×1

rest ×1

tdd ×1

web-services ×1