0

I've recently started developing a simple Spring/Hibernate service but i'm still quite the noob at it, I'm having a persistent issue with this particular test. Everything else seems to be working fine, but I still don't know if it's this piece of code or some misconfig.

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { SPRING_CONFIG_FILE, SPRING_CONFIG_TEST_FILE }) @Transactional public class TestManageService { @Autowired private ManageService manageService; @Test public void testCreateAndFindUser() throws InstanceNotFoundException{ User user = manageService.createUser(new User("testUser", "testPass", "admin")); User foundUser = manageService.findUser(user.getUserId()); assertEquals(user, foundUser); } } 

Line 28 is

User user = manageService.createUser(new User("testUser", "testPass", "admin")); 

Running this in Eclipse results in JUnit complaining bout No Runable Methods, running mvn test results in surefire reporting the following

 <test-method status="FAIL" signature="testCreateAndFindUser()" name="testCreateAndFindUser" duration-ms="6" started-at="2014-07-18T04:34:53Z" finished-at="2014-07-18T04:34:53Z"> <exception class="java.lang.NullPointerException"> <full-stacktrace> <![CDATA[java.lang.NullPointerException at com.viktortech.automaton.test.model.manageservice.TestManageService.testCreateAndFindUser(TestManageService.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74) ..... ..... 

I'm sure this is a trivial problem that I just can't solve because of my short experience with these frameworks. If you need any other file content for solving this issue, just ask away please. Hope you can help and thanks in advance.

Following some Comments' Requests about the ContextConfiguration, this is in the imports of TestManageService

import static com.viktortech.automaton.model.util.GlobalNames.SPRING_CONFIG_FILE; import static com.viktortech.automaton.test.util.GlobalNames.SPRING_CONFIG_TEST_FILE; 

Those lead to two similar files (One for Main and another for Test)

package com.viktortech.automaton.test.util; public final class GlobalNames { public static final String SPRING_CONFIG_TEST_FILE = "classpath:/automaton-spring-config-test.xml"; private GlobalNames () {} } 

I'm adding a Dropbox link to a copy to the the HEAD version of the project's source folder so you can check whatever you need to sort this out. Sorry for not doing this earlier https://www.dropbox.com/sh/wqqghvyoz49ue4y/AABSDrDKtwLIlqT6vUVmehv4a

10
  • Which line is #28? (TestManageService.java:28) Commented Jul 18, 2014 at 2:45
  • Silly me, it's User user = manageService.createUser(new User("testUser", "testPass", "admin")); Commented Jul 18, 2014 at 2:46
  • 1
    I guess manageService is null, which will be because @Autowired did not work. Commented Jul 18, 2014 at 2:47
  • Is ManagedService annotated with @Service? Also what does your context config look like? Assuming line 28 in your unit test is the managerService method call. Commented Jul 18, 2014 at 2:49
  • Any ideas of why may be Autowired malfunctioning here? Commented Jul 18, 2014 at 2:49

1 Answer 1

1

Check if your context configuration files [SPRING_CONFIG_FILE, SPRING_CONFIG_TEST_FILE] contains injection of ManageService bean implementation.

It seems that the Spring can't autowire that bean and that's why you get NullPointerException.

Sign up to request clarification or add additional context in comments.

3 Comments

In viktor-automaton-spring-config.xml (Main Config) I have the following <context:component-scan base-package="com.viktortech.automaton.model"/> while in viktor-automaton-spring-config-test.xml I have <context:component-scan base-package="com.viktortech.automaton.test.model.manageservice"/> Should this be enough or I have to specify the full Class path for each service?
You should place there base-package of your services, so f.e if your service is located in package "com.viktortech.automaton.test.model.manageservice" you can specify parent folder in this case it can be "com.viktortech.automaton.model".
I've been checking some stuff and turns out that this is what is happening but still I can't figure out how to solve it. Please check out the code I've uploaded to Dropbox and see if you can point my silly error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.