Merge pull request #1024 from vorburger/SpringConfigurationTest
New SpringConfigurationTest; the first @RunWith(SpringJUnit4ClassRunner) @vishwasbabu and @mgeiss I'm assuming you have no objection to this one, so I've merged it.
This commit is contained in:
@@ -228,6 +228,9 @@ tomcatRunWar {
|
||||
configFile = file('src/test/resources/META-INF/context.xml')
|
||||
}
|
||||
|
||||
/* http://stackoverflow.com/questions/19653311/jpa-repository-works-in-idea-and-production-but-not-in-gradle */
|
||||
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
|
||||
sourceSets.test.output.resourcesDir = sourceSets.test.output.classesDir
|
||||
|
||||
sourceSets {
|
||||
integrationTest {
|
||||
|
||||
@@ -63,6 +63,11 @@
|
||||
|
||||
<import resource="infrastructure.xml" />
|
||||
|
||||
<!-- do NOT <import resource="jndi.xml" /> here;
|
||||
because we want the TestDataSourceConfiguration to not have that;
|
||||
we'll manually list jndi.xml in web.xml
|
||||
-->
|
||||
|
||||
<import resource="securityContext.xml" />
|
||||
|
||||
<import resource="cache.xml" />
|
||||
|
||||
@@ -40,6 +40,4 @@
|
||||
<property name="transactionManager" ref="transactionManager"></property>
|
||||
</bean>
|
||||
|
||||
<jee:jndi-lookup jndi-name="java:comp/env/jdbc/mifosplatform-tenants"
|
||||
id="tenantDataSourceJndi" />
|
||||
</beans>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xmlns:jee="http://www.springframework.org/schema/jee"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
|
||||
|
||||
<!-- name here must match TestDataSourceConfiguration -->
|
||||
<jee:jndi-lookup jndi-name="java:comp/env/jdbc/mifosplatform-tenants"
|
||||
id="tenantDataSourceJndi" />
|
||||
</beans>
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath*:META-INF/spring/appContext.xml</param-value>
|
||||
<param-value>classpath*:META-INF/spring/jndi.xml classpath*:META-INF/spring/appContext.xml</param-value>
|
||||
</context-param>
|
||||
|
||||
<filter>
|
||||
@@ -36,7 +36,7 @@
|
||||
</listener>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>jersey-serlvet</servlet-name>
|
||||
<servlet-name>jersey-servlet</servlet-name>
|
||||
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
|
||||
@@ -54,7 +54,7 @@
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>jersey-serlvet</servlet-name>
|
||||
<servlet-name>jersey-servlet</servlet-name>
|
||||
<url-pattern>/api/v1/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
</web-app>
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package org.mifosplatform.infrastructure.configuration.spring;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* This integration test ensure that the "production" Spring XML configuration
|
||||
* files (appContext.xml & Co.) are valid. It does not need any database for
|
||||
* that.
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestsWithoutDatabaseAndNoJobsConfiguration.class)
|
||||
public class SpringConfigurationTest {
|
||||
|
||||
/**
|
||||
* This tests if "Aal izz well" (i.e. if we can start-up with the [almost]
|
||||
* "production" Spring XML configuration) by doing nothing - it doesn't have
|
||||
* to, as the SpringJUnit4ClassRunner with this @ContextConfiguration will
|
||||
* automatically fail if e.g. there is any invalid Spring XML, an invalid
|
||||
* bean definition somewhere or anything like that.
|
||||
*
|
||||
* @see https://www.google.ch/search?q=Aal+izz+well
|
||||
*/
|
||||
@Test
|
||||
public void testSpringXMLConfiguration() {}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package org.mifosplatform.infrastructure.configuration.spring;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.mifosplatform.infrastructure.core.service.TenantDatabaseUpgradeService;
|
||||
import org.mifosplatform.infrastructure.jobs.service.JobRegisterService;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
|
||||
/**
|
||||
* Spring @Configuration which does not require a running database. It also does
|
||||
* not load any job configuration (as they are in the DB), thus nor starts any
|
||||
* background jobs. For some integration tests, this may be perfectly sufficient
|
||||
* (and faster to run such tests).
|
||||
*/
|
||||
@Configuration
|
||||
// We do NOT want the testContext.xml here, but the real one!
|
||||
@ImportResource("classpath*:META-INF/spring/appContext.xml")
|
||||
public class TestsWithoutDatabaseAndNoJobsConfiguration {
|
||||
|
||||
/**
|
||||
* Override TenantDatabaseUpgradeService binding, because the real one has a @PostConstruct
|
||||
* upgradeAllTenants() which accesses the database on start-up.
|
||||
*/
|
||||
@Bean
|
||||
public TenantDatabaseUpgradeService tenantDatabaseUpgradeService() {
|
||||
return new TenantDatabaseUpgradeService(null) {
|
||||
|
||||
@Override
|
||||
public void upgradeAllTenants() {
|
||||
// NOOP
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Override JobRegisterService binding, because the real
|
||||
* JobRegisterServiceImpl has a @PostConstruct loadAllJobs() which accesses
|
||||
* the database on start-up.
|
||||
*/
|
||||
@Bean
|
||||
public JobRegisterService jobRegisterServiceImpl() {
|
||||
JobRegisterService mockJobRegisterService = Mockito.mock(JobRegisterService.class);
|
||||
return mockJobRegisterService;
|
||||
}
|
||||
|
||||
/**
|
||||
* DataSource with Mockito RETURNS_MOCKS black magic.
|
||||
*/
|
||||
@Bean
|
||||
public DataSource tenantDataSourceJndi() {
|
||||
DataSource mockDataSource = Mockito.mock(DataSource.class, Mockito.RETURNS_MOCKS);
|
||||
return mockDataSource;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user