SlideShare a Scribd company logo
Unit testing

    (basic)
  November 2011
What is Unit Testing

Unit test is a method by which individual units of source code
are tested to determine if they are fit for use.
A unit is the smallest testable part of an application like
method or class.

Unit tests are written from a programmer’s perspective.
They ensures that a particular method of a class successfully
performs a set of operations.

Unit testing drives design: developer is concerned about how
client will use the interface of the class that is under the test.
What is not for?
It's not for testing correct inter-operation of multiple
subsystems.
NOTE: It should be a stand-alone test which is not related
to other subsystems.

It should not rely on external resources like(RDBMS,LDAP etc).
NOTE: Introducing dependencies on external resources or
data turns unit tests into integration tests.

In many cases you can't write a unit test to reproduce bug
appeared in production.
NOTE: it's not regression testing. with unit test you can
check logic responsible for interaction between different
layers.
The advantages of Unit Testing
● Ensure code continues to work as intended(only if it has a
  good coverage)

● Safe refactoring. Allows refactoring without fear to break the
  code.

● Fewer bugs. Find some tricky bugs in logic on first stage
  NOTE: tricky unit test scenarios may prevent many bugs.

● Developer concentrates more on the code and design.
  NOTE: less build/deploy cycles to see some progress.
  green bar shows you the progress

● Documentation
Disadvantages of Unit testing
Big time investment. For the simple case you lose about 20%
of the actual implementation, but for complicated cases you lose
much more.
NOTE: If you correctly follow TDD it will going to save you
time later in long-term perspective!

Design Impacts. Sometimes the high-level design is not clear
at the start and evolves as you go along - this will force you to
completely redo your test which will generate a big time lose.
NOTE: postpone unit tests in this case until you have
clarified high-level design.
Best Practices
 ● Make sure your tests test one thing and one thing only.
 ● Readability is important for tests. (see Example)
 ● Each unit test should be independent from the other.
 ● Separate you concerns. Extract layers to improve the
   design. (see Example with DAO layer)
 ● Mock behavior with mocks to concentrate on test
   scenario.
 ● Test Coverage(Check coverage during testing)
 ● Tests should run automatically to provide continuous
   feedback.
   Keep the bar green to keep the code clean!
Bad practices
● A singleton gets implemented using a static method. Static
  methods are avoided by people who do unit testing
  because they cannot be mocked or stubbed.
  Static methods are death to unit testability ref

● Don't rely on external resources

● Do not test the GUI.
JUnit - Adding new test case
Mark your test cases with @Test annotations.

Use @Before and @After to run method before and after
every test case. Several tests need similar objects created
before they can run. (See Example)

Use @BeforeClass and @AfterClass to run for one time
before and after all test cases.(Use it only to share expensive
setup)

Static imports makes code more readable: (See Example)
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)
JUnit test structure
Make test readable. Use next pattern:

// Given
Create some objects required for testing.

// When
Execute method that is under test

// Then
Check state and verify interaction

See example
JUnit - Assertion
1. Choose correct assert method from org.junit.Assert.*:
assertNull, assertNotNull, assertTrue, assertEquals...

2. Keep it simple:
assertEquals(age, calculateAge(dob)); // bad practice
assertEquals(age, 25); // Best practice. Keep it simple

3. Use overloaded method with argument for message:
assertNull("Value must be null in case of error", value);
JUnit - test under construction
Sometimes we need to temporarily disable a test that is
under construction. Use @Ignore annotation on method
or class to achieve it. Specify the reason why it's ignored.

@Ignore("Enable when TASK-2 is implemented")
public class MyClassTest {
  @Test public void testThat1() { ... }
  @Test public void testThat2() { ... }
}

