建立测试类

package com.jimo.service.impl;

import com.jimo.dto.Exposer;
import com.jimo.dto.KillExecution;
import com.jimo.entity.KillOne;
import com.jimo.exception.KillCloseException;
import com.jimo.exception.RepeatKillException;
import com.jimo.service.KillOneService;
import org.junit.Test;
import org.junit.runner.RunWith;
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 java.util.List;

import static org.junit.Assert.*;

/**
 * Created by root on 17-5-26.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
        "classpath:spring/spring-dao.xml",
        "classpath:spring/spring-service.xml"
})
public class KillOneServiceImplTest {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private KillOneService killOneService;

    @Test
    public void getKillList() throws Exception {
        List<KillOne> killList = killOneService.getKillList();
        logger.info("killList:{}", killList);
    }

    @Test
    public void getKillById() throws Exception {
        KillOne killOne = killOneService.getKillById(1001);
        logger.info("killOne:{}", killOne);
    }

    @Test
    public void testKillLogic() throws Exception {
        long id = 1000;
        Exposer exposer = killOneService.exportKillUrl(id);
        if (exposer.isExposed()) {
            logger.info("exposer:{}", exposer);
            long phone = 11011111111L;
            try {
                KillExecution killExecution = killOneService.executeKill(id, phone, exposer.getMd5());
                logger.info("result:{}", killExecution);
            } catch (RepeatKillException e) {
                logger.error(e.getMessage());
            } catch (KillCloseException e) {
                logger.error(e.getMessage());
            }
        } else {
            //秒杀未开启
            logger.warn("exposer:{}", exposer);
        }
    }

}

logback配置

在resource目录下建立logback.xml,配置参考:https://logback.qos.ch/manual/configuration.html

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

测试结果

14:16:08.393 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@3561c410] will not be managed by Spring
14:16:08.397 [main] DEBUG com.jimo.dao.KillOneDao.queryById - ==>  Preparing: select * from killone where kill_id=? 
14:16:08.434 [main] DEBUG com.jimo.dao.KillOneDao.queryById - ==> Parameters: 1000(Long)
14:16:08.456 [main] DEBUG com.jimo.dao.KillOneDao.queryById - <==      Total: 1
14:16:08.461 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6cb6decd]
14:16:08.462 [main] WARN  c.j.s.impl.KillOneServiceImplTest - exposer:Exposer{isExposed=false, md5='null', killId=1000, now=1495779368462, start=1495355489000, end=1495382400000}

results matching ""

    No results matching ""