0

Spring newbie here. Trying to meld various online spring tutorials into a single working project. Started off with the spring mvc tutorial described in this link.

Now I'm trying to meld this online tutorial on Spring data jpa into the above project. As you may see in the attached figure, I've added all necessary project files (src, test, .xml files etc).

My issue is that when I do mvn package (which is what I've been using to build and run the tests), the new suite of tests for the second project do not get executed. I wonder why! Without giving away all the sources, I will show some of the most important configuration (.xml) files.

Picture showing new files added, existing files, and brief description of issue: enter image description here Note: Sorry, but some of the xml meta-data are being removed by stackoverflow. They are mostly header tags that import xml definitions and thus not that important.

  1. orm.xml (added for spring-data-jpa exercise)

    <?xml version="1.0" encoding="UTF-8"?>
    

    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <entity-listeners>
                <entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
            </entity-listeners>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
    
    <named-query name="Person.findByName">
        <query>select p from Person p where p.firstName = :firstName AND p.lastName = :lastName</query>
    </named-query>
    

  2. persistence.xml (added for spring-data-jpa exercise)

    <?xml version="1.0" encoding="UTF-8"?>
    

    http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd" version="2.1">

    <persistence-unit name="hsql">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
            <property name="jadira.usertype.autoRegisterUserTypes" value="true" />
            <property name="jadira.usertype.databaseZone" value="jvm" />
        </properties>
    </persistence-unit>
    

  3. root-context.xml (initially there for spring-mvc-tutorial)

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->
    
    <!--
        CSRF protection. Here we only include the CsrfFilter instead of all of Spring Security.
        See http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#csrf for more information on
        Spring Security's CSRF protection
    -->
    <bean id="csrfFilter" class="org.springframework.security.web.csrf.CsrfFilter">
        <constructor-arg>
            <bean class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository"/>
        </constructor-arg>
    </bean>
    <!--
        Provides automatic CSRF token inclusion when using Spring MVC Form tags or Thymeleaf. See
        http://localhost:8080/#forms and form.jsp for examples
    -->
    <bean id="requestDataValueProcessor" class="org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor"/>
    

4.) controllers.xml, 5.)servlet-context.xml files are just standard files that map controllers to path, define and instantiate the dispatcher-servlet, and other beans which were already in the spring-mvc-tutorial code. I believe they don't really have anything to do with the issue described in this ticket.

Here is the test file that doesn't execute, and I'd like to have running:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration
@Transactional
public class PersonRepositoryTest {

    final Logger logger = LoggerFactory.getLogger(PersonRepositoryTest.class);

    private final static Integer FIRST_ID = Integer.valueOf(1);
    private final static Integer SECOND_ID = Integer.valueOf(2);

    private final static int EXPECTED_COUNT = 3;

    private final static String FIRST_NAME = "Joe";
    private final static String LAST_NAME = "Smith";

    private final static int EXPECTED_ADDRESS_COUNT = 1;
    private final static String ADDR = "1060 West Addison St.";
    private final static String CITY = "Chicago";
    private final static String STATE = "IL";
    private final static String ZIP_POSTAL = "60613";
    private final static String COUNTRY = "USA";

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private ProfessionalRepository professionalRepository;

    @Test
    public void testFindOne() {
        Person person = personRepository.findOne(FIRST_ID);

        testPersonOne(person);
    }
    ...
    .. //more tests

Any help is much appreciated!

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.samples</groupId>
    <artifactId>spring-mvc-showcase</artifactId>
    <name>spring-mvc-showcase</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.7</java-version>
        <org.springframework-version>4.2.2.RELEASE</org.springframework-version>
        <org.springframework.security-version>4.0.1.RELEASE</org.springframework.security-version>
        <org.aspectj-version>1.8.1</org.aspectj-version>
        <org.slf4j-version>1.7.12</org.slf4j-version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>7.0.30</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Jackson JSON Processor -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>

