2

I'm using struts2 junit 2.3.12 plugin.

  1. If I run test directly, then I get :

java.lang.NoClassDefFoundError: javax/servlet/ServletContext

so I include

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

in my POM, after which I get :

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException

so, I remove the above dependency and add :

 <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

after which I get

SEVERE:   [53:22.549] Dispatcher initialization failed

Below is my test :

public class TestAction extends StrutsTestCase {

    @Test
    public void testRegister() throws Exception {
        ActionProxy proxy = getActionProxy("/gotoregister");
        assertNotNull(proxy);

        proxy = getActionProxy("/registeraction");
        UserAction action = (UserAction) proxy.getAction();
        assertNotNull(action);
        request.setParameter("usernameexists", "true");
        request.setParameter("emailexists", "false");
        request.setParameter("basicinfo.firstname", "fname");
        request.setParameter("basicinfo.lastname", "lname");
        request.setParameter("user.username", "test");
        request.setParameter("user.email", "[email protected]");
        request.setParameter("user.passwd", "nn");
        request.setParameter("repasswd", "nn");
        request.setParameter("gender", "nn");
        request.setParameter("basicinfo.country", "India");
        String result = action.execute();
        assertEquals("input", result);
        System.out.println(action.getActionErrors());
        System.out.println(action.getActionMessages());
    }
}

Please help me in writing a successful test.

[EDIT] - I thought I should add my complete POM, so below is maximum part of the POM :

<repositories>
        <repository>
            <id>java.net2</id>
            <name>Repository hosting the jee6 artifacts</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.5</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

<!--        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>-->
<!--        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>-->
<!--        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8-beta4</version>
        </dependency>-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
<!--        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>-->

<!--        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.1</version>
        </dependency>-->
<!--        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-servlet</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>2.1.4</version>
        </dependency>-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-tiles-plugin</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-junit-plugin</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.1</version>
        </dependency>
<!--        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.2.5.ga</version>
        </dependency>-->
<!--        <dependency>
            <groupId>javax.sql</groupId>
            <artifactId>jdbc-stdext</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.0.1B</version>
        </dependency>-->

<!--        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>-->
        <!--
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
            <scope>test</scope>
        </dependency>-->
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

<!--        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit-dep</artifactId>
            <version>4.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jmock</groupId>
            <artifactId>jmock-junit4</artifactId>
            <version>2.6.0</version>
            <scope>test</scope>
        </dependency>-->
<!--        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <version>3.8.2</version>
            <scope>test</scope>
        </dependency>-->
    </dependencies>
    <build>
        <plugins>
<!--            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.14</version>
                <configuration>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                    <webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.bio.SocketConnector">
                            <port>8080</port>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1-beta-1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
        <finalName>GoldenTraderMaven</finalName>
    </build>
    <profiles>
        <profile>
            <id>endorsed</id>
            <activation>
                <property>
                    <name>sun.boot.class.path</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.0.2</version>
                        <configuration>
                             javaee6 contains upgrades of APIs contained within the JDK itself.
                                 As such these need to be placed on the bootclasspath, rather than classpath of the
                                 compiler.
                                 If you don't make use of these new updated API, you can delete the profile.
                                 On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.
                            <compilerArguments>
                                <bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
                            </compilerArguments>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>6.0</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <netbeans.hint.deploy.server>Tomcat60</netbeans.hint.deploy.server>
        <struts.version>2.3.12</struts.version>
        <hibernate.version>3.6.10.Final</hibernate.version>
    </properties>
</project>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
coding_idiot
  • 13,526
  • 10
  • 65
  • 116
  • 1
    Try with `javax.servlet:servlet-api` dependency. – Aleksandr M Apr 05 '13 at 08:34
  • 1
    I tried that, after which I got `java.lang.NoClassDefFoundError: javax/servlet/jsp/PageContext` so I included jsp-api too in my POM and then it seems to execute test fine. Thank you so much for urgent response. – coding_idiot Apr 05 '13 at 08:43
  • 1
    Yep, forgot about jsp-api. Added my comment as an answer. You can accept it if it helped you. – Aleksandr M Apr 05 '13 at 08:50

1 Answers1

6

You need to add dependencies of javax.servlet:servlet-api and javax.servlet.jsp:jsp-api to your project pom.xml file.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.4</version>
    <type>jar</type>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1</version>
    <type>jar</type>
    <scope>test</scope>
</dependency>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143