JUnit 5

JUnit 5 测试 <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.8.0</version> <scope>test</scope> </dependency> import org.junit.jupiter.api.*; public class MyTest { @BeforeAll static void init(){ System.out.println("init"); } @BeforeEach public void before(){ System.out.println("before"); } @AfterEach public void after(){ System.out.println("after"); } @Test public void testAdd(){ Assertions.assertEquals(2,2); System.out.println("123"); } @Test public void testAdd2(){ Assertions.assertEquals(2,2); System.out.println("456"); } } 输出结果: init before 123 after before 456 after SpringBoot测试 @SpringBootTest会创建Spring boot上下文 @Autowired、@SpyBean和@MockBean,可以替换原来的Bean 例如,在测试写数据库时,我们一般不直接写数据库,而是模拟该函数的运行。 @SpringBootTest public class MyTest{ @SpyBean //类似Autowired自动注入,但是是模拟的 Svc svc1; @Test void t1(){ when(svc1....

January 13, 2022 · 1 min · alvazu

Java 注解和反射的示例代码

一、注解 1. 初识注解 package com.onlineframework; import java.util.ArrayList; import java.util.List; @SuppressWarnings("all") public class annoTest { @Override public String toString() { return super.toString(); } @Deprecated public static void dep(){ System.out.println("dep"); } public static void test02(){ List list = new ArrayList(); } public static void main(String[] args) { dep(); test02(); } } 2. 元注解 package com.onlineframework; import java.lang.annotation.*; @MyAnnotation public class AnnoTest02 { @MyAnnotation public void test(){ } } @Retention(RetentionPolicy.SOURCE) @Target({ElementType.METHOD,ElementType.TYPE}) @Documented @Inherited @interface MyAnnotation{ //@interface自动继承了java....

January 13, 2022 · 7 min · alvazu