I want to fake a new type of class that doesnt exist which implements an interface
is this possible?
for example, i have an interface
public interface Worker {
boolean execute(Job c);
}
I have a class
public class CompositeWorker implements Worker {
private List<Worker> childWorkers = new ArrayList<>();
public List<boolean> execute(Job j){
return childWorkers.stream().map(w -> w.execute(j)).collect(Collectors.toList());
}
public addWorker(Worker worker) {
childWorkers.add(worker)
}
}
To test:
public class CompositeWorkerTest {
private CompositeWorker worker;
@BeforeMethod
void setup() {
MockitoAnnotations.initMocks(this);
worker = new CompositeWorker();
}
@Test
test_worker() {
Worker worker_A = mock(Worker.class);
Worker worker_A = mock(Worker.class);
Job job = mock(Job.class);
when(worker_A.execute(job)).thenReturn(true);
when(worker_B.execute(job)).thenReturn(true);
worker.add(worker_A);
worker.add(worker_b);
List<boolean> results = worker.execute(job);
//assert results contains true
}
is there a way to fake/change Worker worker_A = mock(Worker.class); to FakeWorker fakeWorker = mock(FakeWorker.class) which implements Worker but it doesnt actually exists
by NOT exists, i mean there is no definition for:
public class FakeWorker implement Worker
the reason why I want to mock a different class is because Worker Type is unique
public addWorker(Worker worker) {
childWorkers = childWorkers.stream().filter(w -> w.getClass() != worker.getClass())
.collect(Collectors.toList());
childWorkers.add(worker)
}
Aucun commentaire:
Enregistrer un commentaire