Annotation Interface BroadleafSiteIntegrationTest


@Target(TYPE) @Retention(RUNTIME) @Documented @ContextConfiguration(name="siteRoot", initializers=org.broadleafcommerce.common.config.BroadleafEnvironmentConfiguringApplicationListener.class, classes=SiteTestContextConfiguration.class) @WebAppConfiguration @ActiveProfiles("mbeansdisabled") public @interface BroadleafSiteIntegrationTest

Convenient annotation for integration tests dealing with the Site applicationContext. This can be used to annotate any type of test that uses spring-test (e.g. TestNG, Spock, JUnit).

If you need to customize the application context to add your own configuration (and not only use the Broadleaf out of the box beans) you will need to use one of the superclasses, JUnitSiteIntegrationSetup or TestNGSiteIntegrationSetup. Otherwise you will not get the inheriting strategy with all of the Broadleaf beans plus your custom ones.

Example usage:

JUnit

 @RunWith(SpringRunner.class)
 @BroadleafSiteIntegrationTest
 public class ExampleBroadleafJUnitTest {
     
     @Autowired
     private CatalogService catalogService;
     
     @Test
     public void catalogServiceInjected() {
         Assert.assertNotEquals(catalogService, null);
     }
 }
 

TestNG

 @BroadleafSiteIntegrationTest
 public class ExampleBroadleafTestNGTest extends AbstractTestNGSpringContextTests {
     
     @Autowired
     private CatalogService catalogService;
     
     @Test
     public void catalogServiceInjected() {
         Assert.assertNotEquals(catalogService, null);
     }
 }
 

Spock

 @BroadleafSiteIntegrationTest
 class SpockExampleTest extends Specification {

     @Resource
     private CatalogService catalogService

     def "Test injection works"() {
         when: "The test is run"
         then: "The catalogService is injected"
         catalogService != null
     }
 }
 

Example usage with a customized ApplicationContext (additional beans, overrides, etc):

JUnit

 @ContextHierarchy(@ContextConfiguration(name = BroadleafSiteIntegrationTest.CONTEXT_NAME))
 public class ExampleBroadleafJUnitTest extends JUnitSiteIntegrationSetup {
     
     @Configuration
     public static class CustomConfiguration {
         @Bean
         public CatalogService blCatalogService() {
             return MyCatalogService();
         }
     }
     
     @Autowired
     private CatalogService catalogService;
     
     @Test
     public void catalogServiceInjected() {
         Assert.assertTrue(MyCatalogService.class.isAssignableFrom(catalogService.getClass()));
     }
 }
 

TestNG

 @ContextHierarchy(@ContextConfiguration(name = BroadleafSiteIntegrationTest.CONTEXT_NAME))
 public class ExampleBroadleafTestNGTest extends AbstractTestNGSpringContextTests {
     
     @Configuration
     public static class CustomConfiguration {
         @Bean
         public CatalogService blCatalogService() {
             return MyCatalogService();
         }
     }
     
     @Autowired
     private CatalogService catalogService;
     
     @Test
     public void catalogServiceInjected() {
         Assert.assertTrue(MyCatalogService.class.isAssignableFrom(catalogService.getClass()));
     }
 }
 

Spock

 @ContextHierarchy(@ContextConfiguration(name = BroadleafSiteIntegrationTest.CONTEXT_NAME))
 class SpockExampleTest extends SpockSiteIntegrationSetup {
 
     @Configuration
     public static class CustomConfiguration {
         @Bean
         public CatalogService blCatalogService() {
             return MyCatalogService();
         }
     }
     
     @Resource
     private CatalogService catalogService

     def "Test injection works"() {
         when: "The test is run"
         then: "The catalogService is an instance of my override"
         MyCatalogService.class.isAssignableFrom(catalogService.getClass())
     }
 }
 

When used within the Enterprise module, you cannot use both this annotation along with BroadleafAdminIntegrationTest. This is because class transformation can be different depending on the context. For this reason, you usually need to split out your "site" and "admin" tests into different JVM runs. This can be done with the following surefire configuration in Maven that brings everything contained within a package that contains adminjvm into a completely separate JVM execution from anything in sitejvm.

 <executions>
   <execution>
      <id>default-test</id>
      <configuration>
          <includes>
              <!-- Include all the default Surefire tests -->
              <include>**/Test*.java,**/*Test.java,**/*TestCase.java,**/*Spec*</include>
          </includes>
          <excludes>
              <exclude>**/adminjvm/**,**/sitejvm/**,**/browsertests/**</exclude>
          </excludes>
      </configuration>
  </execution>
  <execution>
      <id>admin-only-test</id>
      <goals>
          <goal>test</goal>
      </goals>
      <configuration>
          <includes>
              <include>**/adminjvm/**</include>
          </includes>
      </configuration>
  </execution>
  <execution>
      <id>site-only-test</id>
      <goals>
          <goal>test</goal>
      </goals>
      <configuration>
          <includes>
              <include>**/sitejvm/**</include>
          </includes>
      </configuration>
  </execution>
 </executions>
 
Author:
Phillip Verheyden (phillipuniverse)
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String