public class MyClassTest {
  @Ignore("Gotta go now! will fix it later.")
  @Test void testThat1() { ... }
}
JUnit - Test Case With Exception
1. Expect that exception is thrown: (See example) @Test
(expected=EmailExistException.class) public void
testExceptionIsThrown() { ... }
2. More flexible old school way:
try { // execute method under the test customerService.add
(customer);
fail("exception must be thrown");
} catch (ServiceException exception) {
// state assertion: check error code
assertEquals(exception.getCode(), 404);
}
3. JUnit 4.7 has @Rule ExpectedException
State vs Interaction testing
State testing asserts properties on an object
Example: Verify that after tested method execution object has
properties: assertEquals(2, item.getCount());

Interaction testing verifies the interactions between objects.
Example: Did my controller correctly call my services in
specified order? Can be used only with mocked objects.

Mockito is a framework for interactions testing.

Mockito is a mocking framework
that tastes really good!
Mock behavior with mocks.
Mocks or mock objects simulate the behavior of complex, real
(non-mock) objects and are therefore useful when a real object
is impractical or impossible to incorporate into a unit test.

 They provide:
 ● Default values unless stubbed
 ● Can help verify interactions, order of interaction, method
   parameters etc.

Mockito is a Java based mocking framework that allows you to
write beautiful tests with clean & simple API.
Why Mockito is good?

● with Mockito, you only stub what you need and go on
  happily completing the functionality. No need to stub
  everything.

● simple and clean syntax

● all required documentation can be found in javadocs to org.
  mockito.Mockito.

● Has an ability to create partial mocks.

● It tastes really good
JUnit - Mock in Mockito
This will mock all methods in MyService class and provide
default values:

 1. @Mock MyService myService;
    All classes with @Mock annotation will be injected to
    @InjectMocks class by type automatically when using
    @RunWith(MockitoJUnitRunner.class).

 2. You can mock and inject services manually:
    MyService myService = mock(MyService.class);
    ServiceUnderTest service = new ServiceUnderTest();
    service.setMyService(myService);
You can mock concrete classes, not only interfaces!
JUnit - Stubbing in Mockito
 Stubbing is adding canned response to Mock object methods.

Examples:
 ● method stubbing:
   when(mockService.someMethod(”value”))
     .thenReturn(”someValue”);
 ● stubbing method with exception:
   when(mockService.someMethod("value"))
     .thenThrow(new MyException("Error"));
   or stubbing void method with exception:
   doThrow(new MyException(”Error”)
     .when(mockService).someMethod("value");

Mockito verifies argument values in natural java style: by using
an equals() method.
JUnit - Argument matching in Mockito

Sometimes, when extra flexibility is required then you might
use argument matchers:
 ● when(mockedList.get(anyString())).thenReturn("element");
 ● any(ClassName.class)
 ● Custom argument matcher:(see ArgumentMatcher)

IMPORTANT: If you are using argument matchers, all
arguments have to be provided:
 verify(mock).someMethod(anyInt(), anyString(), eq("arg"));
 verify(mock).someMethod(anyInt(), anyString(), "arg") -
Wrong
JUnit - Verify behavior in Mockito
Once created, mock will remember all interactions. Then you
can selectively verify whatever interaction you are interested in:

 ● verify(mockService).someMethod("someArgs");
 ● verify(mockService, times(2)).someMethod("someArgs");
 ● verify(mockService, never()).someMethod("someArgs");
   NOTE: never() is an alias to times(0)
 ● atLeastOnce()
 ● atLeast(2)
 ● atMost(5)
 ● verifyZeroInteractions(mockService)
 ● verifyNoMoreInteractions(mockService)
JUnit - Verification in order
Verification in order is flexible - you don't have to verify all
interactions one-by-one but only those that you are
interested in testing in order.

//create inOrder object passing any mocks relevent for in-order verification
InOrder inOrder = inOrder(firstMock, secondMock);

// make sure that firstMock was called before secondMock
inOrder.verify(firstMock).add("was called first");
inOrder.verify(secondMock).add("was called second");
JUnit - Spy(Partial mocking) in Mockito
When you use the Spy then the real methods are called (unless
a method was stubbed).
  ● Calls real methods unless the method is stubbed.
  ● Use of too much spy is potential code smell.

   MyService myService = new MyService();
   MyService spy = spy(myService);
   //optionally, you can stub out some methods when(spy.
   someMethod()).thenReturn(val);
   //real method logic will be executed
   spy.realMethod();

   See Example
