Spring使用测试用例测试业务方法-----Spring框架

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;
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值