Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Last active May 10, 2017 15:29
Show Gist options
  • Save mauricioaniche/a65b6bfa5154ad9f85235021e0872e64 to your computer and use it in GitHub Desktop.
Save mauricioaniche/a65b6bfa5154ad9f85235021e0872e64 to your computer and use it in GitHub Desktop.
package nl.tudelft.mocks2;
import java.util.ArrayList;
import java.util.List;
public class InvoiceFilter {
private InvoiceDao invoiceDao;
private Webservice webservice;
public InvoiceFilter(InvoiceDao invoiceDao, Webservice webservice) {
this.invoiceDao = invoiceDao;
this.webservice = webservice;
}
public List<Invoice> filter() {
List<Invoice> filtered = new ArrayList<>();
List<Invoice> allInvoices = invoiceDao.all();
for(Invoice inv : allInvoices) {
if(inv.getValue() < 100.0) {
filtered.add(inv);
webservice.send(inv);
}
}
return filtered;
}
}
package nl.tudelft.mocks2;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import javax.xml.ws.WebEndpoint;
import java.util.Arrays;
import java.util.List;
public class InvoiceFilterTest {
@Test
void filterInvoices() {
InvoiceDao dao = mock(InvoiceDao.class);
Webservice webservice = mock(Webservice.class);
Invoice mauricio = new Invoice("Mauricio", 20.0);
Invoice arie = new Invoice("Arie", 300.0);
List<Invoice> fakeInvoices = Arrays.asList(mauricio, arie);
when(dao.all()).thenReturn(fakeInvoices);
InvoiceFilter filter = new InvoiceFilter(dao, webservice);
List<Invoice> result = filter.filter();
assertThat(result).containsExactly(mauricio);
}
@Test
void sendToWebservice() {
InvoiceDao dao = mock(InvoiceDao.class);
Webservice webservice = mock(Webservice.class);
Invoice mauricio = new Invoice("Mauricio", 20.0);
Invoice arie = new Invoice("Arie", 300.0);
List<Invoice> fakeInvoices = Arrays.asList(mauricio, arie);
when(dao.all()).thenReturn(fakeInvoices);
InvoiceFilter filter = new InvoiceFilter(dao, webservice);
List<Invoice> result = filter.filter();
verify(webservice).send(mauricio);
verify(webservice, never()).send(arie);
}
}
package nl.tudelft.mocks2;
public interface Webservice {
void send(Invoice invoice);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment