mardi 1 septembre 2015

Combine Mockito VerificationModes with JUnit Parameterized Tests?

Starting Point
I wanted to unit-test a class that basically doesn't create an output itself, but modifies an object that it receives. To be precise: it delegates to a service class, that creates an image that is appended to the object's imageList:

public class Class {
 //field declarations ...

 public Class(@Autowired Service service){
  this.service = service;
 }

 public Object process(Object object){
  //determine property here ...

  if(property == optionA){
   //the service will add the new image A to a list in object
   this.service.createImageA(object);
  } else if(property == optionB){
   //the service will add the new image B to a list in object
   this.service.createImageB(object);
  }

  //object will be returned, with or without a new image
  return object;
 }
}

The Work So Far
The best way - in my opinion - to test this class is:

  1. to check if the product returned is the same as the one assigned to the process method
  2. check how often both servicemethods were invoked (service is mocked with Mockito, of course ;) )

Now I would like to combine this with JUnit's ability to create Parameterized tests. Something analogous to:

@Parameters
public static List<Object[]> parameters() {
 return Arrays.asList(new Object[][] {
            {optionA, Mockito.times(1), Mockito.never()},
            {optionB, Mockito.never(), Mockito.times(1)},
            {optionC, Mockito.never(), Mockito.never()},
 });
}

Question
1. Is it possible to pass static functions within a parameterized test?
2. Are there special reasons not to do this?
3. Any known alternatives?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire