SlideShare ist ein Scribd-Unternehmen logo
1 von 51
Testing basics for developers
Anton Udovychenko
1 1 / 2 0 1 3
Agenda
• Motivation
• Unit testing
• JUnit, Mockito, Hamcrest, JsTestDriver
• Integration testing
• Persistence testing, Arquillian
• Functional testing
• SoapUI, Selenium
• Q&A
Why should we care?
Automated testing
Functional
Integration
Unit
5%
15%
80%
Unit testing
Test small portions of production code
Confidence to change
Quick Feedback
Documentation
Functional
Integration
Unit
Unit testing
TestNG
JUnit lifecycle
1. @BeforeClass
2. For each @Test
a) Instanciate test class
b) @Before
c) Invoke the test
d) @After
3. @AfterClass
JUnit advanced
1. @Rule and @ClassRule
2. Parametrized
3. Mocks
4. Hamcrest
JUnit @Rule
public class MyRule implements TestRule {
@Override
public Statement apply( Statement base, Description description ) {
return new MyStatement( base );
}
}
JUnit @Rule
public class MyStatement extends Statement {
private final Statement base;
public MyStatement( Statement base ) {
this.base = base;
}
@Override
public void evaluate() throws Throwable {
System.out.println( "before" );
try {
base.evaluate();
} finally {
System.out.println( "after" );
}
}
}
public class MyRule implements TestRule {
@Override
public Statement apply( Statement base, Description description ) {
return new MyStatement( base );
}
}
JUnit @Rule
public class MyTest {
@Rule
public MyRule myRule = new MyRule();
@Test
public void testRun() {
System.out.println( "during" );
}
}
public class MyRule implements TestRule {
@Override
public Statement apply( Statement base, Description description ) {
return new MyStatement( base );
}
}
public class MyStatement extends Statement {
private final Statement base;
public MyStatement( Statement base ) {
this.base = base;
}
@Override
public void evaluate() throws Throwable {
System.out.println( "before" );
try {
base.evaluate();
} finally {
System.out.println( "after" );
}
}
}
JUnit @Rule
public class MyTest {
@Rule
public MyRule myRule = new MyRule();
@Test
public void testRun() {
System.out.println( "during" );
}
}
public class MyRule implements TestRule {
@Override
public Statement apply( Statement base, Description description ) {
return new MyStatement( base );
}
}
public class MyStatement extends Statement {
private final Statement base;
public MyStatement( Statement base ) {
this.base = base;
}
@Override
public void evaluate() throws Throwable {
System.out.println( "before" );
try {
base.evaluate();
} finally {
System.out.println( "after" );
}
}
}
Output:
before
during
after
JUnit @ClassRule
@RunWith(Suite.class)
@SuiteClasses({ TestCase1.class, TestCase2.class })
public class AllTests {
@ClassRule
public static Timeout timeout = new Timeout(3000);
}
JUnit Parametrized
@RunWith(value = Parameterized.class)
public class MyTest {
private int number;
public MyTest(int number) {
this.number = number;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
return Arrays.asList(data);
}
@Test
public void pushTest() {
System.out.println("Parameterized Number is : " + number);
}
}
Mockito
Mockito is a mocking framework for unit tests in Java
Mockito
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import java.util.Iterator;
import org.junit.Test;
....
@Test
public void iteratorWillReturnHelloWorld(){
//arrange
Iterator i=mock(Iterator.class);
when(i.next()).thenReturn("Hello").thenReturn("World");
//act
String result=i.next()+" "+i.next();
//assert
assertEquals("Hello World", result);
}
Hamcrest
Hamcrest is a matchers framework that assists writing software tests
Hamcrest
assertTrue(foo.contains("someValue") && foo.contains("anotherValue"));
Hamcrest
assertTrue(foo.contains("someValue") && foo.contains("anotherValue"));
assertThat(foo, hasItems("someValue", "anotherValue"));
vs
Hamcrest
assertTrue(foo.contains("someValue") && foo.contains("anotherValue"));
assertThat(foo, hasItems("someValue", "anotherValue"));
vs
assertThat(
table,
column("Type",contains("A","B","C")).where(cell("Status", is("Ok")))
);
Another example:
JsTestDriver
JsTestDriver is an open source JavaScript unit tests runner
JsTestDriver
Integration testing
Test collaboration between components
Database
IO system
Special environment configuration
Functional
Integration
Unit
Persistence testing
In memory databases
DBUnit
Arquillian
Arquillian is a platform that simplifies integration testing for Java middleware
Arquillian
• Real Tests (no mocks)
Arquillian
• Real Tests (no mocks)
• IDE Friendly
Arquillian
• Real Tests (no mocks)
• IDE Friendly
• Test Enrichment
Arquillian
• Real Tests (no mocks)
• IDE Friendly
• Test Enrichment
• Classpath Control
Arquillian
• Real Tests (no mocks)
• IDE Friendly
• Test Enrichment
• Classpath Control
• Drive the Browser
Arquillian
• Real Tests (no mocks)
• IDE Friendly
• Test Enrichment
• Classpath Control
• Drive the Browser
• Debug the Server
Arquillian
• Real Tests (no mocks)
• IDE Friendly
• Test Enrichment
• Classpath Control
• Drive the Browser
• Debug the Server
• Container agnostic
Arquillian
• Real Tests (no mocks)
• IDE Friendly
• Test Enrichment
• Classpath Control
• Drive the Browser
• Debug the Server
• Container agnostic
• Extensible platform
Arquillian
public class Greeter {
public void greet(PrintStream to, String name) {
to.println(createGreeting(name));
}
public String createGreeting(String name) {
return "Hello, " + name + "!";
}
}
Arquillian
public class Greeter {
public void greet(PrintStream to, String name) {
to.println(createGreeting(name));
}
public String createGreeting(String name) {
return "Hello, " + name + "!";
}
}
@RunWith(Arquillian.class)
public class GreeterTest {
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(Greeter.class)
.addAsManifestResource(EmptyAsset.INSTANCE,
"beans.xml");
}
@Inject
Greeter greeter;
@Test
public void should_create_greeting() {
Assert.assertEquals("Hello, Earthling!",
greeter.createGreeting("Earthling"));
}
}
Functional testing
Test customer requirements
Functional
Integration
Unit
SoapUI
SoapUI is a web service testing application
SoapUI
• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS
SoapUI is a web service testing application
SoapUI
• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS
• Allows security and load testing
SoapUI is a web service testing application
SoapUI
• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS
• Allows security and load testing
• Service mocking
SoapUI is a web service testing application
SoapUI
• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS
• Allows security and load testing
• Service mocking
• Logging of the test results
SoapUI is a web service testing application
SoapUI
• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS
• Allows security and load testing
• Service mocking
• Logging of the test results
• Groovy API
SoapUI is a web service testing application
SoapUI
public void testTestCaseRunner() throws Exception {
WsdlProject project = new WsdlProject( "src/dist/sample-soapui-project.xml" );
TestSuite testSuite = project.getTestSuiteByName( "Test Suite" );
TestCase testCase = testSuite.getTestCaseByName( "Test Conversions" );
// create empty properties and run synchronously
TestRunner runner = testCase.run( new PropertiesMap(), false );
assertEquals( Status.FINISHED, runner.getStatus() );
}
Selenium
Selenium is a portable software GUI testing framework for web applications
Selenium
• Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby.
Selenium is a portable software GUI testing framework for web applications
Selenium
• Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby.
• The tests can then be run against most modern web browsers
Selenium is a portable software GUI testing framework for web applications
Selenium
• Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby.
• The tests can then be run against most modern web browsers
• Selenium deploys on Windows, Linux, and Macintosh platforms
Selenium is a portable software GUI testing framework for web applications
Selenium
• Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby.
• The tests can then be run against most modern web browsers
• Selenium deploys on Windows, Linux, and Macintosh platforms
• Selenium provides a record/playback tool for authoring tests without learning a test
scripting language (Selenium IDE)
Selenium is a portable software GUI testing framework for web applications
Selenium
public class Selenium2Example {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
};
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
Summary
Functional
Integration
Unit
5%
15%
80%
TestNG
Hamcrest
Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (19)

