How to run a test jersey container (Grizzly) once for all tests in a test class?

I am working on fixing integration tests in one of the projects. Currently, all integration test classes extend the JerseyTest class. After going through the JerseyTest class , I realized that it starts and stops the container for each test method using Junit Before and After comments.

Why is this necessary? Isn't that enough if we lift the container once, run the tests and close it at the end?

We also use Spring, and it takes time to initialize the context.

Prior to Junit4, we worked on this limitation, processing it manually using logical flags.

@Before
public void setup() {
  if(!containerStarted) {
   // start
   containerStarted = true;    
  }
 // else do nothing
}
+4
2

@BeforeClass @AfterClass JerseyTest @Before @After . :

public abstract class MyJerseyTest {
  private JerseyTest jerseyTest;
  public MyJerseyTest(){

  }

  @BeforeClass
  public void setUp() throws Exception {
    initJerseyTest() 
    jerseyTest.setUp();
  }

  @AfterClass
  public void tearDown() throws Exception {
    jerseyTest.tearDown();
  }

  private void initJerseyTest() {
    jerseyTest = new JerseyTest() {
      @Override
      protected Application configure() {
        // do somthing like
        enable(...);
        set(...);

        // create ResourceConfig instance
        ResourceConfig rc = new ResourceConfig();

        // do somthing like
        rc.property(...);
        rc.register(...);

        return rc;
      }
    };
  }
}

, .

+2

, spring (jersey- spring -bridge). JerseyTest , . , , spring beans .

?

, :

HttpServer

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class OrderResourceTest {
    ...
    public static final String BASE_URI = "http://localhost:8989/";
    private static HttpServer server = null;
    ...
    ...
}

, JerseyTest, / . @Before @AfterClass . @Before server , / web.xml (, web.xml server)

@Before
public void setUp() throws Exception {
    if (server == null) {
        System.out.println("Initializing an instance of Grizzly Container");
        final ResourceConfig rc = new ResourceConfig(A.class, B.class);

        WebappContext ctx = new WebappContext() {};
        ctx.addContextInitParameter("contextConfigLocation", "classpath:applicationContext.xml");

        ctx.addListener("com.package.something.AServletContextListener");

        server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
            ctx.deploy(server);
        }
    }

, @Before, if @BeforeClass. , @BeforeClass, , , - if. , , .

  • ResourceConfig , , Resource/Controller, ExceptionMapper, , , .
  • WebappContext, web.xml, applicationContext. applicationContext spring - .
  • web.xml, , .
  • - - , ctx.
  • server Grizzly, URI ResourceConfig
  • WebappContext, , web.xml deploy . WebappContext server.

, Grizzly web.xml spring - , .

@AfterClass

@AfterClass
    public static void tearDown() throws Exception {
        System.out.println("tearDown called ...");
        if (server != null && server.isStarted()) {
            System.out.println("Shutting down the initialized Grizzly instance");
            server.shutdownNow();
        }
    }

REST-

@Test
public void testGetAllOrderrs() { 
    List<Orders> orders= (List<Orders>) 
    when().
        get("/orders").
    then().
        statusCode(200).
    extract().
        response().body().as(List.class);

    assertThat(orders.size()).isGreaterThan(0);
}

, , REST,

RestAssured.baseURI = "http://localhost:8989/";

REST-assured,

@Test
public void testGetAllOrders() {
    Client client = ClientBuilder.newBuilder().newClient();
    WebTarget target = client.target(BASE_URI);
    Response response = target
            .path("/orders")
            .request(MediaType.APPLICATION_JSON)
            .get();

    assertThat(response.getStatus()).isEqualTo(200);

    JSONArray result = new JSONArray(response.readEntity(String.class));

    assertThat(result.length()).isGreaterThan(0);
}

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
+2

Source: https://habr.com/ru/post/1648767/


All Articles