Let’s Make a Video Game! Part 1

Using a Testing Framework

JUnit is a test Framework that will run small parts of our code for us without having to run the whole program together.  Mostly it’s used for Testing functionality.  We’re going to abuse it a bit.  
Add the following  to your pom.xml after the properties element but before the close of the project element

   <dependencies>

       <dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <version>4.12</version>

           <scope>test</scope>

       </dependency>

   </dependencies>

Our pom.xml should look like this:

<?xml version=“1.0” encoding=“UTF-8”?>

<project xmlns=http://maven.apache.org/POM/4.0.0 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

        xsi:schemaLocation=http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd>

   <modelVersion>4.0.0</modelVersion>

   <groupId>com.drakos.invaders</groupId>

   <artifactId>DerpInvadersOne</artifactId>

   <version>1.0-SNAPSHOT</version>

   <packaging>jar</packaging>

   <properties>

       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

       <maven.compiler.source>1.8</maven.compiler.source>

       <maven.compiler.target>1.8</maven.compiler.target>

   </properties>

   <dependencies>

       <dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <version>4.12</version>

           <scope>test</scope>

       </dependency>

   </dependencies>

</project>

 

Highlight your test package by clicking on it then click File-> New File.  Select Unit Test, JUnit Test.

Name it something like IntegrationTest.

You’ll see some Method Annotations marked by @:

@BeforeClass – This runs once before the all tests are run

@AfterClass –  this runs once after all tests are run

@Before – this runs before every test

@After – this runs after every test

@Test – every method marked with this will run during a test.

Uncomment the @Test method named hello and between the {} brackets type:

System.out.println(“hello world”);

   @Test

   public void hello() {

       System.out.println(“hello world!”);

   }

 

Right click your test file and from the context menu select Test File.

Click the Output – Test tab in the lower window and you will see this:

Ok.  That was a lot.  Now it’s time to start making the game.

Author: Bruce Brown

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *