initialization Error java.lang.Exception: No runnable methods 怎么解決 Junit失敗、 控制臺沒信息?
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();
??????????? }
??????? };
??? }
}
2017-04-10
2017-04-10
<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>
2017-04-10
2017-04-10
2017-04-10
你把你的pom.xml 關于 junit 和 spring 的版本 貼上來看看. ?看起來好像是 junit版本問題
2017-04-10
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);
?? ?}
}
2017-04-10
沒看懂 你想測試什么,難道是我功力不夠?沒有@Test的入口啊?