How to write the Junit test with Spring Autowire?

Asked

Viewed 257 times

2

I am currently setting up a Dynamic Web project to run unit tests, but this project is using dependency injections by Spring Frameworks, it is important to mention that the version of spring that exists in my application is 3.0.4, and there is no chance to update the version in my project because the serial impacts too large.

In the current version of spring, the settings for Junit would be made through annotations, but by the documentation of version 3.0.4 were made with archery configuration XML.

It is important to explain the context before I request help.

To configure the Junit unit tests it would only be enough to place a class and configure it as a Spring component in a specific project folder where the Model, Controller and Services packages are located, and it would look like this;

inserir a descrição da imagem aqui

And in the class where the tests would be run put include the annotations in this way;

inserir a descrição da imagem aqui

These settings would be important for the @Autowired could work in the project, otherwise the settings would not be made that way the @Autowired would not be injected and in the act of performing the test would give Nullpointerexception.

In version 3.0.4 the settings are made differently, only remembering that the version of my project is 3.0.4.

Take a look at my test class;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class IndicioDAOTest {

    @Autowired
    private IndicioDAO indicioDAO;

    @Test
    public void findByIdIndicio() {

        TipoIndicioEntity tipoIndicio = new TipoIndicioEntity();
        tipoIndicio.setCodigo(1);

        NaturezaIndicioEntity naturezaIndicio = new NaturezaIndicioEntity();
        naturezaIndicio.setCodigo(1);

        SituacaoIndicioEntity situacaoIndicio = new SituacaoIndicioEntity();
        situacaoIndicio.setCodigo(1);

        IndicioEntity indicio = new IndicioEntity();
        indicio.setCodigo(1);
        indicio.setTipoIndicio(tipoIndicio);
        indicio.setCodigoNaturezaIndicio(naturezaIndicio.getCodigo());
        indicio.setSituacaoAtual(situacaoIndicio);

        IndicioEntity result = indicioDAO.getPorId(indicio.getCodigo());

        assertThat(result, notNullValue());

    }

In the annotations are trying to map the project’s XML file, in our context is the applicationContext.xml, as shown in the code snippet below;

@Contextconfiguration(Locations = { "/applicationContext.xml" })

However it has to be mapped in the correct way, when trying to run a test it gave this error message below;

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:333)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceControle': Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    ... 24 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:104)
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    ... 37 more

This is happening because the file applicationContext.xml is mapped incorrectly;

I’ve tried these ways;

@Contextconfiguration(Locations = { "/.. /applicationContext.xml" })

I only need to know the correct way to map the file!

See where it is located my file

inserir a descrição da imagem aqui

I tried to use this example;

Spring @Contextconfiguration how to put the right location for xml

And I adapted it that way;

@Contextconfiguration({ "classpath:applicationContext.xml", "file:Resources/Struts.xml", "file:Resources/Tiles.xml"

})

But to no avail;

The problem with my application is that I can pass the context of your server settings!

  • From the error, it seems that the file is being found correctly but has some problem in the XML configuration. Otherwise, you would get an error that file was not found like this: class path resource [applicationContext.xml] cannot be opened because it does not exist

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.