Test Coverage (see Example)
Eclemma plugin for eclipse: http://www.eclemma.org/




#ant coverage - to generate report from console
Test Driven Development




After that build project, deploy, make sure that it works.
See Example
Unit testing is a lot like going to the gym
No pain, no gain

*
Let's drink!


Contact Information
Email: yuri.anischenko@gmail.com

More Related Content

What's hot

What's hot (20)

Power mock
Power mockPower mock
Power mock
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
05 junit
05 junit05 junit
05 junit
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Unit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftUnit testing in xcode 8 with swift
Unit testing in xcode 8 with swift
 
Mock with Mockito
Mock with MockitoMock with Mockito
Mock with Mockito
 
Introduction To J unit
Introduction To J unitIntroduction To J unit
Introduction To J unit
 
Junit
JunitJunit
Junit
 
J Unit
J UnitJ Unit
J Unit
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
Junit
JunitJunit
Junit
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1
 

Viewers also liked

Fission rate and_time_of_higly_excited_nuclei
Fission rate and_time_of_higly_excited_nucleiFission rate and_time_of_higly_excited_nuclei
Fission rate and_time_of_higly_excited_nucleiYuri Anischenko
 
Rc2010 alt architecture
Rc2010 alt architectureRc2010 alt architecture
Rc2010 alt architectureJasonOffutt
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...DevDay.org
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 

Viewers also liked (6)

Fission rate and_time_of_higly_excited_nuclei
Fission rate and_time_of_higly_excited_nucleiFission rate and_time_of_higly_excited_nuclei
Fission rate and_time_of_higly_excited_nuclei
 
Rc2010 alt architecture
Rc2010 alt architectureRc2010 alt architecture
Rc2010 alt architecture
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Mocking with Moq
Mocking with MoqMocking with Moq
Mocking with Moq
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 

Similar to Unit testing basic

A la découverte des google/mock (aka gmock)
A la découverte des google/mock (aka gmock)A la découverte des google/mock (aka gmock)
A la découverte des google/mock (aka gmock)Thierry Gayet
 
Unit test documentation
Unit test documentationUnit test documentation
Unit test documentationAnjan Debnath
 
31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdfgauravavam
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsMarcelo Busico
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnitAktuğ Urun
 
An Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAn Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAhmed Ehab AbdulAziz
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testingpleeps
 
Software Testing
Software TestingSoftware Testing
Software TestingAdroitLogic
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosFlutter Agency
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentJohn Blum
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in AngularKnoldus Inc.
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testingalessiopace
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in JavaAnkur Maheshwari
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android ApplicationsRody Middelkoop
 

Similar to Unit testing basic (20)

A la découverte des google/mock (aka gmock)
A la découverte des google/mock (aka gmock)A la découverte des google/mock (aka gmock)
A la découverte des google/mock (aka gmock)
 
Unit test documentation
Unit test documentationUnit test documentation
Unit test documentation
 
31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
An Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAn Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDD
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in Java
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android Applications
 

Recently uploaded

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka DoktorováCzechDreamin
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIES VE
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 
Agentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfAgentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfChristopherTHyatt
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxDavid Michel
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsExpeed Software
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Alison B. Lowndes
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeCzechDreamin
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsPaul Groth
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Jeffrey Haguewood
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaCzechDreamin
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCzechDreamin
 

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Agentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfAgentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdf
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 

