Spring WebFlux Test Throwing "IllegalStateException: Failed to load ApplicationContext" Exception - java

I created a simple Spring WebFlux REST api and I wanted to write test cases to test it end to end (No mocking).
But it is throwing error message.
Here are my classes.
Rest Controller Class
#RestController
#RequestMapping("/users")
public class UsersController {
private final UserService userService;
#Autowired
public UserController(UserService userService) {
this.userService = userService;
}
#RequestMapping(path = "list")
public Mono<ResponseEntity> getusersList() {
return userService.getusers();
}
Rest Controller Test Class
#RunWith(SpringRunner.class)
#WebFluxTest
public class UserControllerTestIT {
#Mock
private UserService userService;
#Autowired
private WebTestClient webTestClient;
#Test
public void testApplicationsList() {
this.webTestClient
.get()
.uri("/users/list")
.exchange();
}
}
But I'm getting the following error message. How to fix the issue ?
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

If you are running a test with SpringRunner.class then you need to use #MockBean instead of #Mock.
Please refer to the spring boot documentation
#RunWith(SpringRunner.class)
#WebFluxTest
public class UserControllerTestIT {
#MockBean
private UserService userService;
#Autowired
private WebTestClient webTestClient;
#Test
public void testApplicationsList() {
this.webTestClient
.get()
.uri("/users/list")
.exchange();
}
}
Also,#WebFluxTest is used in combination with #MockBean or #Import to create any collaborators required by your #Controller beans.

Related

Getting NPE on ControllerTest using SpringBoot WebFluxTest and JUnit4

I've this project structure:
And I am trying to test a dummy method on the Controller. The funcionality is very simple. You send a String by POST and is returned with a + "123"
CustomerServiceImpl.java
package com.example.demo.service;
import org.springframework.stereotype.Service;
#Service
public class CustomerServiceImpl implements CustomerService {
#Override
public String dummyEndpoint(String str) {
return str + "123";
}
}
CustomerController.java
package com.example.demo.controller;
import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
#RestController
#RequestMapping("/customers")
public class CustomerController {
#Autowired
private CustomerService customerService;
#PostMapping(value = {"/dummy"})
#ResponseStatus(HttpStatus.OK)
public String postDummy(#RequestBody String str) {
return customerService.dummyEndpoint(str);
}
}
And the controller test class:
CustomerControllerTest.java
package com.example.demo.controller;
import com.example.demo.service.CustomerServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
#WebFluxTest(controllers = CustomerController.class)
#RunWith(SpringRunner.class)
public class CustomerControllerTest {
#Autowired
WebTestClient webTestClient;
#MockBean
CustomerServiceImpl customerService;
#Test
public void dummyTest() {
this.webTestClient.post().uri("/customers/dummy")
.syncBody("hello")
.exchange()
.expectStatus().isOk()
.expectBody(String.class)
.value(c -> c.equals("hello123"));
}
}
Then, when I test the exepectSatus().isOk() the test is passed:
#Test
public void dummyTest() {
this.webTestClient.post().uri("/customers/dummy")
.syncBody("hello")
.exchange()
.expectStatus().isOk();
}
But if I add the rest of funcionality I get a NPE on the 'c' lambda variable as a Customer object. I am new doing this kind of testing so I don't know what is happening.
#Test
public void dummyTest() {
this.webTestClient.post().uri("/customers/dummy")
.syncBody("hello")
.exchange()
.expectStatus().isOk()
.expectBody(String.class)
.value(c -> c.equals("hello123"));
}
NPE:
java.lang.NullPointerException
at com.example.demo.controller.CustomerControllerTest.lambda$dummyTest$0(CustomerControllerTest.java:29)
at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.lambda$value$3(DefaultWebTestClient.java:407)
at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:197)
at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.value(DefaultWebTestClient.java:407)
at com.example.demo.controller.CustomerControllerTest.dummyTest(CustomerControllerTest.java:29)
at 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:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
The example is mocking CustomerServiceImpl but doesn't stub the customerService.dummyEndpoint() method call.
By default Mockito will return null for a non-stubbed method call. This is why just checking the status passes. Since the value is null, value(c -> c.equals("hello123") will fail with NPE.
You'll need to stub the method call:
when(customerService.dummyEndpoint("hello")).thenReturn("hello123");
Of course this doesn't now test the real service, but it's something that should not be tested in a #WebFluxTest.
Obviously this is a dummy example, but if you want to test the service functionality, you can write a plain unit test that verifies that calling the service method returns what is wanted.

org.mockito.exceptions.misusing.InjectMocksException: Cannot instantiate #InjectMocks

I am using #InjectMocks to inject Repository Implementation into my Test class, but it throws InjectMocksException. Below is my code and Error, please help how to resolve this error?
Error:
org.mockito.exceptions.misusing.InjectMocksException:
Cannot instantiate #InjectMocks field named 'muRepository' of type 'class com.example.MyrRepositoryImpl'.
You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : null
at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
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$3.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.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:77)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:83)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.NullPointerException
at com.example.MyRepositoryImpl.<init>(MyRepositoryImpl.java:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
Injecting:
#InjectMocks
private MyRepositoryImpl myRepository;
Test:
#Test (expected = NullPointerException.class )
public void procTest( ) throws Exception
{
when( addProc.execute( null,
null,
null
)
).thenThrow( new NullPointerException() );
myRepository.addUser( null );
}
Repository:
public class MyRepositoryImpl implements MyRepository
{
private final AddProc addProc;
#Autowired
public MyRepositoryImpl( final ProcFactory procFactory )
{
this.addProc = procFactory.create( AddProc.class ); //this is line 27 referenced in error
}
#Override
public User addUser( User user)
{
return addProc.execute(
user.getUserName( ),
user.getFirstName( ),
user.getLastName( )
);
}
}
Mockito uses the constroctor injection but you didnt use mock object for ProcFactory class.
So null pointer exception occurs. Either you have to use ProcFactory as mock in test class or, you have to add null check.
#Mock
private ProcFactory procFactory;
Regards
Having seen that you are running tests on JUnit 4, in order to initialize and inject mocks through #InjectMocks needs to use #RunWith(MockitoJUnitRunner.class) as test runner or Mockito.initMocks(this) in the setup step.
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

Best practices on how to test a void returning method in JUnit and Spring?

I am not sure if I am testing a void returning method the correct way and also if my class-under-test (cut) requires any change in order to make it 100% testable and bug-proof.
I am seeing NullPointerException while executing the test because loginOperations is not getting set.
Error:
java.lang.NullPointerException
at com.demo.service.LoginService.doLogin(LoginService.java:40)
at com.demo.service.LoginServiceTest.doLogin(LoginServiceTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
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$3.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.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
LoginService.java
#Service
public class LoginService {
#Autowired
private ILoginOperations loginOperations;
public void doLogin(HttpServletRequest request, String result) {
LoginDTO loginDTO = new LoginDTO(request.getParameter("username"), result);
loginOperations.doLogin(loginDTO);
}
}
LoginServiceTest.java
public class LoginServiceTest {
private LoginService instance = new LoginService();
ILoginOperations loginOperations = Mockito.mock(ILoginOperations.class);
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
String result = "some string";
#Test
public void doLogin() {
when(request.getParameter("username")).thenReturn("johndoe");
instance.doLogin(request, result); //throws NPE while calling loginOperations.doLogin() because
assertNotNull(instance); //IS THIS THE CORRECT WAY TO TEST A VOID RETURNING METHOD ???
}
}
Now, there are 2 ways to fix the test.
I can fix the class-under-test by adding a setter method for loginOperations class and call that setter method in the test
Change #Test public void doLogin() { to #Test(expected = Exception.class) public void doLogin() {
Not sure which one is the best practice above and why.
Another Question:
Other question that I have is how to assert on a method that returns nothing. There is something like verify() but not sure how to use it.
1.You can fix the test case by adding setter method in LoginService or you can use constructor injection like -
#Autowired
public LoginService(ILoginOperations loginOperations) {
this.loginOperations = loginOperations;
}
Validating exception as #Test(expected = Exception.class) public void doLogin() is certainly not a good idea as doLogin method does not throw exception in normal circumstance.
The better way to test method with void return type is using verification API (example - mockito verification API example). You can also use Mockito's ArgumentCaptor to capture argument and assert state of that argument, along with verification API as -
#RunWith(MockitoJUnitRunner.class)
public class LoginServiceTest {
#Captor
private ArgumentCaptor<LoginDTO> captor;
#Mock
private ILoginOperations loginOperations;
#Mock
private HttpServletRequest mockServletRequest;
#InjectMocks
private LoginService loginService;
#Test
public void validateLogin() {
when(mockServletRequest.getParameter("username")).thenReturn("mock_user_name");
loginService.doLogin(mockServletRequest, "mock_result");
verify(loginOperations).doLogin(captor.capture());
LoginDTO expectedLoginDTO = captor.getValue();
assertThat(expectedLoginDTO.getResult(), is("mock_result"));
assertThat(expectedLoginDTO.getUsername(), is("mock_user_name"));
}
}
There is an excellent article from Martin Fowler about this method of testing - Mocks Aren't Stubs
actually you should create a constructor for your LoginService that gets the ILoginOperations, in this way you can create the LoginService in your test class and pass the mocked ILoginOperations as parameter, all this stuff should be done in a #Before method.
Or you can try with #InjectMocks for your LoginService and have your ILoginOperations annotated as #Mock.

Nullpointer exception on constructor injection and works with field injection mockito

I have an abstract class which has a dependency which is being autowired:
public abstract class ClassB {
#Autowired
private ClassC classC;
public String getValue() {
classC.getSomeMethod();
}
}
I've a class which extends this abstract class:
#Component
public class ClassA extends ClassB {
#Autowired
private ClassD classD;
public String getClassAMethod() {
String value = getValue();
String dReturn = classD.getD();
return value + dReturn;
}
}
Now while doing UnitTesting I can do:
public class ClassATest {
#Mock
private ClassC classC;
#Mock
private ClassD classD;
#InjectMocks
private ClassA classA;
#Test
public void testSomething() {
when(classC.getSometMethod()).thenReturn("classC");
when(classD.getD()).thenReturn("classD");
assertEquals(classA.getClassAMethod(), "classCclassD");
}
}
This works fine, however if I use constructor injection for ClassA I get a Null pointer exception for classC.
#Component
#RequiredArgsConstructor(onConstructor = #__(#Autowired))
public class ClassA extends ClassB {
private final ClassD classD;
public String getClassAMethod() {
String value = getValue();
String dReturn = classD.getD();
return value + dReturn;
}
}
In the second case I even tried replacing InjectMocks with a normal constructor invocation, but ClassC object doesn't get mocked.
Stack trace:
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
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$3.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.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(Unknown Source)
at org.mockito.internal.runners.DefaultInternalRunner.run(Unknown Source)
at org.mockito.internal.runners.StrictRunner.run(Unknown Source)
at org.mockito.junit.MockitoJUnitRunner.run(Unknown Source)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Lombok's #AllArgsConstructor and #RequiredArgsConstructor only cover fields that are declared in the class itself. Fields from superclasses are not considered, as lombok cannot access those classes, because they are not yet resolved at the time lombok runs during the compilation process.
As a result in your case, the constructor that is generated for ClassA only has ClassD classD as argument, but not ClassC classC. (You can see that in the class outline view of your IDE, or by delomboking your code.) Therefore, classC remains uninitialized, causing the NPE.
So lombok cannot help you in your case, unfortunately. You have to write your constructor manually.

Issue with #WithUserDetails and spring boot 1.4 TestEntityManager

I have an issue with Spring Boot's TestEntityManager and #WithUserDetails annotation.
Here is my test suite:
public class AdvertisementAuthorizationTest extends AbstractIntegrationTest {
private static final String IMPERSONATOR_EMAIL = "joe.hacker#gmail.com";
private final static String OWNER_OF_ADVERTISEMENT_EMAIL = "john.nice#gmail.com";
#Autowired
private TestEntityManager testEntityManager;
#Autowired
private MockMvc mockMvc;
private Advertisement advertisement;
private UserAccount impersonator;
private ObjectMapper mapper = new ObjectMapper();
#Before
public void setUp() {
advertisement = testEntityManager.persist(createAdvertisement(OWNER_OF_ADVERTISEMENT_EMAIL));
impersonator = testEntityManager.persist(createUserAccount(IMPERSONATOR_EMAIL));
}
#Test
#WithUserDetails(IMPERSONATOR_EMAIL)
public void shouldNotAllowAdvertisementModification() throws Exception {
String jsonAdvertisement = mapper.writeValueAsString(advertisement);
mockMvc.perform(put("/api/advertisement/{id}", advertisement.getId())//
.contentType(MediaType.APPLICATION_JSON)//
.content(jsonAdvertisement))//
.andDo(print())//
.andExpect(status().isForbidden());//
}
#Test
#WithUserDetails(OWNER_OF_ADVERTISEMENT_EMAIL)
public void shouldAllowAdvertisementModification() throws Exception {
String jsonAdvertisement = mapper.writeValueAsString(advertisement);
mockMvc.perform(put("/api/advertisement/{id}", advertisement.getId())//
.contentType(MediaType.APPLICATION_JSON)//
.content(jsonAdvertisement))//
.andDo(print())//
.andExpect(status().isOk());//
}
}
Here is the super class:
#AutoConfigureMockMvc
#AutoConfigureTestEntityManager
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(classes = {TestApplication.class})
#WebAppConfiguration
#ActiveProfiles(Profiles.TEST)
#Transactional
public abstract class AbstractIntegrationTest {
}
When I run the tests it seems the entities are not persisted to H2 by TestEntityManager as indicated by this error message:
Hibernate: select useraccoun0_.id as id1_9_, useraccoun0_.address_id as address13_9_, useraccoun0_.email as email2_9_, useraccoun0_.email_notification as email_no3_9_, useraccoun0_.enabled as enabled4_9_, useraccoun0_.first_name as first_na5_9_, useraccoun0_.last_connection_date as last_con6_9_, useraccoun0_.password as password7_9_, useraccoun0_.registration_date as registra8_9_, useraccoun0_.role as role9_9_, useraccoun0_.token as token10_9_, useraccoun0_.user_account_type as user_ac11_9_, useraccoun0_.version as version12_9_ from user_account useraccoun0_ where useraccoun0_.email=?
22:52:39.943 [Test worker] WARN o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener#43ec4dcd] to process 'before' execution of test method [public void com.bignibou.it.web.security.advertisement.AdvertisementAuthorizationTest.shouldNotAllowAdvertisementModification() throws java.lang.Exception] for test instance [com.bignibou.it.web.security.advertisement.AdvertisementAuthorizationTest#646496bc]
java.lang.IllegalStateException: Unable to create SecurityContext using #org.springframework.security.test.context.support.WithUserDetails(value=joe.hacker#gmail.com, userDetailsServiceBeanName=)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:79)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.beforeTestMethod(WithSecurityContextTestExecutionListener.java:56)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:269)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:112)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:56)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:364)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.security.core.userdetails.UsernameNotFoundException: Username: joe.hacker#gmail.com not found!
at com.bignibou.service.security.UserAccountUserDetailsService.loadUserByUsername(UserAccountUserDetailsService.java:21)
at org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.createSecurityContext(WithUserDetailsSecurityContextFactory.java:56)
at org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.createSecurityContext(WithUserDetailsSecurityContextFactory.java:39)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:76)
... 43 common frames omitted
Can someone please help?
This is an issue with timing with regard to TestExecutionListener callbacks and #Before test methods.
#WithUserDetails is supported by the Spring Security's WithSecurityContextTestExecutionListener which will never run after a #Before method. It is therefore impossible for Spring Security to see the user that you persist to the database in your setUp() method. That's basically what the exception is telling you: Spring Security attempted to read the user from the database before it existed.
One way to fix this is to migrate to #Sql support for inserting test data in the database. You might not find that as comfortable as simply persisting your entities, but the #Sql approach allows the test data to be created within the test-managed transaction (i.e., does not require manual clean up). Note that you will have to upgrade to Spring Security 4.1.1 in order for this to work properly.
An alternative way to address this is to persist your entities in a user-managed transaction in a #BeforeTransaction method -- for example, using Spring's TransactionTemplate. However, you will then need to manually clean up the database in an #AfterTransaction method in a similar fashion. Plus, you will still need to upgrade to Spring Security 4.1.1 in order for this to work.
Something like the following should do the trick:
#Autowired
private TestEntityManager testEntityManager;
#Autowired
PlatformTransactionManager transactionManager;
#BeforeTransaction
public void setUp() {
new TransactionTemplate(transactionManager).execute(status -> {
UserAccount owner = testEntityManager.persist(createUserAccount(OWNER_OF_ADVERTISEMENT_EMAIL));
Language language = testEntityManager.persist(createLanguage("Français"));
DayToTimeSlot dayToTimeSlot = testEntityManager.persist(createDayToTimeSlot());
advertisement = testEntityManager.persist(createAdvertisement(owner, language, dayToTimeSlot));
impersonator = testEntityManager.persist(createUserAccount(IMPERSONATOR_EMAIL));
return null;
});
}
#AfterTransaction
public void tearDown() {
new TransactionTemplate(transactionManager).execute(status -> {
testEntityManager.remove(testEntityManager.find(Advertisement.class, advertisement.getId()));
UserAccount owner = advertisement.getUserAccount();
testEntityManager.remove(testEntityManager.find(UserAccount.class, owner.getId()));
testEntityManager.remove(testEntityManager.find(UserAccount.class, impersonator.getId()));
return null;
});
}
Regards,
Sam

Categories