0

I'm testing log out feature of application. When passing valid "seesionId" everything works fine, but when I'm passing incorrect "sessionId" it supposed to give 500 error( I was testing in Postman before). When I implement this using springframework it does not catch this code, it give me following:

org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error

at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:572)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:493)
at com.cablevision.cas.adc.channel900.Trusted.LogOutWithIndalidSession.setUpClass(LogOutWithIndalidSession.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:178)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Do you guys have any idea how to solve it

This is my code:

public class LogOutWithIndalidSession {

    static ResponseEntity<String> response;
    HttpHeaders headers = new HttpHeaders();
    JSONObject request;

    @BeforeClass
    public void setUpClass() throws URISyntaxException {
        headers.setContentType(MediaType.APPLICATION_JSON);
        request = new JSONObject();
        request.put("sessionId", "41a21846-32f4-42bbfdc9-7442-735679retgfr8ae2aa");

        ClientHttpRequestFactory requestFactory = new
            HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());

        RestTemplate restTemplate = new RestTemplate(requestFactory);

        response = restTemplate.exchange(
                new URI(URL_LOGOUT),
                HttpMethod.POST, 
                new HttpEntity<String>(request.toString(), headers),
                String.class);

        System.out.println(response.getStatusCode());
    }

    @Test
    public void test(){
        assertThat(response.getStatusCode().value(), is (500));
    }


}
avojak
  • 2,342
  • 2
  • 26
  • 32
Ilya G
  • 447
  • 2
  • 7
  • 17
  • 2
    You should post the stack trace of the server, not the client. If something is called `server error` then it's not without reason. – Boris Treukhov May 12 '16 at 22:03
  • " LogOutWithIndalidSession"? That's not the problem .. but it sounds like a spelling error. As far as the REAL error: "500: Internal server error" means you need to look on the *server* side. Please update your post with the server logs. – paulsm4 May 13 '16 at 00:36
  • But actually it suppose to give "500 : Internal server error", this is how suppose to work. But my client side gives me exception "org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error". I just want test from client side and get that 500 status code to do assertThat(500, is (500)). I don't need fix server – Ilya G May 13 '16 at 00:45
  • In my test I'm just passing invalid seesion ID to check that server will not take it – Ilya G May 13 '16 at 00:52
  • But it doesn't work like that. It throws an exception. Your test is wrong. – user207421 May 13 '16 at 01:07
  • If you're doing a JUnit test, you can assert exception: [How do you assert that a certain exception is thrown in JUnit 4 tests?](http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests) – paulsm4 May 13 '16 at 03:51
  • did you find the solution? I am having the exact same problem – newbie Sep 13 '16 at 10:51
  • yoo just use try {response = restTemplate.exchange(some code)} catch (HttpServerErrorException e) {e.getStatusCode().value();} and it will give you that number. in my case 500 – Ilya G Sep 13 '16 at 17:02

0 Answers0