Unit testing basic

  • 1. Unit testing (basic) November 2011
  • 2. What is Unit Testing Unit test is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application like method or class. Unit tests are written from a programmer’s perspective. They ensures that a particular method of a class successfully performs a set of operations. Unit testing drives design: developer is concerned about how client will use the interface of the class that is under the test.
  • 3. What is not for? It's not for testing correct inter-operation of multiple subsystems. NOTE: It should be a stand-alone test which is not related to other subsystems. It should not rely on external resources like(RDBMS,LDAP etc). NOTE: Introducing dependencies on external resources or data turns unit tests into integration tests. In many cases you can't write a unit test to reproduce bug appeared in production. NOTE: it's not regression testing. with unit test you can check logic responsible for interaction between different layers.
  • 4. The advantages of Unit Testing ● Ensure code continues to work as intended(only if it has a good coverage) ● Safe refactoring. Allows refactoring without fear to break the code. ● Fewer bugs. Find some tricky bugs in logic on first stage NOTE: tricky unit test scenarios may prevent many bugs. ● Developer concentrates more on the code and design. NOTE: less build/deploy cycles to see some progress. green bar shows you the progress ● Documentation
  • 5. Disadvantages of Unit testing Big time investment. For the simple case you lose about 20% of the actual implementation, but for complicated cases you lose much more. NOTE: If you correctly follow TDD it will going to save you time later in long-term perspective! Design Impacts. Sometimes the high-level design is not clear at the start and evolves as you go along - this will force you to completely redo your test which will generate a big time lose. NOTE: postpone unit tests in this case until you have clarified high-level design.
  • 6. Best Practices ● Make sure your tests test one thing and one thing only. ● Readability is important for tests. (see Example) ● Each unit test should be independent from the other. ● Separate you concerns. Extract layers to improve the design. (see Example with DAO layer) ● Mock behavior with mocks to concentrate on test scenario. ● Test Coverage(Check coverage during testing) ● Tests should run automatically to provide continuous feedback. Keep the bar green to keep the code clean!
  • 7. Bad practices ● A singleton gets implemented using a static method. Static methods are avoided by people who do unit testing because they cannot be mocked or stubbed. Static methods are death to unit testability ref ● Don't rely on external resources ● Do not test the GUI.
  • 8. JUnit - Adding new test case Mark your test cases with @Test annotations. Use @Before and @After to run method before and after every test case. Several tests need similar objects created before they can run. (See Example) Use @BeforeClass and @AfterClass to run for one time before and after all test cases.(Use it only to share expensive setup) Static imports makes code more readable: (See Example) import static org.mockito.Mockito.*; import static org.junit.Assert.*; @RunWith(MockitoJUnitRunner.class)
  • 9. JUnit test structure Make test readable. Use next pattern: // Given Create some objects required for testing. // When Execute method that is under test // Then Check state and verify interaction See example
  • 10. JUnit - Assertion 1. Choose correct assert method from org.junit.Assert.*: assertNull, assertNotNull, assertTrue, assertEquals... 2. Keep it simple: assertEquals(age, calculateAge(dob)); // bad practice assertEquals(age, 25); // Best practice. Keep it simple 3. Use overloaded method with argument for message: assertNull("Value must be null in case of error", value);
  • 11. JUnit - test under construction Sometimes we need to temporarily disable a test that is under construction. Use @Ignore annotation on method or class to achieve it. Specify the reason why it's ignored. @Ignore("Enable when TASK-2 is implemented") public class MyClassTest { @Test public void testThat1() { ... } @Test public void testThat2() { ... } } public class MyClassTest { @Ignore("Gotta go now! will fix it later.") @Test void testThat1() { ... } }
  • 12. JUnit - Test Case With Exception 1. Expect that exception is thrown: (See example) @Test (expected=EmailExistException.class) public void testExceptionIsThrown() { ... } 2. More flexible old school way: try { // execute method under the test customerService.add (customer); fail("exception must be thrown"); } catch (ServiceException exception) { // state assertion: check error code assertEquals(exception.getCode(), 404); } 3. JUnit 4.7 has @Rule ExpectedException
  • 13. State vs Interaction testing State testing asserts properties on an object Example: Verify that after tested method execution object has properties: assertEquals(2, item.getCount()); Interaction testing verifies the interactions between objects. Example: Did my controller correctly call my services in specified order? Can be used only with mocked objects. Mockito is a framework for interactions testing. Mockito is a mocking framework that tastes really good!
  • 14. Mock behavior with mocks. Mocks or mock objects simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. They provide: ● Default values unless stubbed ● Can help verify interactions, order of interaction, method parameters etc. Mockito is a Java based mocking framework that allows you to write beautiful tests with clean & simple API.
  • 15. Why Mockito is good? ● with Mockito, you only stub what you need and go on happily completing the functionality. No need to stub everything. ● simple and clean syntax ● all required documentation can be found in javadocs to org. mockito.Mockito. ● Has an ability to create partial mocks. ● It tastes really good
  • 16. JUnit - Mock in Mockito This will mock all methods in MyService class and provide default values: 1. @Mock MyService myService; All classes with @Mock annotation will be injected to @InjectMocks class by type automatically when using @RunWith(MockitoJUnitRunner.class). 2. You can mock and inject services manually: MyService myService = mock(MyService.class); ServiceUnderTest service = new ServiceUnderTest(); service.setMyService(myService); You can mock concrete classes, not only interfaces!
  • 17. JUnit - Stubbing in Mockito Stubbing is adding canned response to Mock object methods. Examples: ● method stubbing: when(mockService.someMethod(”value”)) .thenReturn(”someValue”); ● stubbing method with exception: when(mockService.someMethod("value")) .thenThrow(new MyException("Error")); or stubbing void method with exception: doThrow(new MyException(”Error”) .when(mockService).someMethod("value"); Mockito verifies argument values in natural java style: by using an equals() method.
  • 18. JUnit - Argument matching in Mockito Sometimes, when extra flexibility is required then you might use argument matchers: ● when(mockedList.get(anyString())).thenReturn("element"); ● any(ClassName.class) ● Custom argument matcher:(see ArgumentMatcher) IMPORTANT: If you are using argument matchers, all arguments have to be provided: verify(mock).someMethod(anyInt(), anyString(), eq("arg")); verify(mock).someMethod(anyInt(), anyString(), "arg") - Wrong
  • 19. JUnit - Verify behavior in Mockito Once created, mock will remember all interactions. Then you can selectively verify whatever interaction you are interested in: ● verify(mockService).someMethod("someArgs"); ● verify(mockService, times(2)).someMethod("someArgs"); ● verify(mockService, never()).someMethod("someArgs"); NOTE: never() is an alias to times(0) ● atLeastOnce() ● atLeast(2) ● atMost(5) ● verifyZeroInteractions(mockService) ● verifyNoMoreInteractions(mockService)
  • 20. JUnit - Verification in order Verification in order is flexible - you don't have to verify all interactions one-by-one but only those that you are interested in testing in order. //create inOrder object passing any mocks relevent for in-order verification InOrder inOrder = inOrder(firstMock, secondMock); // make sure that firstMock was called before secondMock inOrder.verify(firstMock).add("was called first"); inOrder.verify(secondMock).add("was called second");
  • 21. JUnit - Spy(Partial mocking) in Mockito When you use the Spy then the real methods are called (unless a method was stubbed). ● Calls real methods unless the method is stubbed. ● Use of too much spy is potential code smell. MyService myService = new MyService(); MyService spy = spy(myService); //optionally, you can stub out some methods when(spy. someMethod()).thenReturn(val); //real method logic will be executed spy.realMethod(); See Example
  • 22. Test Coverage (see Example) Eclemma plugin for eclipse: http://www.eclemma.org/ #ant coverage - to generate report from console
  • 23. Test Driven Development After that build project, deploy, make sure that it works. See Example
  • 24. Unit testing is a lot like going to the gym
  • 25. No pain, no gain *
  • 26. Let's drink! Contact Information Email: yuri.anischenko@gmail.com