2017-05-18 11 views
0

私はSpringブート1.5.3.RELEASEを使用して新しいMVCアプリケーションを構築しています。@SpringBootTestで依存性注入が失敗する

次のように私はCustomerエンティティを持っている:

@Entity 
public class Customer { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id") 
    private int id; 

    @Column(name = "name", length = 100, nullable = false) 
    private String name; 

    @Column(name = "enabled", nullable = false) 
    private boolean enabled; 
} 

次のように私はCustomerRepositoryを持っている:

public interface CustomerRepository extends CrudRepository<Customer, Integer> { 

} 

を次のように私はたCustomerServiceを持っている:

@Service 
public class CustomerService { 

    @Autowired 
    CustomerRepository customerRepository; 

    @Autowired 
    CustomerMapper customerMapper; 

    public CustomerDto save(CustomerDto customerDto) { 

     Customer customer = customerMapper.map(customerDto, Customer.class); 

     customerRepository.save(customer); 

     return customerMapper.map(customer, CustomerDto.class); 
    } 

    public CustomerDto findById(int id) { 

     Customer customer = customerRepository.findOne(id); 

     return customerMapper.map(customer, CustomerDto.class); 
    } 
} 

私のアプリケーションがあります

次のように私は、テストを書かれている

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories 
public class PersistenceConfig { 

} 

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

    @Autowired 
    CustomerService customerService; 

    @Test 
    public void testCustomerService() { 

     CustomerDto customerDtoIn = new CustomerDto(); 

     customerDtoIn.setName("Test Customer"); 
     customerDtoIn.setEnabled(true); 

     customerService.save(customerDtoIn); 

     CustomerDto customerDtoOut = customerService.findById(customerDtoIn.getId()); 

     assertThat(customerDtoOut).isEqualTo(customerDtoIn); 
    } 
} 

私が続いている構造がある:210

@SpringBootApplication 
public class CoreApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(CoreApplication.class, args); 
    } 
} 

私のJPAの設定があるしかし

com.app.core <- CoreApplication lives here 
com.app.core.repositories <- Repositories here 
com.app.core.services <- Services here 

、Iこのテストケースを実行しようとすると、次のようになる:

org.springframework.beans.factory.NoSuchBeanDefinitionException 
No qualifying bean of type 'com.app.core.repositories.CustomerRepository' available 

なぜ、依存性注入が失敗しているのか誰かが教えてもらえますか?春のブートドキュメント@SpringBootTest@RunWith(SpringRunner.class)を読んでから、自分のサービスレイヤーをテストするだけです。

+0

どのようにあなたのメインクラスがどのように見えるのでしょうか?あなたが従ったディレクトリ構造は何ですか?あなたのアプリケーションを実行することができます – pvpkiran

+0

私はいくつかの詳細で質問を更新しました。私はまだコントローラー層を作成していないので、実行するアプリケーションはありません。テストをスキップするとアプリケーションをうまく構築できます。 –

+0

アプリケーションを実行するためのコントローラは必要ありません。あなたのアプリケーションを起動してみて、例外なくそれが星かどうかを確認してください。 – pvpkiran

答えて

0

ApplicationContext.xmlに基本パッケージとリポジトリベースパッケージを追加する必要があります。ヨーヨーは、コードの下に使用することができます: -

<context:component-scan annotation-config="true" 
    base-package="com.demo.test" /> 

<jpa:repositories base-package="com.demo.test.repository" /> 
+0

私はもう少し詳しく質問を更新しました。私はxmlベースの設定を使用していないし、 '@ SpringBootApplication'アノテーションはすべて私が必要と考えるものです。 –