在spring-boot中实现TDD,并且我不能在控制器中使用@valid注释



这是控制器

package com.Controller;
import org.springframework.beans.factory.annotation.Autowired;
//import java.util.ArrayList;
//import java.util.List;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class RegistrationController {
@Autowired
RegistrationRepository repository;

@PostMapping("/registration")
public User registration(@val @RequestBody User user){
// save a single Customer       
return repository.save(user);
//      return "Customer is created";

}

这是我的构建等级

plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'newproject'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'    
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
runtimeOnly 'mysql:mysql-connector-java'
runtimeOnly 'org.postgresql:postgresql'
testImplementation('org.springframework.boot:spring-boot-starter-test') {

exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'

}
testImplementation 'org.testcontainers:junit-jupiter'
}
test {
useJUnitPlatform()
}

这是我的TDD测试类

package com.Controller;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
//import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.web.client.RestTemplate;
//import io.netty.handler.codec.http.HttpMethod;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = LotteryApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class LotteryApplicationTests {
@Autowired
private TestRestTemplate restTemplate;

@LocalServerPort
private int port;

private String getRootUrl() {
return "http://localhost:" + port + "/api/v1";
}

@Test
public void contextLoads() {}

@Test
public void Registration () {

User user = new User();
user.setNick_name("amon");
user.setNmfirts_name("amon");
user.setNmmid_name("aron");
user.setNmlast_name("job");
user.setUserprofile_tpid(321);
user.setUseruser_tpid(312);
user.setUserstatus_tpId(332);
user.setFullpathname_picture("mmmmm");
user.setId_owner(1);


ResponseEntity<User> postResponse =restTemplate.postForEntity(getRootUrl() + "/registration", user, User.class);
Assert.assertNotNull(postResponse);
Assert.assertNotNull(postResponse.getBody());
}       
}
public User registration(@val @RequestBody User user){

你在使用@val——这是故意的吗?

最新更新