junitでAutowired注記を使用する
4111 ワード
POMファイル:junitバージョン要求が点以上4.12
二テスト:
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.11.RELEASEversion>
<scope>testscope>
dependency>
二テスト:
package com.szc.dao;
import com.szc.pojo.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.sql.DataSource;
/**
* Unit test for simple App.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class BeanTest {
private ApplicationContext applicationContext=null;
@Autowired
private EmployeeRepository employeeRepository;
@Test
public void testBean(){
applicationContext=new ClassPathXmlApplicationContext("beans.xml");
DataSource dataSource=applicationContext.getBean(DataSource.class);
}
@Test
public void testEmployeeRepository(){
Employee employee=employeeRepository.findByName("zhangsan");
System.out.println(employee);
}
}