1、pom文件引入依赖

junit

junit

4.12

test

org.test4j

test4j.core

2.0.7

org.springframework

spring-test

4.2.5.RELEASE

2.1、Spring单元测试基础类AbstractTransactionalTests.java

package com.fast.base;

import org.springframework.test.annotation.DirtiesContext;

import org.springframework.test.annotation.Rollback;

import org.springframework.test.context.ActiveProfiles;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import org.test4j.module.ICore;

@DirtiesContext

@ContextConfiguration(locations = {

"classpath*:/spring/applicationContext-test-core.xml",

"classpath*:/spring/applicationContext-test-jdbc.xml"})

//@ContextConfiguration({ "classpath:spring/applicationContext-test-*.xml" })

@ActiveProfiles(profiles = "test")

@Rollback(true)//默认为true

public abstract class AbstractTransactionalTests extends AbstractTransactionalJUnit4SpringContextTests

implements ICore {

}

2.2、单元测试类 DemoServiceTest.java

package com.fast.service;

import static org.junit.Assert.assertNotNull;

import static org.junit.Assert.assertNull;

import static org.junit.Assert.assertTrue;

import java.util.HashMap;

import java.util.Map;

import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;

import com.fast.base.AbstractTransactionalTests;

public class DemoServiceTest extends AbstractTransactionalTests{

@Autowired

DemoService demoService;

@Test

public void testCreate() {

String id= "111";

Map map = new HashMap();

map.put("empId", id);

map.put("leadId", "1");

map.put("empName", "test");

map.put("salary", "100");

map.put("deptNo", "1");

demoService.create(map);

Map result = demoService.get(id);

assertTrue(result != null && result.size() > 0);

}

@Test

public void testDelete() {

String id = "3";

assertNotNull(demoService.get(id));

demoService.delete(id);

assertNull(demoService.get(id));

}

}

3.1、applicationContext-test-jdbc.xml

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/jdbc

http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache-3.1.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

init-method="init" destroy-method="close">

value="20" />

3.2、applicationContext-test-core.xml

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/jdbc

http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache-3.1.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd">

3.3、jdbc_test.properties

driverClassName=oracle.jdbc.driver.OracleDriver

jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl

jdbc_username=TEST

jdbc_password=TEST

4、项目结构

参考: java spring 单元测试_使用 Spring 进行单元测试 https://blog.csdn.net/weixin_34649105/article/details/114207434

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: