1

I have some code which I have written in old version of jUnit. Now I am trying to migrate.

How to write this in 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);
  }

And another is @Rule


@ExtendWith(VertxExtension.class)
public class FunctionDeploymentInfoTest {

  @Rule
  public ExpectedException thrown = ExpectedException.none();
Piku
  • 15
  • 4

1 Answers1

0

With the first one you can try:

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

With the second one maybe this will help you: How to replace WireMock @Rule annotation in JUnit 5?

dcnis
  • 61
  • 4
  • Hi Can you also please look into another method testDeploymentInfoWithEmptyConfig? – Piku Jan 25 '22 at 23:17
  • @Piku Just put the code, which should throw the exception within the lamda-expression in JUnit5. `Assertions.assertThrows(NullPointerException.class, () -> // Put your code here );`. So in your case I think it would be: `Assertions.assertThrows(NullPointerException.class, () -> fdi.registerDeploymentInfo(addressIndex, deploymentInfo));`.
    I would also appreciate an accepted answer if it helped :)
    – dcnis Jan 25 '22 at 23:56
  • After .build(); it is throwing null pointer exception, I just debugged – Piku Jan 26 '22 at 00:03
  • I accepted the answer as one of your solution worked for me. Can you also please try to help me on second one – Piku Jan 26 '22 at 00:05
  • @Piku Do you mean with the second one the testDeploymentInfoWithEmptyConfig or the @Rule? When .build() is throwing the exception, you can try: `Assertions.assertThrows(NullPointerException.class, () -> builder.withDeploymentId(deploymentId).withFulfilledBy(this.getClass().getName()).forPodTag(MicrodoseConstants.ONE_DATA).withConfig(new JsonObject()).build());` Sry in comments it is really bad to format – dcnis Jan 26 '22 at 00:23