Android Unit Test
Android Unit TestAndroid Unit Test
Android Unit Test
 
Java Quiz Questions
Java Quiz QuestionsJava Quiz Questions
Java Quiz Questions
 
Appium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionAppium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation Execution
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Testing with PostgreSQL
Testing with PostgreSQLTesting with PostgreSQL
Testing with PostgreSQL
 
Getting started with Java 9 modules
Getting started with Java 9 modulesGetting started with Java 9 modules
Getting started with Java 9 modules
 
Java8 tgtbatu javaone
Java8 tgtbatu javaoneJava8 tgtbatu javaone
Java8 tgtbatu javaone
 
Java Quiz - Meetup
Java Quiz - MeetupJava Quiz - Meetup
Java Quiz - Meetup
 
Gradle talk, Javarsovia 2010
Gradle talk, Javarsovia 2010Gradle talk, Javarsovia 2010
Gradle talk, Javarsovia 2010
 
Implementing quality in Java projects
Implementing quality in Java projectsImplementing quality in Java projects
Implementing quality in Java projects
 
Why Kotlin - Apalon Kotlin Sprint Part 1
Why Kotlin - Apalon Kotlin Sprint Part 1Why Kotlin - Apalon Kotlin Sprint Part 1
Why Kotlin - Apalon Kotlin Sprint Part 1
 
