gpt4 book ai didi

java - 模拟 spring 方法注释在测试用例中抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 10:41:56 28 4
gpt4 key购买 nike

我在 spring 项目中有一个自定义验证注解。

注释的目的是接受应该是 id 和 dept 值的参数名称。

切面的目的是从注解中获取paramNames,在方法签名中找到相应的参数位置,从识别到的位置获取值并使用值进行验证。

这是我到目前为止编写的类。

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface ValidateDeptAnnotation {
String id();
String dept();
}

注释方法时执行验证的方面。

@Aspect
@Component
public class ValidateDeptAspect {

@Before("@annotation(<package>.ValidateDeptAnnotation)")
public void runValidation(JoinPoint joinPoint) throws MemberIdentityException {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
ValidateDeptAnnotation annotation = method.getAnnotation(ValidateDeptAnnotation.class);
String id = null;
String dept = null;
for (int index = 0; index < signature.getParameterNames().length; index++) {
String paramterName = signature.getParameterNames()[index];
if (annotation.dept().equals(paramterName)) {
dept = joinPoint.getArgs()[index].toString();
} else if (annotation.id().equals(paramterName)) {
id = joinPoint.getArgs()[index].toString();
}
}
//....further processing...throw appropriate error msgs logic
}

}

测试类

@RunWith(PowerMockRunner.class)
@PrepareOnlyThisForTest({Method.class})
public class ValidateDeptAspectTestMockedMethod {
@InjectMocks
private ValidateDeptAspect validationAspect;
@Mock
private JoinPoint joinPoint;

@Mock
private MethodSignature methodSignature;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testIdParamNotFound() {
String[] methodSignatureParams = {"id","dept"};
String[] argumentValues= {"789","dept-789-pacman"};

Mockito.doReturn(methodSignature).when(joinPoint).getSignature();
Mockito.doReturn(methodSignatureParams).when(methodSignature).getParameterNames();
Mockito.doReturn(argumentValues).when(joinPoint).getArgs();
ValidateDeptAnnotation annotation = Mockito.mock(ValidateDeptAnnotation.class);

Method mockedMethod = PowerMockito.mock(Method.class);
Mockito.doReturn(mockedMethod).when(methodSignature).getMethod();
PowerMockito.doReturn(annotation).when(mockedMethod).getAnnotation(Mockito.any());
// PowerMockito.when(mockedMethod.getAnnotation(ValidateDept.class)).thenReturn(annotation); --didnot work, same error.
Mockito.doReturn("iiiiid").when(annotation).id();
Mockito.doReturn("dept").when(annotation).dept();

validationAspect.runValidation(joinPoint);


///...further assertion logic to check for error message as iiiid != id
//but never gets here.

}
}

当我运行测试用例时,它失败并在方面中的行出现 NullPointerException。

if (annotation.dept().equals(paramterName))  

我调试测试用例的时候,这里的注解是正确获取的。

PowerMockito.doReturn(annotation).when(mockedMethod).getAnnotation(Mockito.any());

但是,对切面方法的调用会抛出 NPE。任何帮助深表感谢。

提前致谢。

最佳答案

对于那些徘徊在这种情况下的人。我还没有想出如何解决问题或问题是什么。

我目前已经接受了这种方法 - 在测试用例中创建虚拟方法来反射(reflect)您的测试用例并正常处理测试用例。

代码:

@RunWith(MockitoJUnitRunner.class)
public class ValidateDeptAspectTestMockedMethod {

@InjectMocks
private ValidateDeptAspect validationAspect;
@Mock
private JoinPoint joinPoint;

@Mock
private MethodSignature methodSignature;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testIdParamNotFound() {
String[] methodSignatureParams = {"id","dept"};
String[] argumentValues= {"789","dept-789-pacman"};

Mockito.doReturn(methodSignature).when(joinPoint).getSignature();
Mockito.doReturn(methodSignatureParams).when(methodSignature).getParameterNames();
Mockito.doReturn(argumentValues).when(joinPoint).getArgs();

Method method = myMethod("noMemberId");
Mockito.doReturn(method).when(methodSignature).getMethod();

validationAspect.runValidation(joinPoint);


///...further assertion logic to check for error message
// The test is successful.
}

@ValidateDeptAnnotation(memberId = "",accessToken = "accessToken")
private void noMemberId(String memberId, String accessToken) {}
}

关于java - 模拟 spring 方法注释在测试用例中抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38320820/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com