Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No XML Spring ApplicationContext

I am trying create an project with and without spring xml configurations.

First at all, I create my xml configuration like that:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<bean id="memoryManagerProduct" class="br.com.caelum.stock.ProductManager">
    <constructor-arg ref="memoryProductDAO"/>
</bean>

<bean id="memoryProductDAO" class="br.com.caelum.stock.dao.MemoryProductDAO"/>

</beans>

That I created my non XML configuration (I do not know if is right base in the fact that I do not know how invoke that)

package br.com.caelum.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import br.com.caelum.stock.ProductManager;
import br.com.caelum.stock.dao.MemoryProductDAO;
import br.com.caelum.stock.dao.Persistable;
import br.com.caelum.stock.model.Product;

@Configuration
public class AppConfig {

@Bean
public Persistable<Product> memoryProductDAO(){
    return new MemoryProductDAO();
}

@Bean
public ProductManager memoryProductManager(){
    return new ProductManager(memoryProductDAO());
}
}

Than I created an JUnit test:

package br.com.caelum.stock;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import br.com.caelum.stock.model.Product;

public class ProductManagerTest {

private ProductManager manager;

@Test
public void testSpringXMLConfiguration() {

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");

    manager = (ProductManager) context.getBean("memoryManagerProduct");

    Product product = new Product();
    product.setDescription("[Book] Spring in Action");
    product.setQuantity(10);

    manager.add(product);

    assertThat(manager.getProducts().get(0).getDescription(), is("[Book] Spring in Action"));
}

@Test
public void testSpringWithoutXMLConfiguration() {

    ApplicationContext context = ?
}
}

How can I inject the configurations that are in my AppConfig to do a similar test as in my testSpringXMLConfiguration?

like image 316
arthurfnsc Avatar asked Sep 26 '14 00:09

arthurfnsc


1 Answers

There are good examples in the Spring reference guide here: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java-instantiating-container

In short, you would do this:

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

manager = (ProductManager) context.getBean("memoryManagerProduct");

However a better way of testing with Spring would be to use the spring-test support, details are here - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#integration-testing-annotations

You can do something like this with spring-test support:

@ContextConfiguration(classes = AppConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductManagerTest {

    @Autowired
    private ProductManager manager;

    @Test
    public void testSpringXMLConfiguration() {
    //use the productmanager in a test..
    }
}
like image 92
Biju Kunjummen Avatar answered Oct 21 '22 21:10

Biju Kunjummen