Cavitation in a liquid moving past a constraint is numerically investigated by means of a free-energy lattice Boltzmann simulation based on the van der Waals equation of state. The fluid is streamed past an obstacle and, depending on the pressure drop between inlet and outlet, vapor formation underneath the corner of the sack-wall is observed. The circumstances of cavitation formation are investigated and it is found that the local bulk pressure and mean stress are insufficient to explain the phenomenon. Results obtained in this study strongly suggest that the viscous stress, interfacial contributions to the local pressure, and the Laplace pressure are relevant to the opening of a vapor cavity. This can be described by a generalization of Joseph's criterion that includes these contributions. A macroscopic investigation measuring mass flow rate behavior and discharge coefficient was also performed. As theoretically predicted, mass flow rate increases linearly with the square root of the pressure drop. However, when cavitation occurs, the mass flow growth rate is reduced and eventually it collapses into a choked flow state. In the cavitating regime, as theoretically predicted and experimentally verified, the discharge coefficient grows with the Nurick cavitation number.