Archive

Archive for the ‘TestNG’ Category

Realworld JSF Application Story

Monday, 27. April 2009 Andreas Höhmann Leave a comment

Vielleicht hatte ich es in einem früheren Posting schonmal erwähnt, ich arbeite momentan für Siemens an einem Web 2.0 Projekt und das ist jetzt fertig :D .

Los ging es November 2007 und nun konnten wir planmässig im April 2009 live gehen. Wir konnten dabei all die wunderbaren “neuen” Technologien einsetzen und ausprobieren, u.a. JSF und damit eine funktionsfähige (also den Anforderungen des Kunden voll entsprechende) und zugleich performante “Web 2.0″ Applikation bauen. Die Applikation nimmt sich dem Thema “Sicherheitstechnik” (für die Auskenner: ISO 13849-1 und IEC 62061) an.

Mal kurz zu den verwendeten “Technologien/Frameworks/Tools/Ideen”:

  • Daten ziehen wir aus einer SAP Knowledgebase (selbst gestrickt) und aus einer Datenbank via OpenJPA
  • Services/Manager sind “normale” Javaklassen (Stichwort Design-Pattern),
    als Kleber verwenden wir Spring (Stichwort Dependency Injection)
  • UI mit JBoss Richfaces (Stichwort Ajax), JSF + Facelets (Stichwort Xhtml-Template), jQuery, CSS, etc.
  • Buildsystem, Projektseiten mit Maven2
  • Continues Integration via TeamCity und Maven2
  • Tests hauptsächlich mit TestNG aber teilweise auch mit JUnit
  • Laden/Speichern von Benutzerdaten via XStream (FileupLoad via Tomahawk), das alles noch Versionskompatibel, d.h. selbsgestrickte XML-Transformationskette für DomainModell-Updates
  • PDF Reportgenerierung mit iText
  • Anbindung an Siemens “Single Sign On” System via Spring Security, Webservices

Im Rückblick kann ich sagen, dass eigentlich alles ohne größere Probleme miteinander funktioniert hat
… so soll es sein :D

https://www.automation.siemens.com/cd/safety/html_00/produkte/si_normen/tool.htm

(Für die Applikation muss man sich registrieren)

Using Session/Request-Scoped SpringBeans in TestNG

Wednesday, 15. October 2008 Andreas Höhmann Leave a comment

Today i will show you how to use spring-beans (especially session-scoped respectively request-scoped) in Unittests.

First of all … its easy to use a singleton bean (no scope) in a unit-test, load the ClassPathXmlApplicationContext with configLocations, use getBean(“name”) and then use the returned objects.

But if you want use a enhanced (J2EE) spring-configuration in your unit-test you have to use the XmlWebApplicationContext.

Lets use a simple example! Imagine you have different context-files, e.g. spring-beans-application.xml:

  <bean id="Foobar1" class="de.ahoehma.test.Foobar" scope="request"/>
  <bean id="Foobar2" class="de.ahoehma.test.Foobar" scope="session"/>
  <bean id="Foobar3" class="de.ahoehma.test.Foobar" />

Then you have your unit-test:

public class SpringBeansTestNg {

  //
  // XXX we have to define all spring-context-files - later this could be done via test-ng-provider or
  //     via spring with "spring-beans-*.xml" (i try it but it doesnt work)
  //
  private final String[] contextLocations = new String[]{
      "spring-beans-application.xml",
      "spring-beans-services.xml",
      "spring-beans-persistence.xml",
      "spring-beans-security.xml",};

  private ApplicationContext applicationContext;

  @BeforeTest
  private void loadApplicationContext() {
    final XmlWebApplicationContext xmlApplicationContext = new XmlWebApplicationContext();
    xmlApplicationContext.setConfigLocations(contextLocations);
    final MockServletContext servletContext = new MockServletContext("");
    xmlApplicationContext.setServletContext(servletContext);
    final RequestContextListener requestContextListener = new RequestContextListener();
    final MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    final ServletRequestEvent requestEvent = new ServletRequestEvent(servletContext, request);
    requestContextListener.requestInitialized(requestEvent);
    xmlApplicationContext.refresh();
    applicationContext = xmlApplicationContext;
  }

  /**
   * @return request scoped bean
   */
  private Foobar getFoobar1() {
    return (Foobar) applicationContext.getBean("Foobar1"); //$NON-NLS-1$
  }

 /**
   * @return session scoped bean
   */
  private Foobar getFoobar2() {
    return (Foobar) applicationContext.getBean("Foobar2"); //$NON-NLS-1$
  }

 /**
   * @return singleton bean
   */
  private Foobar getFoobar3() {
    return (Foobar) applicationContext.getBean("Foobar3"); //$NON-NLS-1$
  }
}

The magic happend in the loadApplicationContext.

Try it :)