software engineering a.a. 2018-2019
TRANSCRIPT
JUnit e Testing Tools Luca Mainetti
Software Testing – Why?
! Product Quality! Security! Customer Satisfaction! Software Testing Saves Money
2
It’s required to stay in the business!
JUnit e Testing Tools Luca Mainetti
Test Driven Development
3
TestFAILS
TestPASSED
CodeREFACTOR
! TDD life cycle:– Writing a failing test– Write just enough code to pass it– Change code for better without
changing its behaviour
JUnit e Testing Tools Luca Mainetti
Testing Spring Boot
! Three kinds of Test for Spring Boot:– Unit Test
• Test a single unit of code– Integration test
• Test a set of different architecture concerns (e.i., cross-cutting)• The most common integration test: Web Controller/REST API Test
4
JUnit e Testing Tools Luca Mainetti
Unit test vs. Integration test! A unit test covers a single “unit”: a single class! An integration test can be any of the following:
– a test that covers multiple “units”– a test that covers multiple layers: it might cover the interaction
between a business service and the persistence layer, for instance.– a test that covers the whole path through the application: let’s send a
request to the application and check that it responds correctly and haschanged the database state according to our expectations.
– For example, Web REST API Test covers the following phases:– Listen to HTTP Requests – Deserialize Input – Validate Input – Call the Business Logic– Serialize the Output – Translate Exceptions
5
JUnit e Testing Tools Luca Mainetti
Structure of Test in Javapublic class NameOfClassTest {
private class attributes;
@Beforevoid public setUp() {
// set up the environment;}
@Testvoid testMethod1() {
// list of assertionsassertThat(...);
}
@Testvoid testMethod2() {
…
@Aftervoid public clean() {
// clean the environment;}
6
JUnit e Testing Tools Luca Mainetti
Junit - Introduction
! JUnit is the most popular Java Unit testing framework
! Before unit testing, we depend on deploying the entire app and checking if the screens look great. But that’s not very efficient. And it is manual.
! Unit Testing focuses on writing automated tests for individual classes and methods.
! JUnit is a framework which will help you call a method and check (or assert) whether the output is as expected.
! The important thing about automation testing is that these tests can be run with continuous integration - as soon as some code changes.
! Current stable version: JUnit 5
7
JUnit e Testing Tools Luca Mainetti
Mock-up with Mockito
! Mocking is creating objects that simulate the behavior of real objects.– An object under test may have dependencies on other (complex)
objects– Mocks are useful if the real objects are impractical to incorporate
into the unit test (i.e., in development/test environment)
! Mockito is the most popular mocking framework in Java! Current stable version: 2
9
JUnit e Testing Tools Luca Mainetti
References
! JUnit5 official user guide: https://junit.org/junit5/docs/current/user-guide/
! JUnit5 list of asserts:https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/Assertions.html
! Mockito: http://site.mockito.org/
10