package com.powernode.junit.service; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MathServiceTest { //一个业务类对应一个测试用例 private static final Logger logger = LoggerFactory.getLogger(MathServiceTest.class); //单元测试类:名字规范,测试类名加上Test //单元测试方法写多少个,一般是一个业务方法对应一个测试方法 //测试方法一般是Test加方法名 @Test public void TestSum() { //单元测试的概念,一个是实际值(被测试业务方法的真正执行结果),一个是期望值(业务方法你期待的结果) ApplicationContext context = new ClassPathXmlApplicationContext("spring6.xml"); MathService mathService = context.getBean("mathService", MathService.class); int actual = mathService.sum(1, 2); //获取实际值 logger.info("" + actual); //期望值 // int expected = 30; int expected = 3; //加断言进行测试 Assert.assertEquals(expected,actual); } @Test public void TestSub() { ApplicationContext context = new ClassPathXmlApplicationContext("spring6.xml"); MathService mathService = context.getBean("mathService", MathService.class); int sub = mathService.sub(10, 6); int ex = 4; logger.info("" + sub); Assert.assertEquals(sub,ex); } }
package com.powernode.junit.service;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MathServiceTest
{
//一个业务类对应一个测试用例
private static final Logger logger = LoggerFactory.getLogger(MathServiceTest.class);
//单元测试类:名字规范,测试类名加上Test
//单元测试方法写多少个,一般是一个业务方法对应一个测试方法
//测试方法一般是Test加方法名
@Test
public void TestSum()
{
//单元测试的概念,一个是实际值(被测试业务方法的真正执行结果),一个是期望值(业务方法你期待的结果)
ApplicationContext context = new ClassPathXmlApplicationContext("spring6.xml");
MathService mathService = context.getBean("mathService", MathService.class);
int actual = mathService.sum(1, 2);
//获取实际值
logger.info("" + actual);
//期望值
// int expected = 30;
int expected = 3;
//加断言进行测试
Assert.assertEquals(expected,actual);
}
@Test
public void TestSub()
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring6.xml");
MathService mathService = context.getBean("mathService", MathService.class);
int sub = mathService.sub(10, 6);
int ex = 4;
logger.info("" + sub);
Assert.assertEquals(sub,ex);
}
}
package com.powernode.junit.service; import org.springframework.stereotype.Service; @Service public class MathService { //数学业务类:求和 public int sum(int a,int b) { return a + b; } //减法 public int sub(int a,int b) { return a - b; } }
package com.powernode.junit.service;
import org.springframework.stereotype.Service;
@Service
public class MathService
{
//数学业务类:求和
public int sum(int a,int b)
{
return a + b;
}
//减法
public int sub(int a,int b)
{
return a - b;
}
}