debugging, logging and testing

20
Debugging, Logging and Testing Martin Ledvinka [email protected] Winter Term 2021 Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 1 / 20

Upload: others

Post on 03-Dec-2021

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Debugging, Logging and Testing

Debugging, Logging and Testing

Martin Ledvinka

[email protected]

Winter Term 2021

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 1 / 20

Page 2: Debugging, Logging and Testing

Contents

1 Debugging

2 Logging

3 Testing

4 Task

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 2 / 20

Page 3: Debugging, Logging and Testing

Debugging

Debugging

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 3 / 20

Page 4: Debugging, Logging and Testing

Debugging

Debugging

No software is perfect

When an issue occurs, it is necessary to be able to:

Identify itReproduce itDetermine its causeFix it

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 4 / 20

Page 5: Debugging, Logging and Testing

Debugging

Debugging

Multiple ways of identifying an issue:

1 Error in browser (Developer console/tools – F12)

2 Log entry3 Exception stack trace

It is necessary to be able to determine which part of a stack trace isrelevant and which is just the framework infrastructureEnterprise applications → long stack traces

Hint: Look for the last Caused by entry for the exceptionHint: Look for stack trace entries with your application packages

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 5 / 20

Page 6: Debugging, Logging and Testing

Debugging

Stack trace

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 6 / 20

Page 7: Debugging, Logging and Testing

Debugging

Stack trace II

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 7 / 20

Page 8: Debugging, Logging and Testing

Logging

Logging

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 8 / 20

Page 9: Debugging, Logging and Testing

Logging

Logging

Logging provides important auditing information about the application

Good to log:

Exceptions (duh)Application state informationInput/output data information

Configurable levels

Minimal logging for productionMaximal for debugging/testing

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 9 / 20

Page 10: Debugging, Logging and Testing

Logging

Java - logging frameworks

JUL (java.util.logging)Part of JDK, no external libraries necessarymultitude of logging levels

Apache Log4jLeading logging frameworks for yearsEasy configuration

Simple Logging Facade for Java (SLF4J)Logging facade, requires implementationThis separation is favourable – possible to use with JUL, Log4j,logback or othersCurrent logging leader

logbackSuccessor of the original Log4jToday’s most common configuration – SLF4J + logback

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 10 / 20

Page 11: Debugging, Logging and Testing

Logging

Logging levels

From SLF4J:

1 Trace

2 Debug

3 Info

4 Warn

5 Error

6 Fatal

7 All

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 11 / 20

Page 12: Debugging, Logging and Testing

Testing

Testing

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 12 / 20

Page 13: Debugging, Logging and Testing

Testing

Enterprise Application Testing

We are...

assuming you already have experience with testing.

Enterprise application testing is:

Harder in certain aspects

More about the right configuration

Application frameworks (like Spring) try to help with testing, e.g., byproviding restricted DI container for tests, so that DI still works

Otherwise similar to the usual:

JUnit, TestNGMockitoCucumber, JBehaveWebDriver/Selenium

Should be automated as much as possible

Big applications, not enough testers, not enough time

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 13 / 20

Page 14: Debugging, Logging and Testing

Testing

Code coverage

How much of the code is covered by tests

Coverage of lines, branches, classes, methods etc.

100% coverage does not mean code without bugs

JaCoCo, Cobertura

Testing and coverage can be configured as part of Maven build

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 14 / 20

Page 15: Debugging, Logging and Testing

Task

Task

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 15 / 20

Page 16: Debugging, Logging and Testing

Task

Syncing Your Fork

1 Make sure your local copy git repository is configured correctly1 andall changes are committed (git status - your branch is up to date,nothing to commit).

2 Fetch branches and commits from the upstream repository(EAR/B211-eshop)

git fetch upstream

3 Check out local branch corresponding to the task branch

git checkout -b b211-seminar-06-task

4 Merge changes from the corresponding upstream branch

git merge upstream/b211-seminar-06-task

5 Do your task6 Push the solution to your fork

git push origin b211-seminar-06-task

1see seminar 2 page 18 (Task for 1 point) and page 19 (Syncing Your Fork)Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 16 / 20

Page 17: Debugging, Logging and Testing

Task

E-shop Tests Tour

Let’s go through the tests in our e-shop.

In-memory database configured

Spring Boot-based configuration

@SpringBootTest@DataJpaTest

Transactional with automatic rollback

Test runs in a transaction which is rolled back in the end to keep thedatabase clean

Together

Application won’t deploy

Let’s examine the stack trace and fix the issue

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 17 / 20

Page 18: Debugging, Logging and Testing

Task

Task – 1 point

We have received the following issue reports:

Issues #006

When I attempt to add a product to a category for the first time, theapplication throws a NullPointerException. However, if the productalready belongs to some category, no such issue occurs.

Issues #007

When I create an order, all the items remain in the cart. As far as I amconcerned, creating an order should move the items from cart to the neworder and the cart should be empty.

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 18 / 20

Page 19: Debugging, Logging and Testing

Task

Task – Cont.

Write suitable tests reproducing the issues. For issue #006, the testsshould cover both cases (i.e., when the product does not belong toany category and when it already belongs to some category), as thebehavior needs to be consistent

That is, the tests should fail before the fix

Fix the issues

Verify that the tests pass

Note: Make sure to find a suitable spot where to test the behavior.Also look for existing code generating tests data

Acceptance criteria: Tests reproducing the original issues exist (youwill need at least three tests). The issues are fixed and the tests pass.

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 19 / 20

Page 20: Debugging, Logging and Testing

Task

Resources

Spring Testing

https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html

Spring Boot Testing

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing

JaCoCo Maven Plugin

https://www.jacoco.org/jacoco/trunk/index.html

SJF4J Docs

http://www.slf4j.org/docs.html

Martin Ledvinka ([email protected]) Debugging, Logging and Testing Winter Term 2021 20 / 20