junit测试和Web服务

时间:2017-07-07 04:54:51

标签: java web-services junit

我有一个测试单元测试用于Web服务的crud,但我遇到了问题。当我运行所有测试都工作,但当我同时运行所有这些测试的出版物(例如)我无法更新或恢复,因为它给出500错误,但如果我一次只运行一个作品。我该怎么办才能一次完成所有事情?

//@Ignore
    @Test
    public void inserirPublicacao() throws MalformedURLException, IOException{
        int code = 0;

        PublicacaoDao dao = new PublicacaoDao();
        Publicacao pb = new Publicacao();

        String localidade = "br 212";
        String descricao = "selvagens na estrada";
        long idUsuario = 2;

        JSONObject jsonObject = new JSONObject();

        //Armazena dados em um Objeto JSON
        jsonObject.put("localidade", localidade);
        jsonObject.put("descricao", descricao);
        jsonObject.put("codigo", idUsuario);    

        Gson gson = new Gson();
        String Json = gson.toJson(jsonObject);

        URL url;

            url = new URL("http://localhost:8084/DivulgueAqui/webresources/webService/pb/inserir");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");

            OutputStream os = connection.getOutputStream();
            os.write(Json.getBytes("UTF-8"));
            os.flush();

            code = connection.getResponseCode();

            os.close();
            connection.disconnect();

            pb = dao.recuperar((long)5);

        assertEquals(204,code);
        assertEquals(pb.getDescricao(),descricao);
        assertEquals(pb.getLocalidade(),localidade);
    }

   // @Ignore
    @Test
    public void atualizarPublicacao() throws MalformedURLException, IOException{

        int code = 0;
        String descricao = "nevasca pequena";
        String localidade = "nunca menos";
        long codigo = 1;//codigo da publicacao

        JSONObject jsonObject = new JSONObject();

        //Armazena dados em um Objeto JSON
        jsonObject.put("descricao", descricao);
        jsonObject.put("localidade", localidade);
        jsonObject.put("codigo", codigo);

        Gson gson = new Gson();
        String Json = gson.toJson(jsonObject);

        URL url;

            url = new URL("http://localhost:8084/DivulgueAqui/webresources/webService/pb/update");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Content-Type", "application/json");

            OutputStream os = connection.getOutputStream();
            os.write(Json.getBytes("UTF-8"));
            os.flush();

            code = connection.getResponseCode();

            os.close();
            connection.disconnect();

        assertEquals(200,code);
    }

   // @Ignore
    @Test
    public void recuperarPublicacoesPorIdDoUsuario() throws MalformedURLException, IOException{
        int code = 0;

        long codigo = 2; // id do usuario
            URL url;

            url = new URL("http://localhost:8084/DivulgueAqui/webresources/webService/pb/listaTodasPorIdUsuario?id="+codigo);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("GET");

            code = connection.getResponseCode();

        assertEquals(200,code);
    }

   // @Ignore
    @Test 
    public void deletarPublicacao() throws MalformedURLException, IOException{
        int code = 0;
        URL url;

         Integer codigo = 2;   

            url = new URL("http://localhost:8084/DivulgueAqui/webresources/webService/pb/delete?id="+codigo);//codigo


            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("DELETE");

            code = connection.getResponseCode();

        assertEquals(204,code);
    }

如果我一起运行,那就是我做的事情

atualizarPublicacao(testesUnitários.TesteWebServicePublicacao)  Time elapsed: 0.296 sec  <<< FAILURE!
java.lang.AssertionError: expected:<200> but was:<500>
    at testesUnitários.TesteWebServicePublicacao.atualizarPublicacao(TesteWebServicePublicacao.java:122)

recuperarPublicacoesPorIdDoUsuario(testesUnitários.TesteWebServicePublicacao)  Time elapsed: 0.313 sec  <<< FAILURE!
java.lang.AssertionError: expected:<200> but was:<500>
    at testesUnitários.TesteWebServicePublicacao.recuperarPublicacoesPorIdDoUsuario(TesteWebServicePublicacao.java:142)


Results :

Failed tests: 
  TesteWebServicePublicacao.atualizarPublicacao:122 expected:<200> but was:<500>
  TesteWebServicePublicacao.recuperarPublicacoesPorIdDoUsuario:142 expected:<200> but was:<500>

我单独运行的问题可以解决所有问题

0 个答案:

没有答案