提问者:小点点

Junit5:相当于@规则和期望


我有一些用旧版本的jUnit编写的代码。现在我正在尝试迁移。

如何在Junit5中编写此内容?

 @Test
  public void testSignatureFailureRuntimeException() throws Exception {
    thrown.expect(java.lang.IllegalArgumentException.class);
    HmacUtil hmacUtil = new HmacUtil(HmacUtil.SignatureAlgorithm.HMAC_SHA1, "");
  }
  @Test
  public void testDeploymentInfoWithEmptyConfig() {
    thrown.expect(NullPointerException.class);
    deploymentId = UUID.randomUUID().toString();
    FunctionDeploymentInfo.FunctionDeploymentInfoBuilder builder = FunctionDeploymentInfo.builder();
    FunctionDeploymentInfo fdi = builder
      .withDeploymentId(deploymentId)
      .withFulfilledBy(this.getClass().getName())
      .forPodTag(MicrodoseConstants.ONE_DATA)
      .withConfig(new JsonObject())
      .build();

    LocalMap<String, FunctionDeploymentInfo> deploymentInfo = vertx.sharedData().getLocalMap(deploymentInfoKey);
    deploymentInfo.clear();
    LocalMap<String, String> addressIndex = vertx.sharedData().getLocalMap(addressIndexKey);
    addressIndex.clear();

    fdi.registerDeploymentInfo(addressIndex, deploymentInfo);
  }

另一个是规则


@ExtendWith(VertxExtension.class)
public class FunctionDeploymentInfoTest {

  @Rule
  public ExpectedException thrown = ExpectedException.none();

共1个答案

匿名用户

对于第一个,您可以尝试:

    Assertions.assertThrows(IllegalArgumentException.class, () -> new HmacUtil(HmacUtil.SignatureAlgorithm.HMAC_SHA1, ""));

对于第二个,也许这会对您有所帮助:如何在JUnit 5中替换WireMock@Law注释?