简体   繁体   English

java.lang.AssertionError:预期状态:<200> 但为:<404> 对于 Spring 启动 Rest Controller

[英]java.lang.AssertionError: Status expected:<200> but was:<404> for Spring Boot Rest Controller

This is the error when I run my Integration Test class:这是我运行集成测试时的错误 class:

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /worker/savePaper
       Parameters = {idWorker=[123], workerType=[night], abilities=[{'security'}]}
          Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"205"]
             Body = {
  "workerEntity" : {
    "idWorker" : "123",
    "isAlive" : true,
    "isWorking" : true,
    "validatePro" : true,
    "validatePen" : true,
    "validateBi" : true
  }
}
    Session Attrs = {}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status 
Expected :200
Actual   :404

This is the Test Class:这是测试 Class:

@RunWith(SpringRunner.class)
@WebMvcTest(WorkerController.class)
public class WorkerControllerIntegrationTest {
    final private String URL_SAVE_PAPER = "/worker/savePaper";

    @MockBean
    IWorkerService workerService;

    @MockBean
    IWorkerAbilititesService workerAbilitiesService;

    @Autowired
    private MockMvc mvc;

    public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testSavePaper() throws Exception {
        PaperFormform = new PaperForm(
                new WorkerEntity());
        form.getWorkerEntity().setIdWorker("123");
        form.getWorkerEntity().setIsAlive(true);
        form.getWorkerEntity().setIsWorking(true);
        form.getWorkerEntity().setValidatePro(true);
        form.getWorkerEntity().setValidatePen(true);
        form.getWorkerEntity().setValidateBi(true);

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        String requestJson=ow.writeValueAsString(form);

        mvc.perform(post(URL_SAVE_PAPER).contentType(APPLICATION_JSON_UTF8)
                .param( "idWorker", "123")
                .param("workerType", "night")
                .param("abilitities", "{'security'}")
                .content(requestJson))
                .andExpect(status().isOk())
        .andReturn();
    }

}

My Object:我的Object:

import lombok.*;

import javax.persistence.*;

@Entity
@Data
@Table(name = "worker")
@AllArgsConstructor
@NoArgsConstructor
public class WorkerEntity implements java.io.Serializable {

    @Id
    @Column(name = "id_worker")
    private String idWorker;

    @Column(name = "is_alive")
    private boolean isAlive;

    @Column(name = "is_working")
    private boolean isWorking;

    @Column(name = "validate_pro")
    private Boolean ValidatePro;

    @Column(name = "validate_pen")
    private Boolean validatePen;

    @Column(name = "validate_bi")
    private Boolean validateBi;
}

My Mapping Class with the front: (I am recieving datas via a form)我的映射 Class 与前面:(我通过表格接收数据)

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.app.entity.WorkerEntity;
import lombok.AllArgsConstructor;
import lombok.Data;

import javax.validation.constraints.NotNull;

@JsonDeserialize(as = WorkerEntity.class)
@AllArgsConstructor
@Data
public class paperForm {
    @NotNull
    private WorkerEntity workerEntity;
}

My Controller:我的Controller:

@RestController
@RequestMapping(value = "worker")
@RequiredArgsConstructor
public class WorkerController {

    @Autowired
    private IWorkerService workerService;

    @Autowired
    private IWorkAbilitiesService workAbilitiesService;

    @PostMapping(value = "savePaper")
    @ResponseBody
    @RequestMapping(consumes="application/json",
            produces="application/json")
    public ResponseEntity<WorkerEntity> savePaper(@Valid @RequestBody final PaperForm form, @RequestParam final String idWorker, @RequestHeader("workerType") String workerType,
                                                          @RequestHeader(required = false, value = "Abilities") String[] abilities) {

        HttpStatus statut = HttpStatus.FORBIDDEN;
        WorkerEntity entity = null;

        if (workAbilitiesService.validateAbilities(abilities, idWorker, workerType)) {
            final WorkerEntity w =orkerEntity new WorkerEntity();
            if (workerEntity.getValidatePro() && workerEntity.getValidatePen() && workerEntity.getValidateBI()) {
                workerEntity.setIsAlive(form.getWorkerEntity().isAlive());
                workerEntity.setIsWorking(form.getWorkerEntity().isWorking());
                status = HttpStatus.OK;
                entity = this.workerService.savePaper(workerEntity);
            }
        }
        return ResponseEntity.status(statut).body(entity);
    }
}

My Service:我的服务:

@Transactional
@Service
public class WorkerServiceImpl implements IWorkerService {

    @Autowired
    private IWorkerRepository workerRepository;
}

At first I had 415 status for days and I could'nt reslove it, but I some how managed to pass it and now I have a 404 failure status.起初我有几天都是 415 状态,我无法重新爱它,但我知道如何设法通过它,现在我有一个 404 失败状态。

You are missing / , hence 404 not found.您缺少/ ,因此找不到 404。 It should be @RequestMapping(value = "/worker") and它应该是@RequestMapping(value = "/worker")

@PostMapping(value = "/savePaper") in order to form /worker/savePaper @PostMapping(value = "/savePaper")为了形成/worker/savePaper

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.AssertionError:预期状态:&lt;200&gt; 但原为:&lt;404&gt; Spring 引导页面未找到 - java.lang.AssertionError: Status expected:<200> but was:<404> Spring Boot Page Not found java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> java.lang.AssertionError:预期状态:&lt;200&gt;,但在静态服务中为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> in restful services @RequestMapping java.lang.AssertionError:预期状态:200 实际:404 - @RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404 java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&gt; in Junit test - java.lang.AssertionError: Status expected:<200> but was:<404> in Junit test java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&gt; with test case failed - java.lang.AssertionError: Status expected:<200> but was:<404> with test cases failing java.lang.AssertionError: 预期状态:&lt;200&gt; 但为:&lt;400&gt; - java.lang.AssertionError: Status expected:<200> but was:<400> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;201&gt; - java.lang.AssertionError: Status expected:<200> but was:<201> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;405&gt; - java.lang.AssertionError: Status expected:<200> but was:<405> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;302&gt; itShouldAllowAccessToSecuredPageForPermittedUser - java.lang.AssertionError: Status expected:<200> but was:<302> itShouldAllowAccessToSecuredPageForPermittedUser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM