Unable to inject dependency in Junit test

允我心安 提交于 2019-12-18 09:49:14

问题


Having some trouble injecting a dependency in one of my JUnit test classes. I believe the TestApplication is not package scanning or is not being loaded.

Code below:

package com.mitto.repositories;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.mitto.MittoApplicationTests;
import com.mitto.domain.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( classes= { MittoApplicationTests.class } )
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
    TransactionalTestExecutionListener.class,
    DbUnitTestExecutionListener.class})
@DatabaseSetup("UserRepositoryTest.xml")
public class UserRepositoryTest {

    @Autowired
    UserRepository repository;

    private static final long FACEBOOK_ID = 1234567;

    @Test
    public void getUserById() {
        User user = repository.findOne(1L);
        assertNotNull(user);
        assertEquals( user.getFacebookId(), FACEBOOK_ID  );
    }
}

MittoApplicationTests.java

package com.mitto;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MittoApplicationTests {

    @Test
    public void contextLoads() {
    }    
}

UserRepository.java

package com.mitto.repositories;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import com.mitto.domain.User;

@Repository
public interface UserRepository extends PagingAndSortingRepository<User, Long>{

    User findByFacebookId( long facebookId );
    User findByAuthToken( String token );

}

I can't see anything wrong with this.


回答1:


Sometimes, a working example is better than fixes.
Here is a working example:

First, in your configuration class

@SpringBootApplication
@ComponentScan(value = "com.mitto")
@EnableJpaRepositories(value = "com.mitto")
@EntityScan(basePackages = {"com.mitto.domain"}, basePackageClasses = {Jsr310JpaConverters.class})
public class MittoApplicationTests {
}

Second, in your test class

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MittoApplicationTests.class) // replace the @ContextConfiguration with @SpringBootTest
// rest of of your annotations ... 
public class UserRepositoryTest {
    @Autowired
    UserRepository repository;

    // your test cases
}  

A Spring Boot application is just a Spring ApplicationContext, so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context. One thing to watch out for though is that the external properties, logging and other features of Spring Boot are only installed in the context by default if you use SpringApplication to create it.

Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication.

Please read the documentation for more details:

SpringBootTest annotation

boot-features-testing



来源:https://stackoverflow.com/questions/39823045/unable-to-inject-dependency-in-junit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!