0

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); } 
5
  • Test, you provided, passes for me without any NullPointer with simple UrlConstructor implementation always returning expected string. Commented May 4, 2021 at 5:27
  • I added that my UrlConstructor is with @Component. When i debug it i see that urlConstructor is comming null. Am i define it wrong?.. Commented May 4, 2021 at 5:33
  • try using @Autowired private UrlConstructor urlConstructor; Commented May 4, 2021 at 5:37
  • 1
    post more of your UrlConstructor Class. Just because you instantiate your UrlConstructor does not mean that all the dependant beans are autowired. Have a look at docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/html/… Commented May 4, 2021 at 5:46
  • 1
    btw @Before is JUnit 4, @BeforeEach is the equivalent for JUnit 5. Commented May 4, 2021 at 5:48

1 Answer 1

2

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); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.