I have recently started to use Spring Data MongoDB and I wonder if there is any way to avoid writing entities' attributes explicitly as they are stored in the database. For example, given the following class representing a MongoDB collection:
public class Employee {
#Id
public String id;
private double salary;
...
}
If I want to make a query using MongoTemplate like:
public List findEmployeeBySalaryRange(double salary) {
Query query = new Query();
query.addCriteria(Criteria.where("salary").lt(salary));
...
}
I would like to avoid writing "salary", since that will make the code harder to maintain in the future in case the field name changes. I am thinking of something like getting the field name from the class attribute, but I am not quite sure how. Is there a way to do it? I have looked into the documentation but did not find anything related unless I missed it.
Thanks in advance.
You may create a Utility Class to store all database field names, use #Field annotation on field with constant from that class and use that constant in query to avoid error prone hardcoded Strings.
In Employee Model
#Field(DbFields.SALARY)
private double salary;
In Query,
query.addCriteria(Criteria.where(DbFields.SALARY).lt(salary));
In DbFields Utility class
public static final String SALARY = "salary";
// I have this method in one class
public class cls1 {
public void enterZip1(String zip1Value){
driver.findElement(zip1).sendKeys(zip1Value);
}
}
// I have below snippet in another class.
public class cls2{
Cell zip1Col = row.getCell(zip1ColNo);
String zip1Value = zip1Col.getStringCellValue();
cls1.enterZip1(zip1Value);
}
When I run my selenium webdriver script using Eclipse, I get this error:
java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1077)
at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:395)
at org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:347)
at com.carem.ss.dd(ss.java:215)
..........
Error is pointing to :
String zip1Value = zip1Col.getStringCellValue();
Data is coming from excel file. I have zip1 column in excel file. Zip1 cells have only numeric value. I know what causing the error but I am not able to fix it. Excel cell has numeric value but I declared a string variable. I tried to change from
String zip1Value = zip1Col.getStringCellValue();
to
String zip1Value = zip1Col.getNumericCellValue();
Then I got lost because eclipse is recommending to make other changes including the above method. Excel will have always numeric value. How can I modify my code so it will accept the numeric value?
Update
enter image description here
Referring to your comments, and based upon the screenshot you gave me, the error specifically says that the value is a double. Double is a kind of numeric value. I originally incorrectly assumed that the value was an Integer (numeric) which is why my first suggestion did not work, but the following should work:
String zip1Value = Double.toString(zip1Col.getNumericCellValue());
Imported this:
import org.apache.poi.ss.util.NumberToTextConverter;
Added this:
String zip1Value = NumberToTextConverter.toText(zip1Col.getNumericCellValue());
It worked.
Im using DataProvider in TestNG for my Selenium Scripts . My requirement is to just use a single DataProvider and pass the data to many test methods .
For example : Say i have 10 test methods , So i need to create a Single DataProvider , so that it can pass data to all those 10 Test methods.
Is it possible to do it ? If yes , how to implement it .
Or is there any alternative for this ??
Pl Help !!!
If each of your test method has #Test annotation, then you can simply add parameter to this as -
#Test(dataProvider="Name of your DataProvider")
You can do this with all of the 10 test methods & this will make them get data from your single DataProvider.
I hope it helps. . .cheers!!
Yes it is possible.
So your data provider needs to know for which method or class it is providing the data. I made the following implementation. So you can get the context of the calling method in a data provider and you can ask it what is the parent class name for which the data has to be provided, and then depending on that you can have multiple files which you can read and supply the data or have different rows in the same csv differentiated by class name from where you can read the required row
#DataProvider(name="getDataFromFile")
public static Iterator<Object[]> getDataFromFile(Method testMethod) throws Exception
{
String expected=null;
String className=testMethod.getDeclaringClass().getSimpleName();
Reporter.log("Providing data for class " + className,true);
List<Map<String, String>> setupData = getTestDataFromCsv(classname);
//provide data here
}
Update on this:
I was looking for a solution for the same. But it is not possible to split the data provider. But no harm in reusing the data provider for all methods, the disadvantage is each method must use the complete list of arguments. All other options are more complex to implement and maintain. For my scenario, it is better than creating and maintaining separate data providers for each test methods.
#BeforeMethod
public void setUp() {
init();
login= new LoginPage(myD);
clientsearch = new ClientSearchPage(myD);
toppanel= new TopPanelPage(myD);
}
#Test(dataProvider="search_data")
public void verifySearchByClientNumber(String clientnumber, String policynumber, String policynumberClient, String webreference,
String webreferenceClient, String surname, String surnameClient, String forename, String forenameClient, String dob, String dobClient){
login.Login();
log.info("Logged in successfully, now in ClientSearch Page..");
log.info("Entering client number.." );
clientsearch.enterClientNumber(clientnumber);
log.info("Clicking on the Search button ..." );
clientsearch.clickSearchButton();
log.info("Verifying Client present in results.." );
boolean res=clientsearch.isClientPresent(clientnumber);
Assert.assertEquals(res, true,"Assertion failed !!");
toppanel.clickLogoutButton();
}
#Test(dataProvider="search_data")
public void verifySearchByPolicyNumber(String clientnumber, String policynumber, String policynumberClient, String webreference,
String webreferenceClient, String surname, String surnameClient, String forename, String forenameClient, String dob, String dobClient){
login.Login();
log.info("Logged in successfully, now in ClientSearch Page..");
log.info("Entering Policy number.." );
clientsearch.enterPolicyNumber(policynumber);
log.info("Clicking on the Search button ..." );
clientsearch.clickSearchButton();
log.info("Verifying Client present in results.." );
boolean res=clientsearch.isClientPresent(policynumberClient);
Assert.assertEquals(res, true,"Assertion failed !!");
toppanel.clickLogoutButton();
}
//More methods here with same data provider....
#AfterMethod
public void endTest() {
myD.quit();
}
My WPF app using an XMLDataProvider for its data. The XML file has a
<RELEASEDATE>dd/mm/yyyy</RELEASEDATE>
for each of the listed items. I'm sorting the data in the app using a
Listbox1.Items.SortDescriptions.Add(new SortDescription("RELEASEDATE", ListSortDirection.Descending));
The results aren't what expected as the date is treated as a string.
What is the most elegant way around this? Can I somehow convert to a date inline?
You have to implement own IComparer:
class DateTimeComparer : IComparer
{
public int Compare(object x, object y)
{
//To Do : Implement DataTime Comparering
}
}
and now assign the IComparer implementation to the collection's ListCollectionView.CustomSort:
ListCollectionView view = new ListCollectionView(ListBox.Items);
view.CustomSort = new DateTimeComparer();
See similar Question
I am trying to pass a Long datatype array to one of my webservivce method.
My Webservice method is looks follows:
public String calculate(Long[] values)
{
//my code here to process the array
}
From the client side i am calling the webservice like follows
Long[] data=new Long[1];
data[0]=1;
proxy = webService.getClient(wsdlURL)
String response=proxy.calculate(data);
But it throwing me the exception javax.xml.bind.JAXBException: class [Ljava.lang.Long; nor any of its super class is known to this context.
Based on googling i understand this one because of JAXB unable to marshall the Long array. But i dont know how to fix it. Any help?
Try using ArrayList() on the client side:
List<Long> data = new ArrayList<Long>();
data.add(new Long(1));
data.add(new Long(5));
proxy = webService.getClient(wsdlURL)
String response=proxy.calculate(data);
Not sure, but maybe BigInteger would work for you? That seems to be the default mapping from xml integer to java type.