叶雪松
2017-04-09 01:33
package org.junit.runner.manipulation;
import org.junit.runner.Description;
import org.junit.runner.Request;
/**
* The canonical case of filtering is when you want to run a single test method in a class. Rather
* than introduce runner API just for that one case, JUnit provides a general filtering mechanism.
* If you want to filter the tests to be run, extend <code>Filter</code> and apply an instance of
* your filter to the {@link org.junit.runner.Request} before running it (see
* {@link org.junit.runner.JUnitCore#run(Request)}. Alternatively, apply a <code>Filter</code> to
* a {@link org.junit.runner.Runner} before running tests (for example, in conjunction with
* {@link org.junit.runner.RunWith}.
*
* @since 4.0
*/
public abstract class Filter {
/**
* A null <code>Filter</code> that passes all tests through.
*/
public static Filter ALL = new Filter() {
@Override
public boolean shouldRun(Description description) {
return true;
}
@Override
public String describe() {
return "all tests";
}
@Override
public void apply(Object child) throws NoTestsRemainException {
// do nothing
}
@Override
public Filter intersect(Filter second) {
return second;
}
};
/**
* Returns a {@code Filter} that only runs the single method described by
* {@code desiredDescription}
*/
public static Filter matchMethodDescription(final Description desiredDescription) {
return new Filter() {
@Override
public boolean shouldRun(Description description) {
if (description.isTest()) {
return desiredDescription.equals(description);
}
// explicitly check if any children want to run
for (Description each : description.getChildren()) {
if (shouldRun(each)) {
return true;
}
}
return false;
}
@Override
public String describe() {
return String.format("Method %s", desiredDescription.getDisplayName());
}
};
}
/**
* @param description the description of the test to be run
* @return <code>true</code> if the test should be run
*/
public abstract boolean shouldRun(Description description);
/**
* Returns a textual description of this Filter
*
* @return a textual description of this Filter
*/
public abstract String describe();
/**
* Invoke with a {@link org.junit.runner.Runner} to cause all tests it intends to run
* to first be checked with the filter. Only those that pass the filter will be run.
*
* @param child the runner to be filtered by the receiver
* @throws NoTestsRemainException if the receiver removes all tests
*/
public void apply(Object child) throws NoTestsRemainException {
if (!(child instanceof Filterable)) {
return;
}
Filterable filterable = (Filterable) child;
filterable.filter(this);
}
/**
* Returns a new Filter that accepts the intersection of the tests accepted
* by this Filter and {@code second}
*/
public Filter intersect(final Filter second) {
if (second == this || second == ALL) {
return this;
}
final Filter first = this;
return new Filter() {
@Override
public boolean shouldRun(Description description) {
return first.shouldRun(description)
&& second.shouldRun(description);
}
@Override
public String describe() {
return first.describe() + " and " + second.describe();
}
};
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xy9860</groupId> <artifactId>seckill</artifactId> <version>0.0.1-SNAPSHOT</version> <name>seckill Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 日志相关的依赖 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.1</version> </dependency> <!-- 实现slf4j 这样我们只需要使用 slf4j的接口就可以了 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> </dependency> <!-- 数据库相关的依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- DAO框架依赖 mabits --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- SERVlet web相关的依赖 --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8</version> </dependency> <!-- spring 依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.7.RELEASE</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>seckill</finalName> </build> <packaging>war</packaging> </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>seckill</groupId>
<artifactId>seckill</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>seckill Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mybatis自身实现的整合依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<!-- servlet web 相关依赖 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- spring 依赖 -->
<!-- 因为有些包已经把spring做为默认依赖,这里面可以省略的依赖有:
spring-core;spring-beans(上面这两个spring-context会自动依赖);
spring-context,spring-jdbc(mybatis-spring会依赖); spring-web(spring-webmvc会依赖);
logback-core(logback-classic会依赖). -->
<!-- spring 核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- spring dao 层依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- spring web 相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- spring test 相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>seckill</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source><!-- 这个1.8是jdk的版本,下面那个也是-->
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.xy9860.seckill.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.xy9860.seckill.dto.Exposer;
import com.xy9860.seckill.dto.SeckillExecution;
import com.xy9860.seckill.entity.Seckill;
import com.xy9860.seckill.service.SeckillService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SeckillServiceImplTest {
private Logger logger=LoggerFactory.getLogger(this.getClass());
@Resource
private SeckillService seckillService;
@Test
public void testGetSeckillList() {
List<Seckill> seckills =seckillService.getSeckillList();
logger.info("list={}",seckills);
}
@Test
public void testGetSeckillById() {
Seckill seckill=seckillService.getSeckillById(1000);
logger.info("seckill={}",seckill);
}
@Test
public void testExportSeckillUrl() {
long id=1000;
Exposer exposer=seckillService.exportSeckillUrl(id);
logger.info("exposer={}",exposer);
}
@Test
public void testExecuteSeckill() {
String md5="652785cd5da374c9a9f48b5bf57285bb";
long id=1000;
long phone=13333333233L;
SeckillExecution seckillExecution=seckillService.executeSeckill(id, phone, md5);
logger.info("seckillExecution={}",seckillExecution);
}
}
package com.xy9860.seckill.service.impl;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import com.xy9860.seckill.dao.SeckillDao;
import com.xy9860.seckill.dao.SuccessKilledDao;
import com.xy9860.seckill.dto.Exposer;
import com.xy9860.seckill.dto.SeckillExecution;
import com.xy9860.seckill.entity.Seckill;
import com.xy9860.seckill.entity.SuccessKilled;
import com.xy9860.seckill.enums.SeckillStatEnum;
import com.xy9860.seckill.exception.RepeatKillException;
import com.xy9860.seckill.exception.SeckillCloseException;
import com.xy9860.seckill.exception.SeckillException;
import com.xy9860.seckill.service.SeckillService;
@Service("seckillService")
public class SeckillServiceImpl implements SeckillService {
private Logger logger=LoggerFactory.getLogger(this.getClass());
@Autowired
private SeckillDao seckillDao;
@Autowired
private SuccessKilledDao successKilledDao;
//用于混淆方法
private final String slat="asdasfJASLFKDAJ@#(*&%(@#%><?ˉ&asdjalfg";
public List<Seckill> getSeckillList() {
// TODO Auto-generated method stub
return seckillDao.queryAll(0, 100);
}
public Seckill getSeckillById(long seckillId) {
// TODO Auto-generated method stub
return seckillDao.queryByid(seckillId);
}
public Exposer exportSeckillUrl(long seckillId) {
Seckill seckill=seckillDao.queryByid(seckillId);
if (seckill==null) {
return new Exposer(false,seckillId);
}
Date startTime=seckill.getStrartTime();
Date endTime=seckill.getEndTime();
Date nowTime=new Date();
if (nowTime.getTime()>endTime.getTime()||nowTime.getTime()<startTime.getTime()) {
return new Exposer(false,seckillId,nowTime.getTime(),startTime.getTime(),endTime.getTime());
}
String md5=getMD5(seckillId);
return new Exposer(true,md5,seckillId);
}
@Transactional
public SeckillExecution executeSeckill(long seckillId, long userPhone, String md5)
throws SeckillException, SeckillCloseException, RepeatKillException {
if (md5==null||!md5.equals(getMD5(seckillId))) {
throw new SeckillException("seckill date rewrite");
}
//执行秒杀逻辑
int updateCount=seckillDao.reduceNumber(seckillId, new Date());
try {
if (updateCount<=0) {
//没有更新操作
throw new SeckillCloseException("seckill is closed");
}else{
//记录购买行为
int insertCount =successKilledDao.insertSuccessKilled(seckillId, userPhone);
if (insertCount<=0) {
//重复秒杀
throw new RepeatKillException("seckillId repeated");
}else {
SuccessKilled successKilled=successKilledDao.queryByid(seckillId, userPhone);
return new SeckillExecution(seckillId, SeckillStatEnum.SUCCESS,successKilled);
}
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
throw new SeckillException("seckill iner error"+e.getMessage());
}
}
private String getMD5(long seckillId){
String base=seckillId+"/"+slat;
String md5=DigestUtils.md5DigestAsHex(base.getBytes());
return md5;
}
}
你把你的pom.xml 关于 junit 和 spring 的版本 贴上来看看. 看起来好像是 junit版本问题
package org.seckill.service;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.seckill.dto.Exposer;
import org.seckill.dto.SeckillExecution;
import org.seckill.entity.Seckill;
import org.seckill.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
@Transactional
//@transactional加到测试类中,可让每一次测试之后rollback,即不需要之后把数据库数据改回来
public class SeckillServiceTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private SeckillService seckillService;
@Test
public void testGetSeckillList(){
List<Seckill> list = seckillService.getSeckillList();
logger.info("list={}", list);
}
@Test
public void testGetById() {
long id = 1001;
Seckill seckill = seckillService.getById(id);
logger.info("seckill={}", seckill);
}
@Test
public void testExportSeckillUrl() {
long id = 1000L;
Exposer exposer = seckillService.exportSeckillUrl(id );
logger.info("exposer={}", exposer);
}
@Test
public void testExecutionSeckill() {
long id = 1000L;
long userId = 2013001L;
String md5 = "whoamI?IamtheCodeKingoftheworld.Butnotjustcancoding,alsoBigBird";
SeckillExecution seckillExecution = seckillService.executionSeckill(id, userId, md5);
logger.info("successKilled={}", seckillExecution);
}
}
没看懂 你想测试什么,难道是我功力不够?没有@Test的入口啊?
Java高并发秒杀API之Service层
59971 学习 · 143 问题
相似问题
回答 1
回答 1