Upload
anton-udovychenko
View
194
Download
1
Embed Size (px)
Citation preview
Testing basics for developers
Anton Udovychenko11 / 2013
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 codeConfidence to change
Quick FeedbackDocumentation
Functional
Integration
Unit
Unit testing
TestNG
JUnit lifecycle
1. @BeforeClass2. For each @Test
a) Instanciate test classb) @Beforec) Invoke the testd) @After
3. @AfterClass
JUnit advanced
1. @Rule and @ClassRule2. Parametrized 3. Mocks4. Hamcrest
JUnit @Rule
public class MyRule implements TestRule { @Overridepublic Statement apply( Statement base, Description description ) {
return new MyStatement( base );}
}
JUnit @Rulepublic class MyStatement extends Statement {
private final Statement base;public MyStatement( Statement base ) {
this.base = base;}
@Overridepublic void evaluate() throws Throwable {
System.out.println( "before" );try { base.evaluate();} finally { System.out.println( "after" );}
}}
public class MyRule implements TestRule { @Overridepublic 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 { @Overridepublic 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;}
@Overridepublic void evaluate() throws Throwable {
System.out.println( "before" );try {
base.evaluate();} finally {
System.out.println( "after" );}
}}
JUnit @Rulepublic class MyTest {
@Rule public MyRule myRule = new MyRule(); @Test public void testRun() { System.out.println( "during" ); }
}
public class MyRule implements TestRule { @Overridepublic 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;}
@Overridepublic void evaluate() throws Throwable {
System.out.println( "before" );try { base.evaluate();} finally { System.out.println( "after" );}
}}
Output:beforeduringafter
JUnit @ClassRule
@RunWith(Suite.class)@SuiteClasses({ TestCase1.class, TestCase2.class })public class AllTests {
@ClassRulepublic 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
Mockitoimport static org.mockito.Mockito.*;import static org.junit.Assert.*;import java.util.Iterator;import org.junit.Test;....
@Testpublic void iteratorWillReturnHelloWorld(){
//arrangeIterator i=mock(Iterator.class);
when(i.next()).thenReturn("Hello").thenReturn("World");//actString result=i.next()+" "+i.next();//assertassertEquals("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
HamcrestassertTrue(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 componentsDatabaseIO 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
Seleniumpublic 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?