Testing a Web API Service

I would like to test the methods of the given web API service. I could do this by adding a test project, instantiating the controllers, and inspecting the methods. However, I don't want to just test the methods, I would like to test the request and response. I am doing something similar in Java, for example:

  private HttpServer server;

    @Before
    public void before() {
        this.server = Servidor.initializeServer();
    }

    @After
    public void stopServer() {
        server.stop();
    }

    @Test
    public void testaQueBuscarUmCarrinhoTrazOCarrinhoEsperado() {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("http://localhost:8080");
        String conteudo = target.path("/carrinhos").request().get(String.class);
        Carrinho carrinho = (Carrinho) new XStream().fromXML(conteudo);
        Assert.assertEquals("Rua Teste", carrinho.getRua());
    }

      

Now, to do something like this, I have to run a web API project to start the server, then run the tests. Is there a way to write some code for this?

+3


source to share


1 answer


yes you can do it, there is something called selfhosted web api see this link you can build in test project on the same lines.



http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

+1


source







All Articles