Liverpool
Liverpool

Reputation: 285

Unit test with JUnit 5

Hi all i receive Nullpointer when trying to execute this unit test.I want to test e class which receive 3 parameters and returns a string. I think i need to make @Before or something else but it didn't works. Do you have suggestions...Thanks !

public class UrlConstructorTest {

    private UrlConstructor urlConstructor;

    @Before
    public void setUp() {
        urlConstructor = new UrlConstructor();
    }

    public static final String TEST_UPDATE_MANIFEST_SR = "/packages/proxyId/test/test1/123/test3/test_test";

    @Test
    public void constructUpdateManifestSrInSasTokenTest() {
        String result = urlConstructor.composeDeviceRegistrationUrl("test","test123","test");
        System.out.println(result);
        assertNotNull(result);
        assertEquals(TEST, result);
    }
}

enter image description here

UrlConstructor is define like this:

@Component
public class UrlConstructor {

And this is the method in this class:

 public String composeDUrl(String deviceId, String scopeId) {
    return String.format(Constants.socpe, tes, test);
  }

Upvotes: 0

Views: 154

Answers (1)

Mehdi Benmesssaoud
Mehdi Benmesssaoud

Reputation: 534

In Junit5, you should be using @BeforeEach. Or you can get rid of that setUp method completely.

public class UrlConstructorTest {

       private final UrlConstructor urlConstructor = new UrlConstructor();

       public static final String TEST_SR = "/packages/proxyId/testID/product/testscope/testcomponent/coomponent_up";

      @Test
      public void constructTest() {
             String result = urlConstructor.composeDeviceRegistrationUrl("testID","coomponent_up","testscope");
             System.out.println(result);
             assertNotNull(result);
             assertEquals(TEST_SR, result);
      }
}

Upvotes: 2

Related Questions