Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

References


Notes

Netbeans bug:

ref: Test packages can't find classes in source packages
Apparently this happens because NetbBeans cache isn't synchronized properly.
A quick fix is:
  • Close the project;
  • exit Netbeans;
  • remove .netbeans/6.9/var/cache/index (replace 6.9. with your NetbBeans version) - this folder is located on your windows user default folder (ex. C:\Documents and Settings\Administrator\.netbeans\7.0\var\cache).
    NOTE: NetBeans cache files can always be safely removed because it's in the nature of a cache that it will be re-built as necessary.
  • Then restart NetBeans, reopen the project and everything is fine again.

Overview [1]

Testing an application is an integral part of the development cycle, and writing and maintaining unit tests can help ensure that the individual methods in your source code work correctly.
Testing code often helps ensure that small changes made in the code do not break the application. Automated testing tools like JUnit streamline the process of testing and frequent testing can help catch coding errors early.

JUnit 4 Vs JUnit 3

The syntax for writing tests in JUnit 4 is simpler than JUnit 3 - in JUnit 4 you can use annotations.
In JUnit 3 you need to follow naming conventionsfor the test classes and methods, in JUnit 4 it is no longer necessary to use this naming syntax because you can use annotations to identify test methods and the test class is no longer required to extend TestCase.

JUnit 4 is back-compatible so you can run JUnit 4 test suites that contain JUnit 4 and JUnit 3 tests (NOTE: To run JUnit 3 test suites as part of a JUnit 4 test suite requires JUnit 4.4 or higher).
In JUnit 4 test suites you specify the test classes to include as values of the @Suite annotation.

In Netbeans if you already selected JUnit 3.x as the default version for your tests, you need to change the default version to JUnit 4.x. To change the default JUnit version, expand the Test Libraries node, right-click the JUnit library and choose Remove. You can now use the Add Library dialog box to explicitly add the JUnit 4 library or you can select version 4.x when you are prompted to select the JUnit version when you create a new test. You can still run JUnit 3 tests, but any new tests you create will use JUnit 4.

JUnit 3 uses naming conventions and reflection to identify tests:

  •  the test class must end with the word "Test" and the convention is NameOfClassTest where "NameOfClass" is the class you want to test;
  •  the test class is required to extend junit.framework.TestCase;
  •  names for the test methods: each test method name is required to follow the syntax test<NAME> (ex. "testScalarMultiplication()" for the method that tests "scalarMultiplication()");
 Note: In JUnit 4 it is no longer necessary to use this test method naming syntax because you can use annotations to identify test methods and the test class is no longer required to extend TestCase.

JUnit 4 annotations:

 - each test method is annotated with @Test
 - if you want to test using a timeout you can use: @Test(timeout=1000)
 - if you want to test for a expected exception you can use: @Test(expected=IllegalArgumentException.class)
 - if you want to ignore a test method add @Ignore
 - In JUnit 4 you can use annotations to mark the following types of initializer and finalizer methods:
  • Test Class Initializer. The @BeforeClass annotation marks a method as a test class initialization method. A test class initialization method is run only once, and before any of the other methods in the test class. For example, instead of creating a database connection in a test initializer and creating a new connection before each test method, you may want to use a test class initializer to open a connection before running the tests. You could then close the connection with the test class finalizer.
  • Test Class Finalizer. The @AfterClass annotation marks a method as a test class finalizer method. A test class finalizer method is run only once, and after all of the other methods in the test class are finished.
  • Test Initializer. The @Before annotation marks a method as a test initialization method. A test initialization method is run before each test case in the test class. A test initialization method is not required to run tests, but if you need to initialize some variables before you run a test, you use a test initializer method.
  • Test Finalizer. The @After annotation marks a method as a test finalizer method. A test finalizer method is run after each test case in the test class. A test finalizer method is not required to run tests, but you may need a finalizer to clean up any data that was required when running the test cases.

JUnit assertion methods

ref:  Class Assert on the official JUnit javadoc
  • assertEquals(expResult, result):  To use the assertion, you supply the input variables and the expected result. To pass the test, the test method must successfully return all the expected results based on the supplied variables when running the tested method. You should add a sufficient number of assertions to cover the various possible permutations.
  • JUnit assertTrue and assertFalse methods to test a variety of possible results. For the test of this method to pass, the assertTrue must all be true and assertFalse must all be false.
  • Asserts that two int arrays are equal:
    static void assertArrayEquals(int[] expecteds, int[] actuals); (many overloads for other array types)
  • Asserts that two objects are equal:
    static void assertEquals(Object expected, Object actual); (many overloads for other array types)
  • Asserts that a condition is true:
    static void  assertTrue(boolean condition)
  • Asserts that a condition is false:
    static void assertFalse(boolean condition)
  • Asserts that an object is null:
    static void assertNull(Object object)
  • Asserts that an object is not null:
    static void    assertNotNull(Object object)
  • Asserts that two objects refer to the same object:
    static void    assertSame(String message, Object expected, Object actual)
  • Asserts that two objects do not refer to the same object:
    static void assertNotSame(String message, Object unexpected, Object actual)
  • Asserts that actual satisfies the condition specified by matcher:
    static <T> void assertThat(T actual, Matcher<T> matcher)
  • Fails a test with the given message:
    static void fail(String message)

Test Suites [1]

A test suite is basically a class with a method that invokes the specified test cases, such as specific test classes, test methods in test classes and other test suites. A test suite can be included as part of a test class but best practices recommends creating individual test suite classes.

JUnit 3 suite example:
public JUnit3TestSuite(String testName) {
    super(testName);
}

public static Test suite() {
    TestSuite suite = new TestSuite("JUnit3TestSuite");
    suite.addTest(new TestSuite(sample.VectorsJUnit3Test.class));
    suite.addTest(new TestSuite(sample.UtilsJUnit3Test.class));
    return suite;
}

In JUnit 4 you just need to supply the test classes in the @Suite.SuiteClasses annotation.
Ex.:
@RunWith(Suite.class)
@Suite.SuiteClasses(value={UtilsJUnit4Test.class, VectorsJUnit4Test.class})
public class JUnit4TestSuite {
}

Sem comentários:

Enviar um comentário