title: java.lang.AssertionError: Status Expected:<200> but was:<400> in testing class with junit
body: <p>I need some help with my code. I have this controller:</p>
  
  <pre><code>@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 --&gt; Getting a center by id.");
      return service.findOne(idCenter);
  }
  </code></pre>
  
  <p>and this testing class</p>
  
  <pre><code>@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&lt;Center&gt; listCenters;
  
  @MockBean
  private CenterService centerService;
  
  @Autowired
  private WebApplicationContext webApplicationContext;
  
  @Before
  public void setup() {
      log.debug("CenterControllerTest --&gt; setup()");
      mapper = new ObjectMapper();
      loadData();
      CenterController centerController = new CenterController();
      this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  }
  
  @After
  public void teardown() {
      log.debug("CenterControllerTest --&gt; teardown()");
      centerService.deleteAll();
  }
  
  private void loadData() {
      log.debug("centerControllerTest --&gt; 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());
  }
  </code></pre>
  
  <p>When I try to test it, it gives me the following trace</p>
  
  <blockquote>
    <p>java.lang.AssertionError: Status expected:&lt;200> but was:&lt;404>     at
    org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55)
      at
    org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:82)
      at
    org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:619)
      at
    org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:178)
      at
    com.api.controller.CenterControllerTest.getCenterByIdTest2(CenterControllerTest.java:90)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)     at
    org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at
    org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at
    org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at
    org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at
    org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
      at
    org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
      at
    org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at
    org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at
    org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at
    org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at
    org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)    at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
      at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)  at
    org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)   at
    org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)     at
    org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)   at
    org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)  at
    org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at
    org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)    at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
      at
    org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at
    org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)</p>
  </blockquote>
  
  <p>I think it's a configuration problem, can anyone notice where is the code failure?</p>