Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/472463237292642304
last update to make it ocmpile correctly
Source Link
skiwi
  • 10.6k
  • 6
  • 44
  • 108
@Ignore
public abstract class PlayerActionAbstractTest {
    static {
        assertTrue(true);
    }

    private final Supplier<PlayerAction> playerActionSupplier;

    private PlayerAction playerAction;

    public PlayerActionAbstractTest() {
        this.playerActionSupplier = null;
    }

    protected PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier) {
        this.playerActionSupplier = Objects.requireNonNull(playerActionSupplier, "playerActionSupplier");
    }

    @Before
    privatefinal public void beforebeforePlayerActionAbstractTest() {
        playerAction = playerActionSupplier.get();
    }

    @Test(expected = NullPointerException.class)
    public void testIsActionAllowedNullPlayer() {
        playerAction.isActionAllowed(null);
    }

    @Test(expected = NullPointerException.class)
    public void testPerformActionNullPlayer() {
        playerAction.performAction(null);
    }
}

The pros are that the instance only needs to be passed in once and most importantly that you must pass it in or you will get a compiler error. Another pro is that the list of classes to be tested is not defined in the abstract testclass itself.

Last update, appereantly the latest change was showing weird behaviours when executing unit tests on my whole project, rather than a single file. I have changed the following:

  1. Changed signature in PlayerActionAbstractTest from private void before() to final public void beforePlayerActionAbstractTest(), as the @Before method needs to be public and in case inheriting multiple times is possible, beforeSuper() would clash.
  2. Changed the public PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier) constructor to protected PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier), to fix that a test class must only have one public zero-args constructor.
  3. Added public PlayerActionAbstractTest() to make it have one public zero-args constructor.
  4. Added @Ignore to ensure that this abstract class itself isn't being tested.
public abstract class PlayerActionAbstractTest {
    private final Supplier<PlayerAction> playerActionSupplier;

    private PlayerAction playerAction;

    public PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier) {
        this.playerActionSupplier = Objects.requireNonNull(playerActionSupplier, "playerActionSupplier");
    }

    @Before
    private void before() {
        playerAction = playerActionSupplier.get();
    }

    @Test(expected = NullPointerException.class)
    public void testIsActionAllowedNullPlayer() {
        playerAction.isActionAllowed(null);
    }

    @Test(expected = NullPointerException.class)
    public void testPerformActionNullPlayer() {
        playerAction.performAction(null);
    }
}

The pros are that the instance only needs to be passed in once and most importantly that you must pass it in or you will get a compiler error.

@Ignore
public abstract class PlayerActionAbstractTest {
    static {
        assertTrue(true);
    }

    private final Supplier<PlayerAction> playerActionSupplier;

    private PlayerAction playerAction;

    public PlayerActionAbstractTest() {
        this.playerActionSupplier = null;
    }

    protected PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier) {
        this.playerActionSupplier = Objects.requireNonNull(playerActionSupplier, "playerActionSupplier");
    }

    @Before
    final public void beforePlayerActionAbstractTest() {
        playerAction = playerActionSupplier.get();
    }

    @Test(expected = NullPointerException.class)
    public void testIsActionAllowedNullPlayer() {
        playerAction.isActionAllowed(null);
    }

    @Test(expected = NullPointerException.class)
    public void testPerformActionNullPlayer() {
        playerAction.performAction(null);
    }
}

The pros are that the instance only needs to be passed in once and most importantly that you must pass it in or you will get a compiler error. Another pro is that the list of classes to be tested is not defined in the abstract testclass itself.

Last update, appereantly the latest change was showing weird behaviours when executing unit tests on my whole project, rather than a single file. I have changed the following:

  1. Changed signature in PlayerActionAbstractTest from private void before() to final public void beforePlayerActionAbstractTest(), as the @Before method needs to be public and in case inheriting multiple times is possible, beforeSuper() would clash.
  2. Changed the public PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier) constructor to protected PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier), to fix that a test class must only have one public zero-args constructor.
  3. Added public PlayerActionAbstractTest() to make it have one public zero-args constructor.
  4. Added @Ignore to ensure that this abstract class itself isn't being tested.
added second small update + actually changed it now
Source Link
skiwi
  • 10.6k
  • 6
  • 44
  • 108
public abstract class PlayerActionAbstractTest {
    private final Supplier<PlayerAction> playerActionSupplier;

    private PlayerAction playerAction;

    public PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier) {
        this.playerActionSupplier = Objects.requireNonNull(playerActionSupplier, "playerActionSupplier");
    }

    @Before
    publicprivate void superBeforebefore() {
        playerAction = playerActionSupplier.get();
    }

    @Test(expected = NullPointerException.class)
    public void testIsActionAllowedNullPlayer() {
        playerAction.isActionAllowed(null);
    }

    @Test(expected = NullPointerException.class)
    public void testPerformActionNullPlayer() {
        playerAction.performAction(null);
    }
}

Does this seem to be a reasonable way to do it?

Small update, changed signature in PlayerActionAbstractTest from public void superBefore() to final public void superBefore() to ensure that a compiler errors, rather than warning, will be thrown on an accidental override.

Second small update, changed signature in PlayerActionAbstractTest from final public void superBefore() to private void before(), now it cannot be overridden anymore.

public abstract class PlayerActionAbstractTest {
    private final Supplier<PlayerAction> playerActionSupplier;

    private PlayerAction playerAction;

    public PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier) {
        this.playerActionSupplier = Objects.requireNonNull(playerActionSupplier, "playerActionSupplier");
    }

    @Before
    public void superBefore() {
        playerAction = playerActionSupplier.get();
    }

    @Test(expected = NullPointerException.class)
    public void testIsActionAllowedNullPlayer() {
        playerAction.isActionAllowed(null);
    }

    @Test(expected = NullPointerException.class)
    public void testPerformActionNullPlayer() {
        playerAction.performAction(null);
    }
}

Does this seem to be a reasonable way to do it?

public abstract class PlayerActionAbstractTest {
    private final Supplier<PlayerAction> playerActionSupplier;

    private PlayerAction playerAction;

    public PlayerActionAbstractTest(final Supplier<PlayerAction> playerActionSupplier) {
        this.playerActionSupplier = Objects.requireNonNull(playerActionSupplier, "playerActionSupplier");
    }

    @Before
    private void before() {
        playerAction = playerActionSupplier.get();
    }

    @Test(expected = NullPointerException.class)
    public void testIsActionAllowedNullPlayer() {
        playerAction.isActionAllowed(null);
    }

    @Test(expected = NullPointerException.class)
    public void testPerformActionNullPlayer() {
        playerAction.performAction(null);
    }
}

Does this seem to be a reasonable way to do it?

Small update, changed signature in PlayerActionAbstractTest from public void superBefore() to final public void superBefore() to ensure that a compiler errors, rather than warning, will be thrown on an accidental override.

Second small update, changed signature in PlayerActionAbstractTest from final public void superBefore() to private void before(), now it cannot be overridden anymore.

fixed typo
Source Link
skiwi
  • 10.6k
  • 6
  • 44
  • 108

I have been thinking quite som eitmesome time and asked an StackOverflow question about extending abstract test classes, but I haven't been able to do it until now.

I have been thinking quite som eitme and asked an StackOverflow question about extending abstract test classes, but I haven't been able to do it until now.

I have been thinking quite some time and asked an StackOverflow question about extending abstract test classes, but I haven't been able to do it until now.

Source Link
skiwi
  • 10.6k
  • 6
  • 44
  • 108
Loading