@RunWith(Suite.class)
@Suite.SuiteClasses({AssertTests.class, CalculatorTest.class})
public class SuiteTest {
// the class remains empty, used only as a holder for the above annotations
}
public class AssumptionsTest {
@Test
public void testAssumTrue() {
System.out.println("test");
assumeTrue(3>5);
//该方法中下面所有的代码在上面假设的条件成立后执行
//如果上述假设不成立,则会忽略执行该行下面的代码,并报错
System.out.println("assume is true!");
}
@Test
public void testAssumFalse(){
assumeFalse(3>5);
System.out.println("assume is true!");
}
}
以下语法JUnit5支持:
@Test
public void testAssumTrueMessage() {
assumeTrue(3<5,
//第二个参数为当第一个参数不成立时,输出的自定义错误信息
() -> "Aborting test: not on developer workstation");
System.out.println("assume is true!");
}
@Test
public void testAssumeTrueLambda(){
//这个方法的第一个参数为函数式接口,无参数返回值为boolean
assumeTrue(()->{
System.out.println("in assumeTrue");
boolean flag = false;
return flag;
});
System.out.println("out assumeTrue");
}
@Test
public void testAssumThat() {
assumingThat(3>5,
() -> {
//与上述方法不同的是,仅当前面假设成立时,才会执行这里面的语句
//且只会影响到该lambda表达式中的代码
assertEquals(2, 2);
});
//此处的断言不受上述assumingThat限制,在所有情况下都会执行
System.out.println("no effect");
assertEquals("a string", "a string");
}
@Retention(RetentionPolicy.RUNTIME)
// 声明注解接口所使用的委托处理类
@ParametersSuppliedBy(BetweenSupplier.class)
public @interface Between{
// 声明所有可用参数,效果为 @Between([first = int,] last = int)
int first() default 0; // 声明默认值
int last();
}
(2)定义委托处理类 BetweenSupplier
public class BetweenSupplier extends ParameterSupplier {
@Override
public List getValueSources(ParameterSignature sig) {
// 自定义实参值列表
List list = new ArrayList();
// 获取注解变量
Between between = sig.getAnnotation(Between.class);
// 获取通过注解@Between传入的first值
int first = between.first();
// 获取通过注解@Between传入的last值
int last = between.last();
for (int i = first; i <= last; i++) {
// PotentialAssignment.forValue(String name, Object value)
// name为value的描述标记,没实际作用
// value为实参可选值
list.add(PotentialAssignment.forValue("name", i));
}
return list;
}
}
(3)调用方式
@RunWith(Theories.class)
public class TheoryDefinedTest {
@Theory
public final void test(@Between(last = 0) int i, @Between(first = 3, last= 10) int j) {
// i 取值为 0(first默认=0,last=0),j 取值为 3-10
System.out.println("i="+i+" j="+j);
}
}
public class FixtureTest {
private static int quantity = 0;
public FixtureTest() {
quantity++;
}
@BeforeClass
public static void breforeTestOnlyOnce() throws Exception {
System.out.println("Run before all test only once..."+ quantity);
}
@AfterClass
public static void afterTestOnlyOnce() throws Exception {
System.out.println("Run after all test only once..."+ quantity);
}
@Before
public void beforePerTest() {
System.out.println("Run before per test ..."+ quantity);
}
@After
public void afterPerTest() {
System.out.println("Run after per test ..."+ quantity);
}
//Test Method
@Test
public void testOne() {
System.out.println("testOne Start..."+ quantity);
}
@Test
public void testTwo() {
System.out.println("testTwo Start..."+ quantity);
}
}
运行结果:
Run before all test only once...0
Run before per test ...1
testOne Start...1
Run after per test ...1
Run before per test ...2
testTwo Start...2
Run after per test ...2
Run after all test only once...2
public interface FastTests { /* category marker */ }
public interface SlowTests { /* category marker */ }
public class A {
@Test
public void a() {
fail();
}
@Category(SlowTests.class)
@Test
public void b() {
}
}
@Category({SlowTests.class, FastTests.class})
public class B {
@Test
public void c() {
}
}
@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b and B.c, but not A.a
}
@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@ExcludeCategory(FastTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b, but not A.a or B.c
}
web.xml报错
The content of element type "web-app" must match "(icon?,display-
name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,s
JUnit4:Test文档中的解释:
The Test annotation supports two optional parameters.
The first, expected, declares that a test method should throw an exception.
If it doesn't throw an exception or if it
借鉴网上的思路,用java实现:
public class NoIfWhile {
/**
* @param args
*
* find x=1+2+3+....n
*/
public static void main(String[] args) {
int n=10;
int re=find(n);
System.o
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。
分析:这是不同系统编码格式引起的:在windows系统中编辑的.sh文件可能有不可见字符,所以在Linux系统下执行会报以上异常信息。
解决:
1)在windows下转换:
利用一些编辑器如UltraEdit或EditPlus等工具
Binary search tree works well for a wide variety of applications, but they have poor worst-case performance. Now we introduce a type of binary search tree where costs are guaranteed to be loga