+ All Categories
Home > Documents > Prezentare JUNIT.PPTX

Prezentare JUNIT.PPTX

Date post: 09-Jan-2016
Category:
Upload: heim-bogdan
View: 9 times
Download: 0 times
Share this document with a friend

of 18

Transcript

Prezentare Jenkins

Unit testing

1What is unit testing ?

Unit testingis a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation.Unit testingis often automated but it can also be done manually.

BPMWAVE

Why use unit testing?Tests Reduce Bugs in New FeaturesTests Reduce Bugs in Existing FeaturesTests Are Good DocumentationTests Reduce the Cost of ChangeTests Improve DesignTests Allow RefactoringTests Reduce FearTests Defend Against Other ProgrammersTesting Makes Development Faster

BPMWAVE

JUnit 4JUnit is the most popular java unit testing framework along with TestNG

Advantages and functionalities

adnotations assertionstest suitsparametrizationsrulescategoriesvisual reprezentationworks with many additional libraries( ex: Mockito,Arquillian,Selenium )

BPMWAVE

Common annotations@Testpublic void method()TheTestannotation indicates that the public void method to which it is attached can be run as a test case.@Beforepublic void method()TheBeforeannotation indicates that this method must be executed before each test in the class, so as to execute some preconditions necessary for the test.@BeforeClasspublic static void method()TheBeforeClassannotation indicates that the static method to which is attached must be executed once and before all tests in the class. That happens when the test methods share computationally expensive setup (e.g. connect to database).@Afterpublic void method()TheAfterannotation indicates that this method gets executed after execution of each test (e.g. reset some variables after execution of every test, delete temporary variables etc)@AfterClasspublic static void method()TheAfterClassannotation can be used when a method needs to be executed after executing all the tests in a JUnit Test Case class so as to clean-up the expensive set-up (e.g disconnect from a database). Attention: The method attached with this annotation (similar toBeforeClass) must be defined as static.@Ignorepublic static void method()TheIgnoreannotation can be used when you want temporarily disable the execution of a specific test. Every method that is annotated with@Ignorewont be executed.BPMWAVE

AssertionsAssertionDescriptionvoid assertEquals([String message], expected value, actual value)Asserts that two values are equal. Values might be type of int, short, long, byte, char or java.lang.Object. The first argument is an optional String message.void assertTrue([String message], boolean condition)Asserts that a condition is true.void assertFalse([String message],boolean condition)Asserts that a condition is false.void assertNotNull([String message], java.lang.Object object)Asserts that an object is not null.void assertNull([String message], java.lang.Object object)Asserts that an object is null.void assertSame([String message], java.lang.Object expected, java.lang.Object actual)Asserts that the two objects refer to the same object.void assertNotSame([String message], java.lang.Object unexpected, java.lang.Object actual)Asserts that the two objects do not refer to the same object.void assertArrayEquals([String message], expectedArray, resultArray)Asserts that the array expected and the resulted array are equal. The type of Array might be int, long, short, char, byte BPMWAVE

6BPMWAVE

Test suitsInsoftware development, atest suite, less commonly known as avalidation suite, is a collection oftest casesthat are intended to be used to test a software program to show that it has some specified set of behaviours. A test suite often contains detailed instructions or goals for each collection of test cases and information on the system configuration to be used during testing. A group of test cases may also contain prerequisite states or steps, and descriptions of the following tests.

Parameterized JUnit

Parameterized unit tests are used to test the same code under different conditions. Thanks to parameterized unit tests we can set up a test method that retrieves data from some data source. This data source can be a collection of test data objects, external file or maybe even a database. The general idea is to make it easy to test different conditions with the same unit test method, which will limit the source code we need to write and makes the test code more robust. We can call these tests data-driven unit tests.BPMWAVE

BPMWAVE

BPMWAVERules

Rules add special handling around tests, test cases or test suites. They can do additional validations common for all tests in the class, concurrently run multiple test instances, set up resources before each test or test case and tear them down afterwards.The rule gets complete control over what will done with the test method, test case or test suite it is applied to. Complete control means that the rule decides what to do before and after running it and how to deal with thrown exceptions.

BPMWAVE

CategoriesBPMWAVE

Categories are used to organize tests. For example, we can use them to easily distinguish performance unit test from integration tests or slow tests from faster tests.

The concept of mockingBPMWAVE

To put it simply, mocking means creating an imitation of an object that simulates the behavior of a real object.

Mocking scenario:

You have Class A that has a dependency on Class B and you want to test a method in Class A that uses a service from Class B without testing the Class B service. This is why you create a mock of the Class B service. This helps you to test Class A method in isolation so you dont have to test dependencies that already work or already have tests written for them .

Mockito exampleBPMWAVE

BPMWAVE

To create a mock of a class you simply call the static method mock, or you can also use adnotations (@Mock). Using static methods when and thenReturns you can tell the mocked class how to behave.

Our unit testing methodologyBPMWAVE

We use Jenkins to automatically tell maven to build the project and run tests or not. Tests are made to programmatically simulate http post to the server using apache http web client.

BPMWAVE

BPMWAVEIntrebari ?? ??????

??????18


Recommended