        <!-- Rome Atom+RSS -->
        <dependency>
            <groupId>com.rometools</groupId>
            <artifactId>rome</artifactId>
            <version>1.5.0</version>
        </dependency>

        <!-- JSR 303 with Hibernate Validator -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.1.0.Final</version>
        </dependency>

        <!-- Joda Time Library -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.3</version>
        </dependency>

        <!-- File Upload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>

        <!-- Security (used for CSRF protection only) -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${org.springframework.security-version}</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>xmlunit</groupId>
            <artifactId>xmlunit</artifactId>
            <version>1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>0.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <!--Additional POM for spring-data-jpa trial -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>

    </dependencies>
    <repositories>
        <!-- For testing against latest Spring snapshots -->
        <repository>
            <id>org.springframework.maven.snapshot</id>
            <name>Spring Maven Snapshot Repository</name>
            <url>http://repo.spring.io/snapshot</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <!-- For developing against latest Spring milestones -->
        <repository>
            <id>org.springframework.maven.milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </repositories>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                    </includes>
                    <excludes>
                        <exclude>**/Abstract*.java</exclude>
                    </excludes>
                    <junitArtifactName>junit:junit</junitArtifactName>
                    <argLine>-Xmx512m</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <!-- Have to use version 1.2 since version 1.3 does not appear to work with ITDs -->
                <version>1.2</version>
                <dependencies>
                    <!-- You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outxml>true</outxml>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.0.6.v20130930</version>
                <configuration>
                    <webApp>
                        <contextPath>/${project.artifactId}</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
apil.tamang
  • 2,545
  • 7
  • 29
  • 40
  • Executing test classes during maven build phase is nothing to do with persistance API or spring bean configuration. Please post content of POM.xml file. – Pratap A.K Jan 20 '16 at 03:30
  • Also noticed, this may be because of your project structure. I believe your base package is "org.springframework" under which your application.java resides and for JPA entity your package structure is org.springbyexample.orm.entity. In this case spring boot will not scan all the packages. Include JPA things inside package "org.springframework.entities" and "org.springframework.repositories", then try once. – Pratap A.K Jan 20 '16 at 03:36
  • I changed the root name of both the packages to com.apil.springapp ('root-pkg') . Hence, everything lives under 'root-pkg' now. Running mvn package still only runs the 84 unit tests under /test/'root-pkg'/samples.mvc, /test/'root-pkg'/web.app.user, but not under /test/'root-pkg'/orm. Any idea what's going on? Thanks! – apil.tamang Jan 21 '16 at 02:57

1 Answers1

1

So in your pom you are having

enter image description here

and the class which your saying not executing ending with only test.java.

So rename PersonRepositoryTest.java to PersonRepositoryTests.java and try again.

Pratap A.K
  • 4,337
  • 11
  • 42
  • 79
  • Sure that worked, thanks! And after fiddling around with context files for about an hour, I'm still no-where. Too many errors and concepts to take in at once. Think I'll take a more gradual and structured approach. – apil.tamang Jan 21 '16 at 19:16
  • Better you use spring boot. It helps for faster development and easy configuration and no need to worry about dependencies as it will take care all those. – Pratap A.K Jan 22 '16 at 03:12
  • Sure. Been doing that now. But I realized that now I don't see (or need) configurations for a large number of beans file I'd see otherwise in non-springboot applications. Examples: dataSource, entityManagerFactory, JpaTransactionManager etc. for dealing with data-jpa. Is it ultimately beneficial (or not) in your opinion to be hidden from these details? – apil.tamang Jan 22 '16 at 16:45
  • Nothing hidden from us. Still you can do XML based configuration in spring boot. Main advantage of boot is Bootstrapping with defaults included in the configuration / jar-dependencies. Its just another project from Spring framework, where things look simplified, with strong support for Security, Data, Social etc all features you want for your application. You can go through https://www.quora.com/Reviews-of-Spring-Boot http://stackoverflow.com/questions/28831479/advantage-of-spring-boot to explore it. – Pratap A.K Jan 22 '16 at 16:54