java.lang.assertionerror:状态应为:< 200>,但未找到:< 404>spring启动页

9jyewag0  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(417)

checkout 控制器

@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 
  }
}

测试控制器

@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());

  }
}

在上面写着“/结帐”的地方,我可以放上所有其他可以接受的路线,但不是这条,我也不知道为什么。同样,在我登录运行项目后,它对我是可见的。
我尝试使用requestmapping而不是getmapping,但也不起作用。我在google上搜索没有成功,在大多数情况下,人们实际上没有指向正确的html文件,但这里也不是这样。我在这一点上迷路了,问我的朋友和同事都没有成功。
如果你有任何线索,可能是什么,不当的mvc设置,雅达达,请让我知道!

ny6fqffe

ny6fqffe1#

您的配置文件有冲突。您的控制器带有注解 @Profile("!test") 当您的测试正在执行时 @ActiveProfiles("test") .

相关问题