JUnit testing result of a program
This is a very basic question for JUnit testing. I wrote a program which calculate the new position of a dot by given instructions for moving. The program is working properly but I have to write a JUnit test for check the result and I don't kno开发者_如何学运维w how.
Write a test method for every dot movement that you want to check. In each test method you call your method and then compare the actual result with the expected result.
Try something like this, using JUnit 4.x :
package org.dotmover;
import org.junit.Assert;
import org.junit.Test;
public class DotMoverTest {
@Test
public void testDotMoverForward() {
final DotMover dotMover = new DotMover(...);
final int newPos = dotMover.move(...);
final int expectedNewPos = ...;
Assert.assertEquals(expectedNewPos, newPos);
}
}
Add JUnit to classpath of you project. The create JUnit testcase. It is just a class that extends TestCase.
If you are using JUnit prior to v 4.0 each test method must start from word test
, e.g.
testPosition()
, testMoviing()
etc.
If you are using version 4 and higher the test methods must be targeted with annotation @Test.
Now write your testing scenario. User static assertXXX()
methods of class Assert
to verify that your program is working.
Good luck and happy TDD!
精彩评论