Back
Close

Computing with Data

elgeish
35.3K views

Mocking

Here's an example of mocking in Java:

import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.collections.Lists;
import java.util.ArrayList;
import java.util.List;
interface DataLayer {
void insertReminder(final Reminder reminder);
Reminder getReminder(final String reminderID);
void updateReminder(
final String reminderID, final Reminder reminder);
void deleteReminder(final String reminderID);
}
class Reminder {
// Reminder fields go here
}
class Orchestrator {
private final DataLayer dataLayer;
public Orchestrator(final DataLayer dataLayer) {
this.dataLayer = dataLayer;
}
public void addReminder(final Reminder reminder) {
// Validation and pre-processing code goes here
dataLayer.insertReminder(reminder);
// ...
}
}
public class OrchestratorTest {
@Test
public void testAddReminder() throws Exception {
final Reminder reminderToAdd = new Reminder();
final List<String> methodsCalled = new ArrayList<>();
final Orchestrator orchestrator =
new Orchestrator(new DataLayer() {
@Override
public void insertReminder(final Reminder reminder) {
methodsCalled.add("dataLayer.insertReminder");
Assert.assertSame(
reminder,
reminderToAdd,
"reminder mismatch in dataLayer.insertReminder");
}
@Override
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants