如何在TestNG

时间:2017-05-23 05:46:08

标签: spring mockito testng spring-test springmockito

我使用Spring Boot作为我的网络应用程序,使用TestNG进行单元测试。以下是我尝试的单元测试

@ContextConfiguration
public class AuthorizerTest extends AbstractTestNGSpringContextTests {

    @InjectMocks
    private Authorizer authorizer = new Authorizer();

    @Mock
    private PermissionRuleRepository permissionRuleRepository;

    @BeforeMethod
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    @WithMockUser
    public void testCheckPermission() throws Exception {
        try {
            authorizer.checkPermission(null, PermissionLevel.Type.ACCOUNT_OTHER);
            fail();
        } catch (AccessDeniedException ade) {
            //
        }
    }
}

authorizer.checkPermission在内部使用SecurityContext获取当前用户名。但在调试时,authentication对象为null。不确定为什么在使用WithMockUser时没有填充。

0 个答案:

没有答案