Skip to Main Content

Issue with unit tests

User_I8PP6Nov 27 2022

Hello,
I need to write unit test for the below method in my reactive Java application.

@Configuration
public class MyController
{

@Bean
protectded RouterFunction<ServerResponse> myRoutes() {
return route()
.GET("/test", request -> ok().bodyValue("Welcome to test page"))
.build();
}

public RouterFunction<ServerResponse> myRoutesMethod() {
return myRoutes();
}

}

I tried the below but I keep getting NullPointerException for **myController ** object and the test fails. Any thoughts on this please on how to get the test working?
Thank you!

@ContextConfiguration(classes = {MyController.class})
public class MyControllerTest {

[@Autowired](https://apexds-adb-curie.apexdev.oraclecorp.com/ords/apexds/user/Autowired)
MyController myController;


[@Test](https://apexds-adb-curie.apexdev.oraclecorp.com/ords/apexds/user/Test)
public void testMyRoutes() {

RouterFunction\<ServerResponse> result =  myController.myRoutesMethod();
Assertions.assertNotNull(result);

}   

}
Stacktrace:

java.lang.NullPointerException at 

MyController.myRoutesMethod(MyController.java)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at
java.base/java.lang.reflect.Method.invoke(Method.java:564) at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) [junit-4.12.jar:4.12] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12] at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) [junit-4.12.jar:4.12] at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.12.jar:4.12] at
org.junit.internal.runners.ParentRunner$3.evaluate(RunBefores.java:26) at
org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)at
org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at
org.junit.runners.ParentRunner.run(ParentRunner.java:290) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at
org.junit.runners.ParentRunner.access$000(ParentRunner .java:58)at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at
org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:268) at
org.junit.runners.ParentRunner.run(ParentRunner.java:363) at
org.junit.runner.JUnitCore.run(JUnitCore.java:137)at
com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:55) at
com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at
com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) at
com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Comments
Post Details
Added on Nov 27 2022
0 comments
15 views