Question regarding Prog2.tests.tetris.eval.ObserverTest::testRemoveObserver

Hello,

since I fail this test I would like to ask why, because I implement the correct behavior according to this post:

This is an excerpt from my implementation of TetrisGame. I assume this is fine to post as it contains nothing that isn’t boilerplate:

public class TetrisGameImplementation implements TetrisGame {
    
    ...

    private Set<GameObserver> observers;

    public TetrisGameImplementation(... arguments ...) {
        ...
        observers = new HashSet<>();
        ...
    }

    @Override
    public void addObserver(GameObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(GameObserver observer) {
        observers.remove(observer);
    }

    ...

Does this not comply with the post linked above? I would argue that as long as an observer doesn’t override hashCode or equals in a way that breaks the contract then this should be guaranteed to produce the desired behavior.

The observer code looks and behaves correctly.

The problem is with your implementation of newPiece.
Your method throws an exception in case the current piece is not null which is not specified behavior.

1 Like