Reputation: 13
I´m tri to run a JUnit Test um my spring boot project i bilded like this:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.br.suppcomm.ocp.entity.Login;
public interface LoginDao extends JpaRepository<Login, Long>{
...
}
Service:
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.br.suppcomm.ocp.dao.CompanyDAO;
import com.br.suppcomm.ocp.dao.LoginDao;
import com.br.suppcomm.ocp.entity.Login;
@Service
public class LoginService {
@Autowired LoginDao loginDao;
@Autowired CompanyDAO companyDao;
public void save(Login login) {
loginDao.save(login);
}
public void delete(Login login) {
loginDao.delete(login);
}
public Login findById(Login login) {
return loginDao.findOne(login.getLoginId());
}
public Login findByEmail(Login login) {
return loginDao.findByEmail(login.getEmail());
}
public Login FindByLogin(Login login) {
return loginDao.FindByLogin(login);
}
public List<Login> getAll() {
return loginDao.findAll();
}
public Login getUserAccessLoginPass(String login, String password) {
return loginDao.getUserAccessLoginPass(login, password);
}
public void update(Login login) {
loginDao.save(login);
}
}
Test:
package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.br.suppcomm.ocp.service.LoginService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class OcpJpaApplicationTests {
@Autowired LoginService loginService;
@Test
public void contextLoads() {
}
}
When I ran this code did show me the folow error.
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'loginService': Unsatisfied dependency expressed through field 'loginDao': No qualifying bean of type
[com.br.suppcomm.ocp.dao.LoginDao] found for dependency [com.br.suppcomm.ocp.dao.LoginDao]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.br.suppcomm.ocp.dao.LoginDao] found for dependency [com.br.suppcomm.ocp.dao.LoginDao]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
I dont know what happened!! Please.
Upvotes: 0
Views: 6640
Reputation: 1070
Add @Repository your Interface
annotation , so that it can be Autowired.
@Repository
public interface LoginDao extends JpaRepository<Login, Long>{
}
It'll work that way! Exception says that SPring is not able to find a qualifier, to Autowired something you need to sterotype it.
Upvotes: 2
Reputation: 9635
You need to add this annotation to your test:
@DataJpaTest
This will cause the Persistence slice of you application to be initialized.
Upvotes: 0
Reputation: 12347
Please add the classes
attribute to your @SpringBootTest
annotation as follows:
@SpringBootTest(classes = { Application.class })
So that it will know that it has to do component scan, etc that you've specified on your Application
class.
Upvotes: 1