I'm using spring, hibernate and junit to setup a test class for some of my models.
All of my models extend BaseModel
I have the following function in my testing class:
void printValidation(Set<ConstraintViolation<? extends BaseModel>> v){
Iterator<ConstraintViolation<? extends BaseModel>> it = v.iterator();
while (it.hasNext()) {
ConstraintViolation<? extends BaseModel> violation = it.next();
l.info("Validation error {}", violation);
}
}
I use it like so inside a test case:
eModel = new Entry(); //This class extends BaseModel
Set<ConstraintViolation<? extends BaseModel>> v = validator.validate(eModel);
assertTrue("There should be validation errors...", v.size() > 0);
printValidation(v);
However, I get the following message in Eclipse, and it does not seem to work: Type mismatch: cannot convert from Set<ConstraintViolation<Entry>>
to Set<ConstraintViolation<? extends BaseModel>>
Set<ConstraintViolation<? extends BaseModel>>
I have a bunch of other tests, and if I make the function take <Entry>
instead of <? extends BaseEntry>
<? extends BaseEntry>
it works. I have a number of models I'm testing like this, and it is sort of the whole point of generics to not have to duplicate this.
What am I missing here, I guess it has something to do with either what validator.validate is returning or something?
** Edited to fix inline code formatting **
@geoand - Almost, here is the working code:
<T extends BaseModel> void printValidation(Set<ConstraintViolation<T>> v){
Iterator<ConstraintViolation<T>> it = v.iterator();
while (it.hasNext()) {
ConstraintViolation<T> violation = it.next();
l.info("Validation error {}", violation);
}
}
And the function that does the test:
@Test
public void testEntry() {
Set<ConstraintViolation<Entry>> v = validator.validate(eModel);
assertTrue("There should be validation errors...", v.size() > 0);
printValidation(v);
尝试void <T extends BaseModel> printValidation(Set<ConstraintViolation<T>> v)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.