简体   繁体   中英

implementing TDD in spring boot and i cant use @valid annotation in controller

Here is the controller

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";
    
}

This is my build gradle

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()
}

This is my TDD test class

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 - 这是故意的吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM