Junit test sample for REST API's using DropWizzard.

31 views
Skip to first unread message

Kuttaiah Robin

unread,
Jul 3, 2019, 4:57:34 AM7/3/19
to dropwizard-user
Hello all,

Am looking for a sample to write junit test case for DropWizzard.

For Oracle Helidon/J4C  I have something like below;

public class ModelResourceTest extends org.glassfish.jersey.test.JerseyTest {


 
@Override
 
protected org.glassfish.jersey.test.spi.TestContainerFactory getTestContainerFactory() {
   
return new  org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory();
 
}


 
@Override
 
protected Application configure() {
   
try {
     
MockitoAnnotations.initMocks(this);
     
MyMicroServiceClass microService = new MyMicroServiceClass();
     
ResourceConfig rc = Whitebox.<ResourceConfig>invokeMethod(microService, "createBlankResourceConfig");
      rc
.register(new AbstractBinder() {
       
@Override
       
protected void configure() {
          bind
(m_MyContext).to(MyContext.class);
       
}
     
});
     
//Registers all our REST resource in below.
      microService
.customizeResourceConfig(rc);
     
return rc;
   
} catch (Exception e) {
     
throw new RuntimeException(e);
   
}
 
}
 
 
 
@Test
 
public void testListModelsDraft() throws Exception {
   
ExecutionContextFactory factory = mock(ExecutionContextFactory.class);
   
Whitebox.setInternalState(ExecutionContextFactory.class, "INSTANCE", factory);


   
InsightDocumentStoreMetadataManager dsMetadataMrgMock = mock(InsightDocumentStoreMetadataManager.class);
   
Whitebox.setInternalState(InsightDocumentStoreMetadataManager.class, "INSTANCE", dsMetadataMrgMock);

   
ModelStatusDB modelStatusMockDB = mock(ModelStatusDB.class);
   
Whitebox.setInternalState(ModelStatusDB.class, "INSTANCE", modelStatusMockDB);
    given
(modelStatusMockDB.getModelStatus("OnlineShoppingApp","1.0")).willReturn(getModelStatus());

    given
(dsMetadataMrgMock.getMetadata(draftPath)).willReturn(getSearchDocumentResponse(true));
    given
(dsMetadataMrgMock.getDocument(draftPath+"OnlineShoppingApp/1.0/"+ProcMonMDSUtils.getBusinessContextFileName(), "1.0")).willReturn(getContextStream());
    given
(dsMetadataMrgMock.getDocument(draftPath+"OnlineShoppingApp/1.0/"+ProcMonMDSUtils.getBusinessContextMappingFileName(), "1.0")).willReturn(getMappingStream());


   
//call REST API
   
Response response = target("/models").request().get(Response.class);
   
System.out.println("Response testListModelsDraft->"+response);


    assertEquals
(Response.Status.OK.getStatusCode(), response.getStatus());


   
List<ModelInfo> results = response.readEntity(new GenericType<List<ModelInfo>>(){});


    assertEquals
(1, results.size());
   
ModelInfo modelInfo = results.get(0);
    assertNull
( modelInfo.getState());
    assertEquals
(modelInfo.getId(),"OnlineShoppingApp");
    assertNotNull
(modelInfo.getDraft());
    assertNull
(modelInfo.getCommitted());
 
}  
}


Could you recommend on how this can be achieved for DropWizzard.  I would like to tweak little above existing test case and make it work.

thanks
Robin Kuttaiah
Reply all
Reply to author
Forward
0 new messages