如何在春季合同测试中通过自定义安全性?

时间:2019-03-12 14:35:08

标签: java spring-test

我正在尝试使用春季合同,这是我的合同文件:

package contracts
import org.springframework.cloud.contract.spec.Contract

Contract.make {
  request {
       method POST()
       url '/v1/wallet'
       body("{\n" +
            "      \"externalRefType\":\"BIKER\",\n" +
            "      \"externalRefId\":\"9977633951\"\n" +
            "    }")
       headers {
            header 'Content-Type': 'application/json'
            header 'Authorization': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzeXN0ZW0iLCJ0eXBlIjoic3lzdGVtIn0.ITL-gCOBAzgnMWcCHl4ztywLDKZxNgRbXzSfSWzT72wWoKqPr_PLLET3qFUd_cfRRZZMavjMxyz8_Uqin4j2-A'
    }
}
response {
    status 200
    body("{\n" +
            "    \"walletId\": 135\n" +
            "}")
}
}

基类:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = WalletServiceApplication.class)
public class BaseWallet {

   @Autowired
   private WalletController walletController;

@Before
public void setup() {
    RestAssuredMockMvc.standaloneSetup(walletController);
}}

WalletServiceApplication类:

@SpringBootApplication
@EnableAsync
@EnableScheduling
@EnableSnappboxSecurity
@EnableSnappboxClientSecurity
@EnableTransactionManagement
public class WalletServiceApplication {

        public static void main(String[] args) {
            SpringApplication.run(WalletServiceApplication.class, args);
          }}

最后是WalletController:

@RestController
@RequestMapping(value = "/v1/wallet")
public class WalletController {

      @Secured(SYSTEM)
      @PostMapping
      public ResponseEntity<?> createWallet(@Valid @RequestBody WalletCreateRequest walletParams) {
           LOGGER.debug(walletParams.toString());
            return new ResponseEntity<>(new WalletCreatedResponse(135), OK);
}}

但是它没有通过,当我部署它时,出现以下错误:

  

运行测试:1,失败:0,错误:1,跳过:0,经过的时间:10.964   秒<<<失败! -在
  org.springframework.cloud.contract.verifier.tests.ContractVerifierTest   validate_createWalletContract(org.springframework.cloud.contract.verifier.tes   ts.ContractVerifierTest)经过的时间:0.347秒<<<错误!   org.springframework.web.util.NestedServletException:请求   处理失败;嵌套异常为   org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:   在SecurityContext中找不到身份验证对象

有人可以帮我吗?

0 个答案:

没有答案