Qunit Java script Un
Qunit Java script UnQunit Java script Un
Qunit Java script Un
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languages
 
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
 
OSGi and Eclipse RCP
OSGi and Eclipse RCPOSGi and Eclipse RCP
OSGi and Eclipse RCP
 
Just Do It! ColdBox Integration Testing
Just Do It! ColdBox Integration TestingJust Do It! ColdBox Integration Testing
Just Do It! ColdBox Integration Testing
 
Unit testing with Qt test
Unit testing with Qt testUnit testing with Qt test
Unit testing with Qt test
 
Scaladays 2014 introduction to scalatest selenium dsl
Scaladays 2014   introduction to scalatest selenium dslScaladays 2014   introduction to scalatest selenium dsl
Scaladays 2014 introduction to scalatest selenium dsl
 

Andere mochten auch

Andere mochten auch (14)

Writing quick and beautiful automation code
Writing quick and beautiful automation codeWriting quick and beautiful automation code
Writing quick and beautiful automation code
 
Android Espresso
Android EspressoAndroid Espresso
Android Espresso
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest Matchers
 
Design principles in a nutshell
Design principles in a nutshellDesign principles in a nutshell
Design principles in a nutshell
 
Microservices: a journey of an eternal improvement
Microservices: a journey of an eternal improvementMicroservices: a journey of an eternal improvement
Microservices: a journey of an eternal improvement
 
Load-testing 101 for Startups with Artillery.io
Load-testing 101 for Startups with Artillery.ioLoad-testing 101 for Startups with Artillery.io
Load-testing 101 for Startups with Artillery.io
 
Search and analyze your data with elasticsearch
Search and analyze your data with elasticsearchSearch and analyze your data with elasticsearch
Search and analyze your data with elasticsearch
 
Going Serverless with CQRS on AWS
Going Serverless with CQRS on AWSGoing Serverless with CQRS on AWS
Going Serverless with CQRS on AWS
 
Choosing Hippo CMS
Choosing Hippo CMSChoosing Hippo CMS
Choosing Hippo CMS
 
Developing functional domain models with event sourcing (sbtb, sbtb2015)
Developing functional domain models with event sourcing (sbtb, sbtb2015)Developing functional domain models with event sourcing (sbtb, sbtb2015)
Developing functional domain models with event sourcing (sbtb, sbtb2015)
 
Microservice Architecture with CQRS and Event Sourcing
Microservice Architecture with CQRS and Event SourcingMicroservice Architecture with CQRS and Event Sourcing
Microservice Architecture with CQRS and Event Sourcing
 
