Le cas de test ci-dessous me donne une exception de connexion refusée et quand je commente le corps de la méthode, c'est un succès. Donc je crois qu'il n'y a pas de problème avec les ports mais un problème avec l'invocation assurée que je ne peux pas comprendre. Même lorsque je vérifie avec un client de repos, l'api lui-même retourne avec succès des livres en tant que json.
@Test
public void testMe() {
get("/book").then().assertThat().contentType(ContentType.JSON);
}
Le contrôleur ressemble à ci-dessous,
@RestController
@RequestMapping("/book")
public class BookController {
//Other crud api's are there
@RequestMapping(method = RequestMethod.GET)
public Map<String, Object> getAllBooks(){
List<Book> books = bookRepository.findAll();
Map<String, Object> response = new LinkedHashMap<String, Object>();
response.put("totalBooks", books.size());
response.put("books", books);
return response;
}
}
Trace d'exception,
Java.net.ConnectException: Connection refused: connect
at Java.net.DualStackPlainSocketImpl.connect0(Native Method)
at Java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.Java:79)
at Java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.Java:345)
at Java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.Java:206)
at Java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.Java:188)
at Java.net.PlainSocketImpl.connect(PlainSocketImpl.Java:172)
at Java.net.SocksSocketImpl.connect(SocksSocketImpl.Java:392)
at Java.net.Socket.connect(Socket.Java:589)
at org.Apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.Java:117)
at org.Apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.Java:177)
at org.Apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.Java:304)
at org.Apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.Java:611)
at org.Apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.Java:446)
at org.Apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.Java:863)
at org.Apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.Java:82)
at org.Apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.Java:57)
at org.Apache.http.client.HttpClient$execute$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.Java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:120)
at com.jayway.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder.doRequest(RequestSpecificationImpl.groovy:1807)
at com.jayway.restassured.internal.http.HTTPBuilder.doRequest(HTTPBuilder.Java:490)
at com.jayway.restassured.internal.http.HTTPBuilder.request(HTTPBuilder.Java:439)
at com.jayway.restassured.internal.http.HTTPBuilder$request$2.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.Java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:124)
at com.jayway.restassured.internal.RequestSpecificationImpl.sendHttpRequest(RequestSpecificationImpl.groovy:1285)
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:497)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.Java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.Java:324)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:1206)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:1015)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:806)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.Java:45)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.Java:55)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.Java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.Java:133)
at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:1124)
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:497)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.Java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.Java:324)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:1206)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:1015)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:806)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.Java:45)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.Java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:128)
at com.jayway.restassured.internal.filter.SendRequestFilter.filter(SendRequestFilter.groovy:31)
at com.jayway.restassured.filter.Filter$filter.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.Java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:124)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:49)
at com.jayway.restassured.filter.FilterContext$next.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.Java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.Java:120)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeFilterChain(RequestSpecificationImpl.groovy:994)
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:497)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.Java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.Java:324)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:1206)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:1015)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:806)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.Java:45)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.Java:55)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.Java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.Java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.Java:149)
at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1452)
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:497)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.Java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.Java:324)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:1206)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:1015)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.Java:806)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.Java:45)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.Java:55)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.Java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.Java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.Java:149)
at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:150)
at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy)
at com.jayway.restassured.RestAssured.get(RestAssured.Java:808)
at app.controller.BookControllerTest1.testMe(BookControllerTest1.Java:33)
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:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.Java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.Java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.Java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.Java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.Java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.Java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.Java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.Java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.Java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.Java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.Java:163)
at org.Eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.Java:50)
at org.Eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.Java:38)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:459)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:675)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.Java:382)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.Java:192)
Normalement, mon application fonctionnait sur le port 8080, mais dans le test, elle semblait fonctionner sur un port différent (même si je n'ai rien changé concernant les ports).
Pour moi, ce qui suit a résolu le problème:
Ajout de ceci à SpringBootTest:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Obtenir le port attribué (ne fonctionne pas sans le paramètre de port aléatoire):
@LocalServerPort
private int port;
Préfixer le test de repos avec le nouveau paramètre de port:
given().port(port).when().get...
OR
@Before
public void setUp() throws Exception {
RestAssured.port = port;
}
REST Assured effectue de véritables requêtes http et il semble que le serveur ne fonctionne pas (ou qu'il s'exécute sur un port différent de 8080 qui est le port par défaut que REST Assured utilise). Si vous ne le faites pas ' Si vous voulez faire de vraies requêtes http, vous pouvez consulter le module Spring MockMvc qui est sans doute plus facile à configurer.
J'ai eu le même problème et j'utilise OKHttp
au lieu de HttpClient
. Cela a contourné le problème pour moi.