I have a class which handles when a CardView's drag gets started.

I've been looking at tutorials for JUnit and I can't seem to find out how to start testing this method.

public class DragDetectedHandler implements EventHandler<MouseEvent> {
private Stacks stacks;
private NextCards nextCards;
private Table gameTable;

public DragDetectedHandler(Stacks stacks, NextCards nextCards, Table gameTable) {
    this.stacks = stacks;
    this.nextCards = nextCards;
    this.gameTable = gameTable;

public void handle(MouseEvent mouseEvent) {
    CardPane source = (CardPane) mouseEvent.getSource();
    Dragboard db = source.startDragAndDrop(TransferMode.MOVE);
    ClipboardContent cc = new ClipboardContent();

    //IF from stacks or nextCards, we only need to deal with dragging 1 card
    if (stacks.has(source) || nextCards.has(source) || gameTable.getColumns()[gameTable.getColumnIndex(source)].hasNextShown(source)) {
    } else {
        //Else we need to copy the whole stack to the dragboard
        Integer xy[] = gameTable.getXY(source);
        if(xy != null){
            Image ghostStack = gameTable.getStackImage(xy[0], xy[1]);





So far I have come up with this:

class DragDetectedHandlerTest {

void handle() {
    StackPane stackPane = new StackPane();
    PatienceController pc = new PatienceController();

    Stacks stacks = new Stacks();
    NextCards nextCards = new NextCards(stackPane, pc);
    Table table = new Table(pc);

    DragDetectedHandler handler = new DragDetectedHandler(stacks,nextCards, table);



Problem is I don't know what to assert for or how to get there. Any help is appreciated.


The first thing to consider is that you're going to need multiple tests to cover this unit.

Here's how I'd approach it:

  • Each defined condition gets a test: so there'll be a test method for dragging from stack; one for dragging from the next cards, one for dragging from the next cards column, and one for none of the above.
  • Each test method needs to be split into at least two test methods: one to assert that the correct number of cards are moved, and one to assert that the state of the board after the move is as expected.
  • Each object the method uses needs to be instantiated with a mock instance rather than the actual object so that you're not pulling dependencies into your tests.
  • If the code you're testing is not using interfaces to provide the functionality, you'll need to manually create your mock object instances with enough information to exercise your tests.
  • You will also need to create a mock mouse event with the exact properties you need.

With all that configured, I'd structure my tests this way:

void DragFromStacksMovesOneCard()
     // Create the mouse event with the appropriate properties
     // Use helper methods to instantiate the mock objects
     // This includes instantiating the clipboard content

     // call handle(mouseEvent) with the newly created event

     // Assert that the number of cards in the clipboard content has increased by one.

And so on.

  • Thanks, this is really helpful. Now I know where to start.
    – Vinagy
    Aug 1 '17 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.