Work

1)Search directories recursively for file in java(File Search in current directory)See below link

 http://www.mkyong.com/java/search-directories-recursively-for-file-in-java/







Mocking Service and dao layers using mokito Testing framework






public class CommentDaoImplTest {

static final Logger log = LoggerFactory.getLogger(CommentDaoImplTest.class);
@Mock
private CommentDao commentDao;
@Mock
CommentDaoImpl commentDaoImplMock;
@Mock
MongoTemplate mongoTemplateMock;
@Before
public void setUp() {
log.debug("Initilizing before each test case is executed:: CommentDaoImplTest");
MockitoAnnotations.initMocks(this);
}

@Test
public void testCreateComment() {
log.debug("Entered into testCreateComment::CommentDaoImplTest");
Comment comment=new Comment();
setCommentData(comment);
commentDao.createComment(comment);
Mockito.verify(commentDao).createComment(comment);
Assert.assertNotNull(comment);
Assert.assertEquals("55ffb395e030194dbdc17edc", comment.getCardInstanceId());
Assert.assertEquals("55ffb395e030194dbdc17edd", comment.getId());
Assert.assertEquals("9999999", comment.getCommentData().get(0).getCreatorUserId());
Assert.assertEquals("first comment", comment.getCommentData().get(0).getText());
}


@Test
public void testFetchComments() {
log.debug("Entered into testFetchComments::CommentDaoImplTest");
String cardInstanceId="";
List<CommentData> commentList=new ArrayList<CommentData>();
CommentData commentData=new CommentData();
commentData.setCreatorUserId("9999999");
commentData.setText("first comment");
commentData.setTimeStamp(78845894l);
commentList.add(commentData);
Mockito.when(commentDao.fetchComments(cardInstanceId)).thenReturn(commentList);
commentDao.fetchComments(cardInstanceId);
Mockito.verify(commentDao).fetchComments(cardInstanceId);
List<CommentData> result=commentDao.fetchComments(cardInstanceId);
Assert.assertEquals(commentList, result);
}

Rest Assured test cases for Get and post methods




1)Test case for rest full service get methods using Rest Assured Framework and Junit 


public class Test{

@Before
public void setUp(){
RestAssured.baseURI = "http://localhost";
RestAssured.port = 9090;
RestAssured.basePath = "api/business";
}
@Test
public void testGetBusiness(){
String searchValue = "Dental";
//Invoking rest web service
String APIUrl1 = RestAssured.baseURI+"/"+RestAssured.basePath+"/search"+"/"+searchValue;
Response response =RestAssured.get(APIUrl1);
JsonPath regJsonPath = new JsonPath(response.asString());
//Checking response data
assertEquals(200, response.getStatusCode());
assertEquals(AppConstants.SUCCESS, regJsonPath.getJsonObject("statusMessage"));
assertNotNull(regJsonPath.getJsonObject("data.id"));
assertNotNull(regJsonPath.getJsonObject("data.businessId"));
assertNotNull(regJsonPath.getJsonObject("data.logoUrl"));
assertNotNull(regJsonPath.getJsonObject("data.layoutScheme.primaryColor"));
assertNotNull(regJsonPath.getJsonObject("data.layoutScheme.secondaryColor"));
}
}


2)Test case for rest full service post methods using Rest Assured Framework and Junit 


@Test
public void testSmilineProcessCardAction() throws Exception{
log.debug("Entered into testSubmitActionRequest");
String APIUrl = RestAssured.baseURI+"/"+RestAssured.basePath+"/cardaction/submit";
ObjectMapper mapper = new ObjectMapper();
SubmitActionRequest submitActionRequest = null;
//Here fetch the smiline registration service json request.
submitActionRequest =  mapper.readValue(getClass().getClassLoader().getResourceAsStream("test/smiline/smiline_registration.json"), SubmitActionRequest.class);
String submitActionRequestString = mapper.writeValueAsString(submitActionRequest);

// set the smiline registration request parameters
RequestSpecBuilder smilineBuilderBuilder = new RequestSpecBuilder();
smilineBuilderBuilder.setBody(submitActionRequestString);
smilineBuilderBuilder.setContentType("application/json; charset=UTF-8");
smilineBuilderBuilder.addHeader("x-username", "99999999999");

RequestSpecification requestSpec = smilineBuilderBuilder.build(); 
Response simlineRegResponse = given().spec(requestSpec).when().post(APIUrl);
JsonPath regJsonPath = new JsonPath(simlineRegResponse.asString());

assertEquals(200, simlineRegResponse.getStatusCode());
assertEquals(AppConstants.SUCCESS, regJsonPath.get("statusMessage"));
assertNotNull(regJsonPath.getJsonObject("cookie"));
String customerId = regJsonPath.getJsonObject("cookie.customerId");
String refererServiceId = regJsonPath.getJsonObject("cookie.refererServiceId");
String firstCardObjId = regJsonPath.getJsonObject("serverDirectives.redirectCardId");
assertNotNull(customerId);
assertNotNull(refererServiceId);
assertNotNull(firstCardObjId);
}

No comments:

Post a Comment