Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab jUnit 3. Using jUnit from http://www.vogella.com/articles/JUnit/article. html 3.1. Preparation Create a new project LabJUnit. We want to create the unit tests in a separate

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Lab jUnit 3. Using jUnit from http://www.vogella.com/articles/JUnit/article. html 3.1. Preparation Create a new project LabJUnit. We want to create the unit tests in a separate folder. The creation of a separate folder for tests is not mandatory. But it is a good practice to keep the code separated from the regular code. You might even create a separate project for the test classes, but we skip this step to make this example simpler. Create a new source folder test by right-clicking on a project and selecting New- Source Folder. 3.2. Create a Java class In the "src" folder, create the labJUnit package and the following class. package 1abbunit public class Myclass public int multiply(int x, int y) return x y 3.3. Create a JUnit test Right click on your new class in the Package Explorer and select New JUnit Test Case Select "New JUnit 4 test" and set the source folder to "test, so that your test class gets created in this folder Note: The folder and package names throughout the tutorial are different because I changed them New JUnit Test Case Unit Test Case Select the name of the new JUnit test case. You have the options to specfy the class under test and on the next page, to select methods to be tested ONew JUnt 3 test ONew JUnk 4 test Source folder: de.vogela.jnit Browse. de.vogela.Junit.frst Browse. Name: Browse Which method stubs would you lke to create? setupo Do you want to add comments? (Configure templates and def ault value bere) Gener ate comments Class under test: de.vogela.junit.frst MyClass browse. Back Net > Finish Cancel Press "Next" and select the methods which you want to test New JUnit Test Case Test Methods Select methods for which test method stubs should be created Available methods: Al Object object hashCode) equals(Object) 1 method selected Create final method stubs Create tasks for gener ated test methods Back ext Cancel If the JUnit library in not part of your classpath, Eclipse will prompt you to do so New JUnit Test Case JUnt 4 is not on the build path. Do you want to add ? t now OOpen the build path property page Add nk 4 ibrary to the bld path OM Cancel Create a test with the following code package de.vogella.junit.first import org.junit.Test; import static org.junit.Assert.assertEquals; public class MyclassTest dTest public void testMultiplyO Myclass testernew MyclassO assertEquals("Result", 50, tester.multiply(10, 5)); 3.4. Run your test via Eclipse Right click on your new test class and select Run-As- JUnit Test. .. 1 Run on Server Run As Debug As Alt+5MHX, R The result of the tests will be displayed in the JUnit View. inished after 0,015 seconds Runs: 1/1 Errors: 0 D Falures: 1 de.vogella.junit.first.MyClassTest [Runner: 3Unit 4) (0,0 The test should be failing (indicated via a red bar). This is because our multiplier class is currently not working correctly (it does a division instead of multiplication). Fix the bug and re-run test to get a green bar. If you have several tests you can combine them into a test suite. Running a test suite will execute all tests in that suite. To create a test suite, select your test classes it New Other JUnit Test Suite. right click on New 2 Select a wizard Create a JUnt Test Sute izards: JUnit o Java 3Unit Test Case Unt Test Suite Enish Cancel Select "Next" and select the methods for which you want to create a test Change the code to the following to make your test suite run your test. If you develop another test later you can add it to @Suite.SuiteClasses package mypackage; import org.junit.runner.Runwith; import ora.iunit.runners. Suite: Runwith(Suite.cTass) @Suite.Suiteclasses( MyclassTest.class public class AllTests 3.5. Run your test via code You can also run your tests from via your own code. The class org.junit.runner.JUnitCore provides the method runClasses() which allows you to run one or several tests classes. As a return parameter you receive an object of the type org.junit.runner.Result. This object can be used to retrieve information about the tests. In your "test" folder create a new class MyTestRunner with the following code. This class will execute your test class and write potential failures to the console. package de.vogella.junit.first: import org.junit.runner.JUnitcore; import org.junit.runner.Result; import org.junit.runner.notification.Failure: public class MyTestRunner t public static void main(String[] args) Result result Junitcore.runclasses (MyclassTest.class); for (Failure failure result.getFailuresO) System.out.println(failure.tostringO) Last but not least modify the main method so that it prints the number of tests run, the number of tests passed and the number of tests that failed. (labeled)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

Students also viewed these Databases questions

Question

What is the preferred personality?

Answered: 1 week ago

Question

What is the relationship between humans?

Answered: 1 week ago