Building and deploying microservices with event sourcing, CQRS and Docker (QC...
Building and deploying microservices with event sourcing, CQRS and Docker (QC...Building and deploying microservices with event sourcing, CQRS and Docker (QC...
Building and deploying microservices with event sourcing, CQRS and Docker (QC...
 
Developing microservices with aggregates (SpringOne platform, #s1p)
Developing microservices with aggregates (SpringOne platform, #s1p)Developing microservices with aggregates (SpringOne platform, #s1p)
Developing microservices with aggregates (SpringOne platform, #s1p)
 
Developing event-driven microservices with event sourcing and CQRS (svcc, sv...
Developing event-driven microservices with event sourcing and CQRS  (svcc, sv...Developing event-driven microservices with event sourcing and CQRS  (svcc, sv...
Developing event-driven microservices with event sourcing and CQRS (svcc, sv...
 

Ähnlich wie Testing basics for developers

Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
JAXLondon2014
 

Ähnlich wie Testing basics for developers (20)

JUnit5 and TestContainers
JUnit5 and TestContainersJUnit5 and TestContainers
JUnit5 and TestContainers
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
Web UI test automation instruments
Web UI test automation instrumentsWeb UI test automation instruments
Web UI test automation instruments
 
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworks
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworks
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
TestNG
TestNGTestNG
TestNG
 
Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5
 
Testing in android
Testing in androidTesting in android
Testing in android
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
 
Testing the Enterprise layers, with Arquillian
Testing the Enterprise layers, with ArquillianTesting the Enterprise layers, with Arquillian
Testing the Enterprise layers, with Arquillian
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Testing Java Code Effectively
Testing Java Code EffectivelyTesting Java Code Effectively
Testing Java Code Effectively
 
Advanced Java Testing
Advanced Java TestingAdvanced Java Testing
Advanced Java Testing
 
How to Build Your Own Test Automation Framework?
How to Build Your Own Test Automation Framework?How to Build Your Own Test Automation Framework?
How to Build Your Own Test Automation Framework?
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 

Kürzlich hochgeladen

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Kürzlich hochgeladen (20)

%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 

Testing basics for developers

  • 1. Testing basics for developers Anton Udovychenko 1 1 / 2 0 1 3
  • 2. Agenda • Motivation • Unit testing • JUnit, Mockito, Hamcrest, JsTestDriver • Integration testing • Persistence testing, Arquillian • Functional testing • SoapUI, Selenium • Q&A
  • 5. Unit testing Test small portions of production code Confidence to change Quick Feedback Documentation Functional Integration Unit
  • 7. JUnit lifecycle 1. @BeforeClass 2. For each @Test a) Instanciate test class b) @Before c) Invoke the test d) @After 3. @AfterClass
  • 8. JUnit advanced 1. @Rule and @ClassRule 2. Parametrized 3. Mocks 4. Hamcrest
  • 9. JUnit @Rule public class MyRule implements TestRule { @Override public Statement apply( Statement base, Description description ) { return new MyStatement( base ); } }
  • 10. JUnit @Rule public class MyStatement extends Statement { private final Statement base; public MyStatement( Statement base ) { this.base = base; } @Override public void evaluate() throws Throwable { System.out.println( "before" ); try { base.evaluate(); } finally { System.out.println( "after" ); } } } public class MyRule implements TestRule { @Override public Statement apply( Statement base, Description description ) { return new MyStatement( base ); } }
  • 11. JUnit @Rule public class MyTest { @Rule public MyRule myRule = new MyRule(); @Test public void testRun() { System.out.println( "during" ); } } public class MyRule implements TestRule { @Override public Statement apply( Statement base, Description description ) { return new MyStatement( base ); } } public class MyStatement extends Statement { private final Statement base; public MyStatement( Statement base ) { this.base = base; } @Override public void evaluate() throws Throwable { System.out.println( "before" ); try { base.evaluate(); } finally { System.out.println( "after" ); } } }
  • 12. JUnit @Rule public class MyTest { @Rule public MyRule myRule = new MyRule(); @Test public void testRun() { System.out.println( "during" ); } } public class MyRule implements TestRule { @Override public Statement apply( Statement base, Description description ) { return new MyStatement( base ); } } public class MyStatement extends Statement { private final Statement base; public MyStatement( Statement base ) { this.base = base; } @Override public void evaluate() throws Throwable { System.out.println( "before" ); try { base.evaluate(); } finally { System.out.println( "after" ); } } } Output: before during after
  • 13. JUnit @ClassRule @RunWith(Suite.class) @SuiteClasses({ TestCase1.class, TestCase2.class }) public class AllTests { @ClassRule public static Timeout timeout = new Timeout(3000); }
  • 14. JUnit Parametrized @RunWith(value = Parameterized.class) public class MyTest { private int number; public MyTest(int number) { this.number = number; } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } }; return Arrays.asList(data); } @Test public void pushTest() { System.out.println("Parameterized Number is : " + number); } }
  • 15. Mockito Mockito is a mocking framework for unit tests in Java
  • 16. Mockito import static org.mockito.Mockito.*; import static org.junit.Assert.*; import java.util.Iterator; import org.junit.Test; .... @Test public void iteratorWillReturnHelloWorld(){ //arrange Iterator i=mock(Iterator.class); when(i.next()).thenReturn("Hello").thenReturn("World"); //act String result=i.next()+" "+i.next(); //assert assertEquals("Hello World", result); }
  • 17. Hamcrest Hamcrest is a matchers framework that assists writing software tests
  • 20. Hamcrest assertTrue(foo.contains("someValue") && foo.contains("anotherValue")); assertThat(foo, hasItems("someValue", "anotherValue")); vs assertThat( table, column("Type",contains("A","B","C")).where(cell("Status", is("Ok"))) ); Another example:
  • 21. JsTestDriver JsTestDriver is an open source JavaScript unit tests runner
  • 23. Integration testing Test collaboration between components Database IO system Special environment configuration Functional Integration Unit
  • 24. Persistence testing In memory databases DBUnit
  • 25. Arquillian Arquillian is a platform that simplifies integration testing for Java middleware
  • 27. Arquillian • Real Tests (no mocks) • IDE Friendly
  • 28. Arquillian • Real Tests (no mocks) • IDE Friendly • Test Enrichment
  • 29. Arquillian • Real Tests (no mocks) • IDE Friendly • Test Enrichment • Classpath Control
  • 30. Arquillian • Real Tests (no mocks) • IDE Friendly • Test Enrichment • Classpath Control • Drive the Browser
  • 31. Arquillian • Real Tests (no mocks) • IDE Friendly • Test Enrichment • Classpath Control • Drive the Browser • Debug the Server
  • 32. Arquillian • Real Tests (no mocks) • IDE Friendly • Test Enrichment • Classpath Control • Drive the Browser • Debug the Server • Container agnostic
  • 33. Arquillian • Real Tests (no mocks) • IDE Friendly • Test Enrichment • Classpath Control • Drive the Browser • Debug the Server • Container agnostic • Extensible platform
  • 34. Arquillian public class Greeter { public void greet(PrintStream to, String name) { to.println(createGreeting(name)); } public String createGreeting(String name) { return "Hello, " + name + "!"; } }
  • 35. Arquillian public class Greeter { public void greet(PrintStream to, String name) { to.println(createGreeting(name)); } public String createGreeting(String name) { return "Hello, " + name + "!"; } } @RunWith(Arquillian.class) public class GreeterTest { @Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addClass(Greeter.class) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } @Inject Greeter greeter; @Test public void should_create_greeting() { Assert.assertEquals("Hello, Earthling!", greeter.createGreeting("Earthling")); } }
  • 36. Functional testing Test customer requirements Functional Integration Unit
  • 37. SoapUI SoapUI is a web service testing application
  • 38. SoapUI • Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS SoapUI is a web service testing application
  • 39. SoapUI • Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS • Allows security and load testing SoapUI is a web service testing application
  • 40. SoapUI • Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS • Allows security and load testing • Service mocking SoapUI is a web service testing application
  • 41. SoapUI • Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS • Allows security and load testing • Service mocking • Logging of the test results SoapUI is a web service testing application
  • 42. SoapUI • Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS • Allows security and load testing • Service mocking • Logging of the test results • Groovy API SoapUI is a web service testing application
  • 43. SoapUI public void testTestCaseRunner() throws Exception { WsdlProject project = new WsdlProject( "src/dist/sample-soapui-project.xml" ); TestSuite testSuite = project.getTestSuiteByName( "Test Suite" ); TestCase testCase = testSuite.getTestCaseByName( "Test Conversions" ); // create empty properties and run synchronously TestRunner runner = testCase.run( new PropertiesMap(), false ); assertEquals( Status.FINISHED, runner.getStatus() ); }
  • 44. Selenium Selenium is a portable software GUI testing framework for web applications
  • 45. Selenium • Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby. Selenium is a portable software GUI testing framework for web applications
  • 46. Selenium • Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby. • The tests can then be run against most modern web browsers Selenium is a portable software GUI testing framework for web applications
  • 47. Selenium • Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby. • The tests can then be run against most modern web browsers • Selenium deploys on Windows, Linux, and Macintosh platforms Selenium is a portable software GUI testing framework for web applications
  • 48. Selenium • Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby. • The tests can then be run against most modern web browsers • Selenium deploys on Windows, Linux, and Macintosh platforms • Selenium provides a record/playback tool for authoring tests without learning a test scripting language (Selenium IDE) Selenium is a portable software GUI testing framework for web applications
  • 49. Selenium public class Selenium2Example { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Cheese!"); element.submit(); System.out.println("Page title is: " + driver.getTitle()); new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }; System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } }