dimanche 12 septembre 2021

instantiate object from derived class with constructor that takes super argument

Maybe what I am trying to do is not worthwhile, it sure feels that way after spending many days on it.

I have A Base Class shown here:

package jimmy.kilmer.com;

import java.awt.Color;


import jarPackageImports.AI;
import jarPackageImports.MovementAction;
import jarPackageImports.Info;
import jarPackageImports.PlayerAction;

public class GameAI extends AI {

    public gameAI(Info info) {
        super(info);
        setJerseyNumber(32);
    }

    public Color getColor() {
        return Color.RED;
    }

    public String getName() {
        return "Usain Bolt";
    }

    public PlayerAction update() {

        // TODO game movement actions
        // all available methods not listed here...
        info.getVelocity();
        info.getX();
        info.getY();


    MovementAction steeringBehavior = null;

        return steeringBehavior;
    }

    //basically used for testing setup
    public int[][] populateAllPossibleNodes() {
            int[][] allPossibleNodes = new int[screenWidth/20][screenHeight/20];
            return allPossibleNodes;
    }
}

I have been given a jar, that sets up the game environment. It uses reflection for the setup. I am not familiar with reflection, unfortunately, as I am more beginner level.

I have read a lot about TDD, and am convinced that can help me stay orderly, and code in a disciplined way. I have some say that TDD is not really useful for Game development, which the arguments may be true, in regard to making an "enjoyable game." But, from a purely coding standpoint, I remain steadfast in my believe that TDD is the way to go. But, that remains to be seen, since it is still theoretical. I would like to try it.

I have installed Junit 5, and have done many tutorials, but it's all pretty basic examples. My particular test case uses reflection, super classes, derived classes, dynamic data. My head is spinning.

My goal is just to get setup such that I can start doing some Test driven development.

Here is my Junit test class:

package jimmy.kilmer.com;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import jarPackageImports.Info;

class GameAITest {

  private GameAITest AIObject;
  private jarPackageImports.Info info;

    @BeforeEach
    void setUp() throws Exception {
        AIObject = new GameAITest(info);


    @AfterEach
    void tearDown() throws Exception {
    }

        @Test
    void testPopulateAllPossibleNodes() {
        // 1. given/arrange
        int[][] array1 = new int[80][65];
        // 2. when/act
        int[][] array2 = AIObject.populateAllPossibleNodes();
        // 3. then/assert
        assertArrayEquals(array1, array2);
    }

}

That is my best stab so far, but it still get a compile error. Specifically:

java.lang.NullPointerException:Cannot invoke "jarPackageImports.Info.getScene()" because "this.info" is null

In summation:

  1. maybe everything I am trying is rubbish?
  2. Do I need to use dynamic junit testing? I would have to read up on that.
  3. Do I need to mock (use Mockito?) to instantiate an object to test? I would need to read up on that as well.
  4. Is it possible to instantiate an object from GameAI? Do I need to/how would I use relection to do that? class.getConstructors()? And, I would have to read up on that.

thanks in advance.





Aucun commentaire:

Enregistrer un commentaire