JUNIT参数化测试诡异问题:No tests found matching Method 测试方法(测试类) from org.junit.internal.requests.ClassR..

在进行JUNIT参数化测试时遇到了错误:No tests found matching 方法名(类名)。参考了iteye博客和StackOverflow上的解答,解决方案是重写org.junit.runner.Parameterized,并使用PowerMockRunner Delegate搭配自定义的B2bParameterized类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文参考与

1: https://www.iteye.com/blog/hedajia-2208403

2: https://stackoverflow.com/questions/12798079/initializationerror-with-eclipse-and-junit4-when-executing-a-single-test/18438718#18438718

JUNIT参数化测试,遇到的诡异问题

No tests found matching Method 测试方法名(测试类名) from org.junit.internal.request.ClassRequest@ibdecc21

解决方案:

重写 org.junit.runner.Parameterized

代码如下

public class B2bParameterized extends Parameterized {  
  
    public B2bParameterized (Class<?> klass) throws Throwable {  
        super(klass);  
    }  
  
    public void filter(Filter filter) throws NoTestsRemainException {  
        super.filter(new FilterDecorator(filter));  
    }  
  
   /** 
     * Running single test in case of parameterized test causes issue as explained in 
     * http://youtrack.jetbrains.com/issue/IDEA-65966 
     * 
     * As a workaround we wrap the original filter and then pass it a wrapped description 
     * which removes the parameter part (See deparametrizedName) 
     */  
    private static class FilterDecorator extends Filter {  
        private final Filter delegate;  
  
        private FilterDecorator(Filter delegate) {  
            this.delegate = delegate;  
        }  
  
        @Override  
        public boolean shouldRun(Description description) {  
            return delegate.shouldRun(wrap(description));  
        }  
  
        @Override  
        public String describe() {  
            return delegate.describe();  
        }  
    }  
  
    private static Description wrap(Description description) {  
        String name = description.getDisplayName();  
        String fixedName = deparametrizedName(name);  
        Description clonedDescription =  
                Description.createSuiteDescription(fixedName,description.getAnnotations().toArray(new Annotation[0]));  
        for(Description child : description.getChildren()){  
            clonedDescription.addChild(wrap(child));  
        }  
        return clonedDescription;  
    }  
  
    private static String deparametrizedName(String name) {  
        //Each parameter is named as [0], [1] etc  
        if(name.startsWith("[")){  
            return name;  
        }  
  
        //Convert methodName[index](className) to  
        //methodName(className)  
        int indexOfOpenBracket = name.indexOf('[');  
        int indexOfCloseBracket = name.indexOf(']')+1;  
        return name.substring(0,indexOfOpenBracket).concat(name.substring(indexOfCloseBracket));  
    }  
}  

测试类:

@RunWith(PowerMockRunner.class)

@PowerMockRunnerDelegate(B2bParameterized.class)

参数方法

@B2bParameterized.Parameters

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值