06spring-test

spring容器测试方法一
-----------------------------------------------------------
import com.fz.dao.UserDao;
import com.fz.entity.Book;
import com.fz.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by webrx on 2017-07-05.
*/
public class Demo {
protected BeanFactory f;
@Before
public void init(){
this.f = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void tt(){
User u = this.f.getBean("user",User.class);
u.read();
System.out.println("----------------------------");
u.del();
System.out.println("----------------------------");
Book book = this.f.getBean("book",Book.class);
book.show();

}
}


spring容器测试方法二
-----------------------------------------------------------
1、maven项目加入依赖 pom.xml
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.8.RELEASE</version>
<scope>test</scope>
</dependency>

2、编写测试程序
import com.fz.entity.Book;
import com.fz.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.Date;
/**
* Created by webrx on 2017-07-05.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class MyOk {
@Autowired
protected User user;

@Autowired
public Book book;

@Resource(name="dd")
public Date myd;

@Resource
@Qualifier("dd")
public Date ccc;

@Autowired
@Qualifier("yyy")
public Date yyy;
@Test
public void aa(){
user.read();
user.del();
book.show();
System.out.println(myd);
System.out.println(ccc);
System.out.println(myd == ccc);

System.out.println(yyy);
System.out.println(yyy==myd);
}
}

posted @ 2017-07-18 16:34  归然  阅读(108)  评论(0编辑  收藏  举报