Tagged: unit testing

Overriding JTA into RESOUCE-LOCAL while Unit Testing


references

JTA

Let’s say you have a very simple persistence.xml looks like this.

<persistence-unit name="somePU" transaction-type="JTA">
  <jta-data-source>jdbc/someDS</jta-data-source>
  <exclude-unlisted-classes>false</exclude-unlisted-classes>
  <properties>
    <property name="javax.persistence.validation.mode" value="CALLBACK" />
  </properties>
</persistence-unit>

RESOUCE-LOCAL

You can, maybe you should, override it for unit testing. Following codes uses EclipseLink dependent API.

final Map<String, String> properties = new HashMap<>();
properties.put(PersistenceUnitProperties.TRANSACTION_TYPE,
               PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
properties.put(PersistenceUnitProperties.JDBC_DRIVER,
               "org.apache.derby.jdbc.EmbeddedDriver");
properties.put(PersistenceUnitProperties.JDBC_URL,
               "jdbc:derby:memory:testDB;create=true");
properties.put(PersistenceUnitProperties.DDL_GENERATION,
               PersistenceUnitProperties.CREATE_ONLY);
properties.put(PersistenceUnitProperties.LOGGING_LEVEL,
               Level.FINE.getName());
properties.put(PersistenceUnitProperties.TARGET_SERVER,
               TargetServer.None);
ENTITY_MANAGER_FACTORY
    = createEntityManagerFactory("somePU", properties);

launching 3rd party servlets using jetty for unit testing


References

Apache Maven

<dependency>
  <!-- I'm not sure this is the minimum dependency -->
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-servlet-tester</artifactId>
  <scope>test</scope>
</dependency>

TestNG


    private static final PORT;

    static {
        PORT = ThreadLocalRandom.current().nextInt(1024, 65536);
    }

    private static Server SERVER;

    @BeforeClass
    public static void startServer() throws Exception {
        SERVER = new Server(PORT);
        final ServletHandler handler = new ServletHandler();
        SERVER.addServeltHandler(handler);
        handler.addServletWithMapping(SomeServlet.class, "/some");
        SERVER.start();
    }

    @AfterClass
    public static void stopServer() throws Exception {
        SERVER.stop();
        SERVER = null;
    }

    @Test
    public void test() throws MalformedURLException {
        final URL url = new URL("http://localhost:" + PORT + "/some");
    }

profiling maven tests


I have a very special case of profiling test cases.

ūnus

Note that following example doesn’t work!

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>target13</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <skipTests>false</skipTests>
              <includes>
                <include>com/googlecode/jinahya/test/Target13Test.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile> 
    <profile>
      <id>target14</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <skipTests>false</skipTest>
              <includes>
                <include>com/googlecode/jinahya/test/Target14Test.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

Simply failed. I found an issue which is marked as Fixed but this documentation update is what we should read.

duo

There is a work-around for this problem.

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/*.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>target13</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <excludes>
                <exclude>NONE</exclude>
              </excludes>
              <includes>
                <include>com/googlecode/jinahya/test/Target13Test.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile> 
    <profile>
      <id>target14</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <excludes>
                <exclude>NONE</exclude>
              </excludes>
              <includes>
                <include>com/googlecode/jinahya/test/Target14Test.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

It works.

trēs

This is how I solved my problem.

<project>
  <properties>
    <skipTests>true</skipTests>
  </properties>
  <build>
    <plugins>
      <!--
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration/>
      </plugin>
      -->
    </plugins>
  </build>
  <profiles>
    <properties>
      <skipTests>false</skipTests>
    </properties>
    <profile>
      <id>target13</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <includes>
                <include>com/googlecode/jinahya/test/Target13Test.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile> 
    <profile>
      <id>target14</id>
      <properties>
        <skipTests>false</skipTests>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <includes>
                <include>com/googlecode/jinahya/test/Target14Test.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>