Spring Boot 1.4 - how to test a controller with the validation - java

Spring Boot 1.4 has a number of fine features including #DataJpaTest annotation that automatically wakes up the classpath embedded database for the test purposes. As far I know, it won't work in conjuction with TestRestTemplate in bounds of the same class.
The following test won't work:
#RunWith(SpringRunner.class)
#SpringBootTest
#DataJpaTest
public class PersonControllerTest {
private Logger log = Logger.getLogger(getClass());
private Category category;
#Autowired
private TestRestTemplate restTemplate;
#Autowired
private TestEntityManager entityManager;
#Before
public void init() {
log.info("Initializing...");
category = entityManager.persist(new Category("Staff"));
}
#Test
public void personAddTest() throws Exception {
log.info("PersonAdd test starting...");
PersonRequest request = new PersonRequest("Jimmy");
ResponseEntity<String> response = restTemplate.postForEntity("/Person/Add", request, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
log.info("PersonAdd test passed");
}
During startup of the test an exception will be thrown:
Unsatisfied dependency expressed through field 'restTemplate':
No qualifying bean of type [org.springframework.boot.test.web.client.TestRestTemplate]
Then guessing to switch to the recommended mock based slice approach but it won't work there because the controller looks like this:
#RequestMapping(value="/Person/Add", method=RequestMethod.POST)
public ResponseEntity personAdd(#Valid #RequestBody PersonRequest personRequest,
Errors errors)
personValidator.validate(personRequest, errors):
if (errors.hasErrors())
return new ResponseEntity(HttpStatus.BAD_REQUEST);
personService.add(personRequest);
return new ResponseEntity(HttpStatus.OK);
}
... it's easy to mock the personService as the documentation suggests but how to be with the errors object which is not mockable in this case? As far I know, there's no ways to mock it since it isn't class field or a returned value of a method.
So, I'm unable to test the code above using neither slice approach nor integration one since #DataJpaTest should not be used with a controller.
Is there a way to test the controller with such architecture using Spring Boot 1.4 testing features?

Your understanding of the #DataJpaTest is a little off. From the documentation "Can be used when a test focuses only on JPA components". If you are wanting to test your controller layer you don't want to use this annotation as none of the WebMvc components get loaded into the application context. You instead want to use the #WebMvcTest and have it use the #Controller that you are testing.
#RunWith(SpringRunner.class)
#WebMvcTest(PersonController.class)
public class PersonControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
PersonValidator personValidator;
#MockBean
PersonService personService;
#Test
public void personAddTest() throws Exception {
String content = "{\"name\": \"Jimmy\"}";
mockMvc.perform(post("/Person/Add").contentType(MediaType.APPLICATION_JSON).characterEncoding("UTF-8")
.accept(MediaType.APPLICATION_JSON).content(content)).andExpect(status().isOk());
}
#Test
public void personAddInvalidTest() throws Exception {
String content = "{\"noname\": \"Jimmy\"}";
mockMvc.perform(post("/Person/Add").contentType(MediaType.APPLICATION_JSON).characterEncoding("UTF-8")
.accept(MediaType.APPLICATION_JSON).content(content)).andExpect(status().isBadRequest());
}
}
Not sure how you wired the validator and service so just assumed you autowired them.
#Controller
public class PersonController {
private PersonValidator personValidator;
private PersonService personService;
public PersonController(PersonValidator personValidator, PersonService personService) {
this.personValidator = personValidator;
this.personService = personService;
}
#RequestMapping(value = "/Person/Add", method = RequestMethod.POST)
public ResponseEntity<String> personAdd(#Valid #RequestBody PersonRequest personRequest, Errors errors) {
personValidator.validate(personRequest, errors);
if (errors.hasErrors()) {
return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
}
personService.add(personRequest);
return new ResponseEntity<String>(HttpStatus.OK);
}
}
Sample PersonRequest as I didn't know what else was in there. Note the one validation on the name as being #NotNull as I wanted a way to show how to use the Errors object.
public class PersonRequest {
#NotNull
private String name;
public PersonRequest() {
}
public PersonRequest(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Related

Java/Spring-boot/Mockito #MockBean annotation work strange

In tests i mock DateService to have the same date every time when i run the test, but when i use DateServie in other service then the mock retun null all the time. It is strange because the mock works in my custom date time provder. Here is the code:
Its work here:
#Service(MyDateTimeProvider.MY_DATE_TIME_PROVIDER)
public class MyDateTimeProvider implements DateTimeProvider {
public static final String MY_DATE_TIME_PROVIDER = "MyDateTimeProvider";
#Autowired
private DateService dateService;
#Override
public Optional<TemporalAccessor> getNow() {
return Optional.of(dateService.getCurrentDate().toInstant());
}
}
#Service
public class DateService {
public Date getCurrentDate() {
return new Date();
}
}
Its not work in the UserService:
#SpringBootTest
public class Test{
#MockBean
protected DateService dateService;
#BeforeEach
public void beforeEach() { Mockito.when(dateService.getCurrentDate()).thenReturn(DEFAULT_DATE_TIME.toDate());
}
...
}
#Service
public class UserService {
#Autowired
private UserRepository userRepository;
#Autowired
private DateService dateService;
private User createNewUser(final UserDto dto) {
User user = new User();
user.setEmail(dto.getEmail());
user.setRegistrationDate(dateService.getCurrentDate()); // i got null here
return userRepository.save(user);
}
}
What did i wrong? Thank you!
My colleague helped me. My problem was: i used "UserService" in a method with #PostConstuct annotation, so its run before the mock happened.

Mocked service function not working as expected

Here is my code. I can't see why it is not working. The problem is with the line in test :
when(applicationUserRepository.findApplicationUserByUsername("testUser"))
.thenReturn(userToReturnFromRepository);
doesn't seem to be doing anything. The function findApplicationUserByUsername returns an empty optional when it should be returning an optional of userToReturnFromRepository.
Controller:
#RestController
#RequestMapping("api/v1/exercises")
public class ExerciseController {
#Autowired
ExerciseService exerciseService;
#GetMapping
public List<Exercise> getExercises() {
List<Exercise> exercises = exerciseService.getAllExercises();
return exercises;
}
}
Service:
#Service("exerciseService")
public class ExerciseService {
#Autowired
ExerciseRepository exerciseRepository;
#Autowired
ApplicationUserRepository applicationUserRepository;
#Transactional
public List<Exercise> getAllExercises() {
Principal principal = SecurityContextHolder.getContext().getAuthentication();
Optional<ApplicationUser> applicationUser = applicationUserRepository.findApplicationUserByUsername(principal.getName());
List<Exercise> exercises = new ArrayList<>();
if(applicationUser.isPresent()){
exercises = applicationUser.get().getExercises();
}
return exercises;
}
}
The test:
#SpringBootTest
#AutoConfigureMockMvc(addFilters = false)
public class ExerciseControllerTest {
private final MockMvc mockMvc;
private final ObjectMapper objectMapper;
#Mock
ApplicationUserRepository applicationUserRepository;
#Autowired
public ExerciseControllerTest(MockMvc mockMvc,
ApplicationUserRepository applicationUserRepository, ObjectMapper objectMapper) {
this.mockMvc = mockMvc;
this.applicationUserRepository = applicationUserRepository;
this.objectMapper = objectMapper;
}
#BeforeEach
public void initMocks() {
MockitoAnnotations.openMocks(this);
}
#Test
#WithMockUser(username = "testUser")
public void testGetExercises() throws Exception {
Exercise ex = new Exercise();
ex.setData("test");
ApplicationUser user = new ApplicationUser();
Exercise[] exercises = {ex};
List<Exercise> list = new ArrayList<Exercise>(Arrays.asList(exercises));
user.setExercises(list);
Optional<ApplicationUser> userToReturnFromRepository = Optional.of(user);
when(applicationUserRepository.findApplicationUserByUsername("testUser"))
.thenReturn(userToReturnFromRepository);
mockMvc.perform(get("/api/v1/exercises")).andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(1)));
}
}
There are two conflicting things happening in your test:
You are using Mockito to init a mock implementation via reflection
You have ApplicationUserRepository being Spring injected into the Test class via the constructor.
What ends up happening is this:
spring injects applicationUserRepository into the constructor param
The applicationUserRepository field is set to the spring injected version in the constructor
Mockito inits a new applicationUserRepository mock
Mockito replaces the applicationUserRespository field with the mock (i.e. goodbye to your handle on the spring bean that your MVC setup is using!)
The easiest way I can think of to fix it is to use #MockBean instead of the #Mock combined with Constructor injection. #MockBean will instruct Spring to create the mock instance for you, use it, and provide it to you in the test.
#SpringBootTest
#AutoConfigureMockMvc(addFilters = false)
public class ExerciseControllerTest {
private final MockMvc mockMvc;
private final ObjectMapper objectMapper;
#MockBean // User MockBean instead of Mock
ApplicationUserRepository applicationUserRepository;
#Autowired
public ExerciseControllerTest(MockMvc mockMvc, ObjectMapper objectMapper) {
this.mockMvc = mockMvc;
//remove the applicationUserRepository injection
this.objectMapper = objectMapper;
}
// remove MockitoAnnotations.openMocks(this);
//...
...
}

