问题1:@Mock和@InjectMocks的区别
@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface Mock {
Answers answer() default Answers.RETURNS_DEFAULTS;
String name() default "";
Class<?>[] extraInterfaces() default {
};
boolean serializable() default false;
}
可以看出@Mock用于类的属性注解,且运行时才能发挥作用。
@Documented
@Target(FIELD)
@Retention(RUNTIME)
public @interface InjectMocks {
}
@InjectMocks用于类的属性注解,且运行时发挥作用。
Mark a field as a mock.
@Mock注解的属性对象,在代码运行时,调用属性的方法不会进入具体的方法体;
Mark a field on which injection should be performed.
@InjectMocks注解的属性对象,在代码运行时,调用该属性的方法,则会进入具体的方法体。
问题2:@BeforeMethod和@BeforeClass的区别
@BeforeMethod注解源码
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.METHOD)
public @interface BeforeMethod {
public boolean enabled() default true;
public String[] groups() default {
};
public String[] dependsOnGroups() default {
};
public String[] dependsOnMethods() default {
};
public boolean alwaysRun() default false;
public boolean inheritGroups() default true;
public String description(