参考:java的单元测试和集成spring单元测试_spring 单元测试-CSDN博客
参考:https://www.cnblogs.com/fysola/p/6361181.html
单元测试: 对于注解类的,@Before,以及@Test,均不可有返回值。
java.lang.Exception: Method getDate() should be void
@Before public String getDate(){ System.out.println("getDatePack"); return ""; }
一、不使用spring
user类
package com.spring.test;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
IUserService接口,实现类UserServiceImpl
package com.spring.test;
public interface IUserService{
public User findUserById(Integer id);
}
package com.spring.test;
public class UserServiceImpl implements IUserService{
public User findUserById(Integer id){
System.out.println("接口方法实现");
if(id==1){
User u=new User();
u.setId(1);
u.setName("zhoi");
return u;
}
return null;
}
}
单元测试类:UserServiceImplTest
package com.spring.test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class UserServiceImplTest {
private IUserService userService=null;
@Before
public void before(){
System.out.println("before");
userService=new UserServiceImpl();
}
@Test
public void testFindUserById() {
System.out.println("test");
User u=userService.findUserById(1);
System.out.println(u.getName());
fail("Not yet implemented");
}
}
采用Spring注解为:
在applicationContext.xml中,扫描service实现包
<context:component-scan base-package="service.impl"></context:component-scan>
具体为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<!-- 自动扫描的包名,如果有多个包,请使用逗号隔开 -->
<context:component-scan base-package="com.spring.test">
</context:component-scan>
</beans>
@Resource 自动注入Ioc容器的对应的bean。
在UserServiceImpl实现类上使用springmvc 注解@Service("userService")
package com.spring.test;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements IUserService{
public User findUserById(Integer id){
System.out.println("接口方法实现");
if(id==1){
User u=new User();
u.setId(1);
u.setName("zhoi");
return u;
}
return null;
}
}
UserServiceImplTest类为
package com.spring.test;
import static org.junit.Assert.*;
import javax.annotation.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class UserServiceImplTest {
@Resource
private IUserService userService;
@Test
public void testFindUserById() {
System.out.println("test");
User u=userService.findUserById(1);
System.out.println(u.getName());
fail("Not yet implemented");
}
}
/* 不使用spring 单元测试
public class UserServiceImplTest {
private IUserService userService=null;
@Before
public void before(){
System.out.println("before");
userService=new UserServiceImpl();
}
@Test
public void testFindUserById() {
System.out.println("test");
User u=userService.findUserById(1);
System.out.println(u.getName());
fail("Not yet implemented");
}
}
*/
二、原始Spring写法
斧子接口:
public interface Axe {
public String chop();
}
package com.spring.api;
public class StoneAxe implements Axe {
public String chop() {
return "石斧砍柴好慢";
}
}
public interface Person {
public void useAxe();
}
package com.spring.api;
public class Chinese implements Person {
private Axe axe;
public void setAxe(Axe axe) {
this.axe = axe;
}
public void useAxe() {
System.out.println("我打算去砍点柴火");
System.out.println(axe.chop());
}
}
非spring
public class BeanTest {
public static void main(String[] args) {
Chinese p = new Chinese();
StoneAxe axe = new StoneAxe();
p.setAxe(axe);
p.useAxe();
}
}
spring方式:
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="chinese" class="com.spring.api.Chinese">
<property name="axe" ref="stoneAxe" />
</bean>
<bean id="stoneAxe" class="com.spring.api.StoneAxe" />
<bean id="win" class="javax.swing.JFrame" />
<bean id="date" class="java.util.Date" />
</beans>
package com.spring.api;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Person p = ctx.getBean("chinese", Person.class);
p.useAxe();
}
}