关于工作用测试的一些小常识

本文探讨了单元测试中@Mock和@InjectMocks的区别,@BeforeMethod与@BeforeClass的适用场景,如何Mock静态方法,处理对象内方法互调的场景以及Mock私有方法的方法。介绍了PowerMockito在模拟静态方法中的作用以及使用反射访问和测试私有方法。
摘要由CSDN通过智能技术生成
问题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(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值