简体   繁体   中英

java.lang.AssertionError: Status expected:<200> but was:<404> Spring Boot Page Not found

CheckoutController

@Controller
@Profile("!test")
public class CheckoutController {

  private MonetaryAmount total = Money.of(0, EURO);
  private final UniqueInventory<UniqueInventoryItem> inventory;
  private final Katalog catalogue;

  private List<UniqueInventoryItem> history = new ArrayList();
  
  @Autowired
  public CheckoutController(UniqueInventory<UniqueInventoryItem> _inventory, Katalog _catalogue){
    inventory = _inventory;
    catalogue = _catalogue;
  }
  
  //!  Get Mappings

  //@GetMapping("/checkout")
  @RequestMapping("/checkout")
  @PreAuthorize("hasRole('WORKER')")
  public String checkout(Model model){
    model.addAttribute("total", this.total);
    model.addAttribute("history", this.history);
    model.addAttribute("controller", this);

    return "checkout"; // thymleafe located in proper path, visible when logged in 
  }
}

TestingController

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class CheckoutControllerTests {

  @Autowired
  MockMvc mockMvc;

  @BeforeEach
  public void pre() {
    // code
  }

  @Test
  public void checkout() throws Exception {
    // TODO Error msg: status expected:<200> but was:<404>
        mockMvc.perform(get("/checkout").with(user("paul").roles("WORKER")))
      .andExpect(status().isOk());


  }
}

Where it says "/checkout" above, I could put all other available routes which are accepted, but not this one and I do not know why. Again, it's visible to my once logged in running the project.

I tried using RequestMapping instead of GetMapping but that didnt work either. I googled with no success, in most cases people did not actually point to the right html file but thats not the case here either. I am lost at this point, asked my friends and colleagues with no success.

If you have any clue what it could be, improper mvc setup, yadada, please let me know!

You have conflicting profiles. Your controller is annotated with @Profile("!test") while your test is executing with @ActiveProfiles("test") .

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