您的位置:首页 > 编程语言 > Java开发

spring Junit测试项目例子

2016-07-27 12:09 561 查看
公司的项目里面用到了大量的单元测试以及Spring框架中的内容,花了一上午从gitHub里面找了一个maven的测试例子。所有的代码说明,以及junit的使用说明都放在了代码中。使用的开发工具为Intellij idea,可以在eclipse下导入,具体请自行百度。

话不多说,首先给出项目的下载链接(需要提前安装maven)
http://pan.baidu.com/s/1o8pigbw
示例项目目录截图



简单的一个placeholder类:

package com.nickgroenke;

/**
* Placeholder class generated by archetype.
*
* @author nickgroenke
*/
public class Placeholder {
private String greetName;
public String getGreetName() {
return greetName;
}

public void setGreetName(String greetName) {
this.greetName = greetName;
}

public String helloWorld() {
return "Hello, " + greetName;
}
public int mutiply(int a,int b)
{
return a*b;
}
}注入文件application-config.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<context:component-scan base-package="com.nickgroenke" />

<bean id="placeholder" class="com.nickgroenke.Placeholder">
<property name="greetName" value="leixingbang"/>
</bean>
<!--在xml中已经配置了对应的application.properties的位置,因此在类文件中无需再加载-->
<context:property-placeholder location="application.properties"/>

</beans>测试类:
package com.nickgroenke;

import org.junit.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
//配置加载的应用文件
@ContextConfiguration(locations = { "/application-config.xml" })
public class PlaceholderTest {

/* 属性值从aplication.properties中注入*/
@Value("${greetName}")
private String greetName;

/* 属性值从application-config.xml的定义中注入*/
@Autowired
private Placeholder placeholder;
//对所有的测试只执行一次,并且先于applicationContext的加载
@BeforeClass
public static void testBeforeClass()
{

System.out.println("BeforeClass执行.....");
}
@Before
public void beforeAllTest()//对所有的测试方法都执行一次
{
System.out.println("@before 执行......");
}
@Test
public void testHelloWorld() {
System.out.println("hello,world测试开始执行.....");
placeholder.setGreetName("World!");
assertEquals("Hello, " + greetName, placeholder.helloWorld());
System.out.println("hello,world测试执行完毕.....");
}
@Test
public void testMultiply()
{
System.out.println("乘法测试执行....");
assertEquals(56,placeholder.mutiply(7,8));
System.out.println("乘法测试执行完毕....");
}
@After
public void afterTest()
{
System.out.println("@after执行.....");
}

@AfterClass
public static void testAfterClass()
{
System.out.println("AfterClass执行.....");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: