1 Lars Vogel Version 0.2 Copyright 2007 Lars Vogel Abstract This article gives a short overview of JUnit 4.x and its usage within Eclipse. You should be able to run tests with JUnit from Eclipse after reading this chapter. With Eclipse Mars, they switched to a tar file/Eclipse installer for Mac. This is my first upgrade since that Eclipse became a native Mac app. The installer says “Eclipse installer by Oomph” and gives you a choice of a number of Eclipse packages. Table of Contents 1. General Unit Testing Installation Introduction in Writing a Test Overview Write your first test Run your first test (Eclipse) Run your first test (command line) JUnit with Eclipse Preparation Create a normal class Create a test class Create a test suite JUnit (more) in Detail Annotations Test statements Links and Literature General 1.1. Unit Testing A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested. It can be used to ensure that existing functionality is still working after changes to the coding or the environment the program is running in. JUnit 4.x is a framework for automatically testing java code originally written by Erich Gamma and Kent Beck. It uses annotation to identify what are the test methods. A very important assumption of JUnit is that the all test can be performed in an arbitrary order hence each test should stand alone and not depend on another test. Installation Download JUnit4.x.jar from the JUnit website here [6] and add it to classpath. Tip You can use Junit independently of Eclipse but using Junit with Eclipse is easier due to the very good integrated JUnit support. Introduction in Writing a Test 2.1. Overview JUnit uses annotations to identify which methods should be used for testing. To write a test Annotate a method When you want to check two value for equality, import org.junit.assert.* statically1, call assertequals() and pass both values. O Simulador Impacta um aplicativo grtis que facilitar vendedores e usurios finais que no sabem configurar e optar por um modelo de central PABX. Intelbras media player for mac. ![]() Static imports are available since Java 5.0: static import, e.g. Import static org.junit.assert.*; 2.2. Write your first test Write the following java source file: package testingtest; import static org.junit.assert.assertequals; import org.junit.test; public class MyFirstJUnitTest public void simpleadd() { int result = 1; int expected = 1; assertequals(result, expected); 2 3 2.3. Run your first test (Eclipse) If your are using Eclipse select your new file, right mouse click on it and select Run As -> JUnit test Eclipse will display the result via a green or red bar Run your first test (command line) I the JUnit distribution you have a testrunner which can also run your JUnit tests. Switch to the command line and type the following command. Org.junit.junitcore.runclasses(testclass1.class.); 3. JUnit with Eclipse 3.1. ![]() Preparation Create a new project. Add a folder lib to this project. Copy your Junit jar into this folder and add it to the buld path of Eclipse. See here [ on how to do this. Create a new source folder junit for your project. To do so right mouse click on your project, select properties and choose the java build path. Select the tab source code. Press 'Add folder' then then press 'Create new folder'. Create the folder junit Create a normal class Create his extremely boring class 'MyClass'. We will use this class for testing. Public class MyClass { public int multiply(int x, int y) { return x / y; 3 4 3.3. Create a test class Add JUnit.jar to your build path Select your new class, right mouse click and select New ->JUnit Test case, change the source folder to JUnit Press next and select the methods which you want to test.
0 Comments
Leave a Reply. |