How to write JUnit test with Spring Autowire?

Premraj

Here are the files that I use:

component.xml

<?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"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd 
         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <context:component-scan
        base-package="controllers,services,dao,org.springframework.jndi" />
</beans>

ServiceImpl.java

@org.springframework.stereotype.Service
public class ServiceImpl implements MyService {

    @Autowired
    private MyDAO myDAO;

    public void getData() {...}    
}

ServiceImplTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:conf/components.xml")
public class ServiceImplTest{

    @Test
    public void testMyFunction() {...}
}

Error:

16:22:48.753 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2092dcdb] to prepare test instance [services.ServiceImplTest@9e1be92]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'services.ServiceImplTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private services.ServiceImpl services.ServiceImplTest.publishedServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [services.ServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) ~[spring-beans.jar:3.1.2.RELEASE]
Sembrano

Make sure you have imported the correct package. If I remeber correctly there are two different packages for Autowiring. Should be :org.springframework.beans.factory.annotation.Autowired;

Also this looks wierd to me :

@ContextConfiguration("classpath*:conf/components.xml")

Here is an example that works fine for me :

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

    @Autowired
    OwnerService ownerService;

    @Before
    public void setup() {

        ownerService.cleanList();

    }

    @Test
    public void testOwners() {

        Owner owner = new Owner("Bengt", "Karlsson", "Ankavägen 3");
        owner = ownerService.createOwner(owner);
        assertEquals("Check firstName : ", "Bengt", owner.getFirstName());
        assertTrue("Check that Id exist: ", owner.getId() > 0);

        owner.setLastName("Larsson");
        ownerService.updateOwner(owner);
        owner = ownerService.getOwner(owner.getId());
        assertEquals("Name is changed", "Larsson", owner.getLastName());

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to only autowire a specific class in JUnit spring test?

From Dev

Can't Autowire JpaRepository in Junit test - Spring boot application

From Java

How to write junit test for DirectoryStream

From Dev

How to write Junit test for this "FileNotFoundException"

From Dev

How to AutoWire spring beans when using Mockito and Junit?

From Dev

How to test ConfigurationProperties in Spring with JUnit?

From Dev

How to write xPath for Junit test case

From Dev

how to write junit test case for inner methods?

From Dev

How to write properly a groovy unit test with jUnit?

From Dev

How to write Junit test cases for CDI?

From Dev

How to write Junit test cases for CDI?

From Dev

How do I write a Junit test for a ThreadPoolExecutor

From Dev

How to write Junit test cases for a Thread class

From Dev

How to write a junit test for outputStream.write IO IOException

From Dev

JUnit Spring Boot Application - Could not autowire fields

From Dev

How to run Spring Shell scripts in a JUnit test

From Java

How to make Junit test in spring boot

From Java

How to test read-write lock using JUnit?

From Dev

How can I write a JUnit-Test for custom Jackson Serializer?

From Dev

How to write JUNIT test case for a class which implements ApplicationListener<ApplicationPreparedEvent>

From Dev

How Should I write JUnit test in the following case?

From Dev

How to write a Junit test for a morse code binary tree

From Dev

How to autowire Service with-in JUnit Tests

From Dev

Spring junit test not working

From Dev

Spring Data can't autowire repository in the test

From Dev

Write JUnit test for local @ExceptionHandler

From Dev

Write JUnit test for local @ExceptionHandler

From Dev

How to autowire by name in Spring with annotations?

From Java

How to Autowire conditionally in spring boot?

Related Related

  1. 1

    How to only autowire a specific class in JUnit spring test?

  2. 2

    Can't Autowire JpaRepository in Junit test - Spring boot application

  3. 3

    How to write junit test for DirectoryStream

  4. 4

    How to write Junit test for this "FileNotFoundException"

  5. 5

    How to AutoWire spring beans when using Mockito and Junit?

  6. 6

    How to test ConfigurationProperties in Spring with JUnit?

  7. 7

    How to write xPath for Junit test case

  8. 8

    how to write junit test case for inner methods?

  9. 9

    How to write properly a groovy unit test with jUnit?

  10. 10

    How to write Junit test cases for CDI?

  11. 11

    How to write Junit test cases for CDI?

  12. 12

    How do I write a Junit test for a ThreadPoolExecutor

  13. 13

    How to write Junit test cases for a Thread class

  14. 14

    How to write a junit test for outputStream.write IO IOException

  15. 15

    JUnit Spring Boot Application - Could not autowire fields

  16. 16

    How to run Spring Shell scripts in a JUnit test

  17. 17

    How to make Junit test in spring boot

  18. 18

    How to test read-write lock using JUnit?

  19. 19

    How can I write a JUnit-Test for custom Jackson Serializer?

  20. 20

    How to write JUNIT test case for a class which implements ApplicationListener<ApplicationPreparedEvent>

  21. 21

    How Should I write JUnit test in the following case?

  22. 22

    How to write a Junit test for a morse code binary tree

  23. 23

    How to autowire Service with-in JUnit Tests

  24. 24

    Spring junit test not working

  25. 25

    Spring Data can't autowire repository in the test

  26. 26

    Write JUnit test for local @ExceptionHandler

  27. 27

    Write JUnit test for local @ExceptionHandler

  28. 28

    How to autowire by name in Spring with annotations?

  29. 29

    How to Autowire conditionally in spring boot?

HotTag

Archive