Skip to content

Commit 2e3d7bf

Browse files
committed
Set up and tear down tests per class
1 parent cb05c29 commit 2e3d7bf

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

java-client/src/test/java/co/elastic/clients/base/UserAgentTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
import org.apache.http.HttpHost;
2424
import org.elasticsearch.client.RequestOptions;
2525
import org.elasticsearch.client.RestClient;
26+
import org.junit.AfterClass;
27+
import org.junit.BeforeClass;
2628
import org.junit.Test;
2729

30+
import java.io.IOException;
31+
2832
import static co.elastic.clients.base.UserAgent.DEFAULT_NAME;
2933
import static co.elastic.clients.base.UserAgent.DEFAULT_VERSION;
3034
import static org.junit.Assert.assertEquals;
@@ -41,32 +45,40 @@ public class UserAgentTest {
4145
private static final String CUSTOM_NAME_2 = "MegaClient";
4246
private static final String CUSTOM_VERSION_2 = "6.7.8";
4347

48+
public static RestClient restClient;
49+
50+
@BeforeClass
51+
public static void setUp() {
52+
restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
53+
}
54+
55+
@AfterClass
56+
public static void tearDown() throws IOException {
57+
restClient.close();
58+
}
59+
4460
@Test
4561
public void testDefaultUserAgent() throws Exception {
46-
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
4762
Transport transport = new RestClientTransport(restClient, null);
4863
assertEquals(DEFAULT_USER_AGENT, transport.userAgent());
4964
}
5065

5166
@Test
5267
public void testCustomUserAgent() throws Exception {
53-
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
5468
Transport transport = new RestClientTransport(restClient, null, null,
5569
new UserAgent(CUSTOM_NAME, CUSTOM_VERSION));
5670
assertEquals(CUSTOM_USER_AGENT, transport.userAgent());
5771
}
5872

5973
@Test
6074
public void testManualUserAgent() throws Exception {
61-
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
6275
Transport transport = new RestClientTransport(restClient, null,
6376
RequestOptions.DEFAULT.toBuilder().addHeader("User-Agent", CUSTOM_USER_AGENT).build());
6477
assertEquals(CUSTOM_USER_AGENT, transport.userAgent());
6578
}
6679

6780
@Test
6881
public void testMultipleUserAgentsThrowsException() throws Exception {
69-
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
7082
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
7183
Transport transport = new RestClientTransport(restClient, null,
7284
RequestOptions.DEFAULT.toBuilder().addHeader("User-Agent", CUSTOM_USER_AGENT).build(),

0 commit comments

Comments
 (0)