Configuration to use @Mock and @InjectMocks

I want to use a mock DAO to unit test the service layer in my spring application. In the DAO, it's seesinoFactory is injected using @Inject.

When the test class is configured with @RunWith(MockitoJUnitRunner.class)

@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
    @Mock
    private MyDao myDaoMock;

    @InjectMocks
    private MyServiceImpl myService;
}

The output is just as expected.

When I changed the configuration to using @RunWith(SpringJUnit4ClassRunner.class)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/ServiceTest-context.xml")
public class ServiceTest {
    @Mock
    private MyDao myDaoMock;

    @InjectMocks
    private MyServiceImpl myService;

    @Before
    public void setup(){
        MockitoAnnotations.initMocks(this);
    }
}

The exception,"No qualifying bean of type [org.hibernate.SessionFactory] found for dependency", will be thrown if sessionFactory bean is not available in ServiceTest-context.xml.

What I don't understand is MyDao is annotated with @Mock already, why is sessionFactory still needed?

链接地址: http://www.djcxy.com/p/82048.html

上一篇: 如何有效地许多春天背景

下一篇: 配置使用@Mock和@InjectMocks