Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy autowiring bean is null?
    text
    copied!<p>I write test for my application:</p> <pre><code>public class CandidateServiceTest { @Autowired CandidateService candidateService; @BeforeClass public static void initialize() throws Exception{ ApplicationContext context = new ClassPathXmlApplicationContext( "test/BeanConfig.xml");//I think here I load context UtilMethods.createTestDb(); } @Test public void add(){ Candidate candidate = new Candidate(); candidate.setName("testUser"); candidateService.add(candidate);//NullPointerException here List&lt;Candidate&gt; candidates = candidateService.findByName(candidate.getName()); Assert.assertNotNull(candidates); } } </code></pre> <p>in row </p> <pre><code>candidateService.add(candidate);//Null entity here here </code></pre> <p>candidateService is null.</p> <p>test/BeanConfig.xml:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt; &lt;!-- Включаем опцию использования конфигурационных аннотаций (@Annotation-based configuration)--&gt; &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.epam.hhsystem.jpa" /&gt; &lt;context:component-scan base-package="package com.epam.hhsystem.services" /&gt; &lt;!-- Файл с настройками ресурсов для работы с данными (Data Access Resources) --&gt; &lt;import resource="data.xml" /&gt; &lt;/beans&gt; </code></pre> <p>data.xml - info about hibernate,</p> <p>class of CandidateService: </p> <pre><code>package com.epam.hhsystem.services; ... @Transactional @Service("candidateService") public class CandidateService { @Autowired private CandidateDao candidateDao;//package com.epam.hhsystem.jpa @Autowired private VacancyDao vacancyDao;//package com.epam.hhsystem.jpa @Autowired private SkillDao skillDao;//package com.epam.hhsystem.jpa @Autowired private EventDao eventDao;//package com.epam.hhsystem.jpa @Autowired UtilService utilService;//package com.epam.hhsystem.services public void add(Candidate candidate) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String login = auth.getName(); User user = utilService.getOrSaveUser(login); candidate.setAuthor(user); candidateDao.add(candidate); } .... } </code></pre> <p>Can you help me?</p> <p><strong>UPDATE</strong></p> <p>I rewrite code so:</p> <pre><code>@ContextConfiguration(locations = {"classpath:/test/BeanConfig.xml"}) public class CandidateServiceTest extends AbstractTransactionalJUnit4SpringContextTests{ @Autowired CandidateService candidateService; @BeforeClass public static void initialize() throws Exception{ UtilMethods.createTestDb(); } @Test public void add(){ Candidate candidate = new Candidate(); candidate.setName("testUser"); candidateService.add(candidate); List&lt;Candidate&gt; candidates = candidateService.findByName(candidate.getName()); Assert.assertNotNull(candidates); } } </code></pre> <p>And I have so trace:</p> <pre><code>java.lang.NullPointerException at com.epam.hhsystem.services.CandidateService.add(CandidateService.java:56) at com.epam.hhsystem.services.CandidateService$$FastClassByCGLIB$$3796612d.invoke(&lt;generated&gt;) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) at com.epam.hhsystem.services.CandidateService$$EnhancerByCGLIB$$afe33393.add(&lt;generated&gt;) at com.epam.hhhsystem.services.CandidateServiceTest.add(CandidateServiceTest.java:40) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload