This site requires JavaScript, please enable it in your browser!
Greenfoot back
vked_coder
vked_coder wrote ...

2014/9/10

[Urgent] How to use Test Driven Development for Greenfoot - Greenfoot Noob

vked_coder vked_coder

2014/9/10

#
Hi, I am new to the Greenfoot IDE and want to write a game using Test Driven Development principles. I have tried searching for any resources for Greenfoot related testing but have not found many. Using TDD, I would like to test the behaviour and control flow of my objects. Can someone please guide me about such a workflow and the tools required on top of Greenfoot. Please post your thoughts as soon as possible as I have to deliver the code to my repository by later tonight. I appreciate your help. Sincerely vked_coder
lordhershey lordhershey

2014/9/10

#
Interesting, is this a homework assignment? What were you given to use if this was an assignment. I am not sure how to use asserts for things that are non-trivial. How would you write test cases for things that take simple inputs and outputs. I guess you might try JUnit and Eclipse. Found this: http://technologyconversations.com/2013/12/20/test-driven-development-tdd-example-walkthrough/
danpost danpost

2014/9/10

#
From what I understand, TDD is a programming technique; so, the fact that you are using the Greenfoot IDE is not relevant. You should be looking for 'using TDD with Java', since Java is the language you would be programming in.
vked_coder vked_coder

2014/9/11

#
Hi! Thanks @lordhershey and @danpost for the replies. Ok, so from what I can understand, JUnit is a tool for unit testing and writing assertions, right? Now how do I use it with Greenfoot IDE to create a Test-Fail-Refactor loop for handling exceptions related to my objects? And yes @danpost, I understand that TDD with Java should be my focus. But I'm a bit confused as to how I should bring TDD in my Greenfoot development workflow? Can both of you and anyone else shed some light on this? Thanks!
danpost danpost

2014/9/11

#
Well, greenfoot will warn you on compilation errors (if something is missing in your code, etc). But, does not have the functionality to create missing methods or create missing classes for you. You could create a 'Tester' class as a subclass of Actor to put your test code in, giving it any image you wish; and instead of using the 'org.junit' assertEquals method, just print the 'pass'/'fail' results to the terminal window. However, it is easier to add and remove a print statement directly in the code that is to be tested. If you would rather still go with more what you asked for, I created a sample Tester class which looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import greenfoot.*;
 
public class Tester extends Actor
{
    public Tester() {
        // creates image for tester
        GreenfootImage image = new GreenfootImage(20, 20);
        image.fill();
        image.setColor(java.awt.Color.lightGray);
        image.fillRect(3, 3, 14, 14);
        GreenfootImage let = new GreenfootImage("T", 16, null, null);
        image.drawImage(let, 10-let.getWidth()/2, 10-let.getHeight()/2);
        setImage(image);
    }
     
    /** tests calculator add */
    public void testCalCulatorAdd() {
        int x = 5;
        int y = 2;
        int expAns = 7;
        Calculator calculator = new Calculator();
        int ans = calculator.add(x, y);
        boolean result = (expAns == ans);
        print("calculator.add", result);
    }
     
    /** tests calculator subtract */
    public void testCalculatorSubtract() {
        int x = 6;
        int y = 4;
        int expAns = 2;
        Calculator calculator = new Calculator();
        int ans = calculator.subtract(x, y);
        boolean result = (expAns == ans);
        print("calculator.subtract", result);
    }
     
    // add new test methods here
     
    /** performs all tests */
    public void testAll() {
        testCalCulatorAdd();
        testCalculatorSubtract();
        // add a call to each new test method here
    }
     
    /** outputs result of any test */
    private void print(String text, boolean result) {
        System.out.println(text+": "+(result ? "passed" : "failed"));
    }
}
You can right click on the Tester class icon and select 'new Tester()', click anywhere in your world, right-click on the Tester object placed in the world and select any test method to execute.
vked_coder vked_coder

2014/9/14

#
This gave me a headstart and is really helpful of u @danpost. I might be able to take this ahead from here. The Tester class would essentially include all my behaviour related methods too. I'm plain curious, do the makers of Greenfoot plan to add any TDD functionality in Greenfoot in the future?
danpost danpost

2014/9/14

#
Maybe adding functionality to add methods and classes automatically when double-clicking on an undefined name might help. The compiler will point out anything missing and you can always use 'System.out.print' to put results of tests to the terminal. It would not be a bad idea to add the aforementioned functionality; but, do not hold your breath (it would only save re-typing the name of the class; unless it put in the arguments as well). Also, this site is designed for beginners, and adding TDD would take away from the benefit of one coding the classes or methods themselves. I myself do not see much benefit in adding it.
You need to login to post a reply.