Spring boot 2.1 test fails because of null property

I'm not an expert in Spring Boot. I have to write tests for my #RestController methods but I have a problem, which is, the #AutoWired ConfigurationProperties class is null when the test class executes the main controller. I found many posts about a similar issue here but they really don't solve this problem. The strange thing is that in the #PostConstruct method of the #RestController the property class is not null, it is null only in the #RequestMapping methods I'm trying to test.
This is my #SpringBootApplication class:
#SpringBootApplication
#ComponentScan
#EnableConfigurationProperties({MysqlProperties.class, CassandraProperties.class, GenericsProperties.class})
#EnableAutoConfiguration
public class REST {
public static void main(String[] args) {
SpringApplication.run(REST.class, args);
}
}
This is the #RestController:
#RestController
public class MainController {
#Autowired
private MysqlProperties mysqlProperties;
#PostConstruct
public void init() throws Exception {
//Here mysqlProperties is not null and I can get elements from it
}
#RequestMapping(value = "/online", method = RequestMethod.GET)
public #ResponseBody
String online(#RequestHeader(value = "email", required = true) String email, #RequestHeader(value = "password", required = true) String password) {
Utils.logInfo(logger, "/online endpoint");
//Here mysqlProperties is null
String sql = "SELECT * FROM " + mysqlProperties.getAddress() + " WHERE email= ?";
return new Return(Return.ERROR_MESSAGE, "Access denied, not superuser").toString();
}
This is the #ConfigurationProperties class:
#Configuration
#PropertySource("classpath:application.properties")
#ConfigurationProperties(prefix = "mysql")
public class MysqlProperties {
String address;
String database;
...
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
}
This is the test:
#RunWith(SpringRunner.class)
#SpringBootTest(classes = {REST.class})
#EnableConfigurationProperties({CassandraProperties.class, GenericsProperties.class, MysqlProperties.class})
#AutoConfigureMockMvc
public class MainControllerTest {
#Autowired
private MockMvc mvc;
#Autowired
private GenericsProperties genericsProperties;
#Before
public void init() {
try {
//mc.init();
mvc = MockMvcBuilders.standaloneSetup(new MainController()).build();
} catch (Exception ex) {
ex.printStackTrace();
Logger.getLogger(MainControllerTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Test
public void testOnline() throws Exception {
//Return returnObject = new Return(Return.DONE_MESSAGE, "System online");
Return returnObject = new Return(Return.ERROR_MESSAGE, "Access denied, not superuser");
this.mvc.perform(get("/online")
.header("email", genericsProperties.getSuperuser_email())
.header("password", genericsProperties.getSuperuser_password()))
//.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(returnObject.toString()));
}
}
This is the package structure:
main
--java
----configurations
------MysqlProperties.java
----main
------MainController.java
----...
--resources
----application.properties
test
--java
----main
------MainControllerTest.java
The NullPointerException occurs in the MainController class at:
mysqlProperties.getAddress()
Any clue why it is not working? Thank you.
#Autowired MysqlProperties mysqlProperties is null bacause you created a new instance of MainController here:
mvc = MockMvcBuilders.standaloneSetup(new MainController()).build();
That instance will not be registered in the Spring context, therefore it will not be available for dependency injection.
You could read more about this issue here
You should use an autowired MainController in your MainControllerTest class.
#Autowired
private MainController mainController;

MockMvc and losing #SessionScope bean value

I have a #SessionScope bean that keeps track of the current users role. When I run the application the value is present, however when I run my integration tests the bean is null.
Here's what I have:
#Component
#SessionScope
public UserSessionDataImpl implements UserSessionData {
private String role; // "Admin" or "User"
// getters/setters below
}
// Service
#Service("roleService")
public RoleServiceImpl implements RoleService {
#Autowired
UserSessionData sessionData;
public String getRole(){
return this.sessionData.getRole();
}
public String setRole(String role){
return this.sessionData.setRole(role);
}
}
// API
#Api
public class TicketApi {
#Autowired
private RoleService roleService;
#Autowired
private TicketService TicketService;
#RequestMapping(value = "person/{id}/tickets", method = RequestMethod.GET)
public String getTickets(long personId) {
// only admins can lookup tickets
if(roleService.getRoles.equals("Admin"){
// do logic
}
}
}
// Unit test method
#Before
public void setup(){
roleService.setRole("Admin"); //set role to admin for testing
}
#Test
// Calls TicketApi
public void getTicketsTest(){
mockMvc.perform(
get("/person/{id}/tickets")); // blows up due to null role
}
I am stumped as to why my roleSerivce loses the reference to sessionData. I do see that UserSessionDataImpl does get instantiated multiple times, which I wouldn't think would happen. I'm wondering if the mockMvc call creates a new Session which would cause the extra instantiations. Has anyone else figured this issue out?

How to mock any services inside a #MockedBean?

Is it possible to just ignore/mock any injected dependencies inside a MockedBean?
Example:
#Service
public class MyService {
#Autowired
private MailerService mailer;
public void test1() {
//does not use mailer
}
public void test2() {
//...
mailer.send();
}
}
#Service
public class MailerService {
//I want these to be automatically mocked without explicit declaration
#Autowired
private JavaMailSender sender;
#Autowired
private SomeMoreService more;
//also these should be mocked without having to provide properties
#Value("${host}") private String host;
#Value("${user}") private String user;
#Value("${pass}") private String pass;
}
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest
public class MyServiceTest {
#Autowird
private MyService myservice;
#MockBean
private MailserService mailer;
#Test
public void test1() {
myservice.test1();
}
}
I could use #MockBean to sort out mailer injection dependency. But any service inside the mocked bean would also have to be explicitly mocked.
Question: is it possible to mock a service "away". Means, just mock the bean and don't care what's inside the #MockedBean (or automatically also mock anything inside #MockedBean)?
As for me the best way to inject mocks is to use MockitoJUnitRunner
#RunWith(MockitoJUnitRunner.class)
public class MocksTests {
#InjectMocks
private ParentService parent;
#Mock
private InnerService inner; // this will be injected into parent
//your tests
}

Categories