java.lang.AssertionError:预期状态:<200>,但在使用junit的测试类中为:<400>

时间:2018-07-10 10:44:17

标签: spring unit-testing junit4

我的代码需要一些帮助。我有这个控制器:

@RestController
@RequestMapping("/center")
public class CenterController {

private final Logger log = LoggerFactory.getLogger(CenterController.class);

@Autowired
private CenterService service;

@GetMapping(value = "/{idCenter}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(code = HttpStatus.OK)
public Center findBy(@PathVariable String idCenter) {
    log.debug("findBy --> Getting a center by id.");
    return service.findOne(idCenter);
}

和这个测试班

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServiceTestConfiguration.class)
public class CenterControllerTest {

private final Logger log = LoggerFactory.getLogger(CenterControllerTest.class);

private MockMvc mockMvc;    
private ObjectMapper mapper; 
private Center center;
private Center center2;
private ArrayList<Center> listCenters;

@MockBean
private CenterService centerService;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setup() {
    log.debug("CenterControllerTest --> setup()");
    mapper = new ObjectMapper();
    loadData();
    CenterController centerController = new CenterController();
    this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@After
public void teardown() {
    log.debug("CenterControllerTest --> teardown()");
    centerService.deleteAll();
}

private void loadData() {
    log.debug("centerControllerTest --> loadData() Loading test data");

    center = new Center();
    center.setIdCenter("1");
    center.setCenterCode("123");
    center.setStreet("Test street");
    center.setPopulation("Test population");
    center.setProvince("Test province");
    center.setPostalCode("Test postal code");
    center.setTelephone((long) 999888777);
    center.setActive(true);
    centerService.save(center);

}

@Test
public void getCenterByIdTest2() throws Exception{
    when(centerService.findOne("1")).thenReturn(new Center());
    mockMvc.perform(get("/center")
            .param("idCenter", "1"))
        .andExpect(status().isOk());
}

当我尝试对其进行测试时,它给出了以下跟踪信息

  

java.lang.AssertionError:预期状态:<200>,但在:<404>   org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55)     在   org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:82)     在   org.springframework.test.web.servlet.result.StatusResultMatchers.lambda $ matcher $ 9(StatusResultMatchers.java:619)     在   org.springframework.test.web.servlet.MockMvc $ 1.andExpect(MockMvc.java:178)     在   com.api.controller.CenterControllerTest.getCenterByIdTest2(CenterControllerTest.java:90)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)     在   org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)     在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在   org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)     在   org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)     在   org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)     在   org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)处   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)     在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)处   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

我认为这是一个配置问题,任何人都可以注意到代码失败在哪里吗?

0 个答案:

没有答案