Quality assurance and testing are very important in a life cycle of any application. Although, by far not all developers understand the significance of tests.
In this presentation, we cover the basic testing practices for developers. The following tools are discussed: JUnit, Mockito, Hamcrest, JsTestDriver, DBUnit, Arquillian, SoapUI, Selenium.
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
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"));
}
}
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() );
}
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();
}
}