Results 1 to 4 of 4
  1. #1
    danboy4 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Junit Testing within local classes

    Hey there, I was wondering if anyone could possibly help me with writing jUnit tests for some of my code. I currently have one test calculating distance working fine. As some background, the code I need to write a test for moves a playerObject towards the closest barrier which it then changes colour when it touches. I'd like to write a jUnit test that checks to make sure that it's distance from the closest barrier is 0 and that the X and Y are changing. I would also like to write a new test to test if the playerObject changes colour when distance<delta. Here is my code:

    Java Code:
    if (playerObject.getX() < barriers[whereTo].getX()
    							&& !(playerObject.getX() > barriers[(int) whereTo].getX()))
    						playerObject.setX(playerObject.getX() + 0.30 * delta);
    
    					if (playerObject.getY() < barriers[whereTo].getY()
    							&& !(playerObject.getY() > barriers[(int) whereTo].getY()))
    						playerObject.setY(playerObject.getY() + 0.30 * delta);
    
    					if (playerObject.getX() > barriers[whereTo].getX()
    							&& !(playerObject.getX() < barriers[(int) whereTo].getX()))
    						playerObject.setX(playerObject.getX() - 0.30 * delta);
    
    					if (playerObject.getY() > barriers[whereTo].getY()
    							&& !(playerObject.getY() < barriers[(int) whereTo].getY()))
    						playerObject.setY(playerObject.getY() - 0.30 * delta);
    					
    					if (currentDist < (delta)) {
    						playerObject.setX(barriers[whereTo].getX());
    						playerObject.setY(barriers[whereTo].getY());
    						barriers[whereTo].setColour(0.1, 0.1, 0.1);
    						next = true;
    					}
    Last edited by danboy4; 10-08-2012 at 07:51 AM. Reason: Am I doing something wrong? I seem to be getting lots of views and no replies. I'm new please let me know what I can improve

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Junit Testing within local classes

    Well, you haven't actually asked anything, really.
    You haven't shown a unit test that you;re having problems with, just the code you want to test (and not even the method).

    Unit tests test methods...but there is no method there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    danboy4 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: Junit Testing within local classes

    Thanks for the reply Tolls,
    So would you advise that i create a function that includes the aforementioned code? I can then test that?

    I was under the impression i could test this using multiple tests but I don't know how to test inside a class inside a class.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Junit Testing within local classes

    Your tests are supposed to be testing that the interface does what it says it does.

    If you have a method that "takes two ints and returns the sum" then you write a test for that.

    You don't write tests for some random path through some bit of your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Junit testing, testing list<e> interface
    By mackavelirip in forum New To Java
    Replies: 0
    Last Post: 10-05-2011, 06:08 AM
  2. JUNIT testing assertEquals function
    By Drun in forum New To Java
    Replies: 7
    Last Post: 01-22-2010, 06:08 PM
  3. JUnit testing
    By hedonist in forum Advanced Java
    Replies: 4
    Last Post: 10-04-2009, 12:06 PM
  4. Replies: 1
    Last Post: 04-17-2008, 02:30 PM
  5. Testing JUnit PDE with Ant
    By keynan in forum Eclipse
    Replies: 0
    Last Post: 02-14-2008, 11:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •