How do I reset #Repository after every test in SpringBootTest? - spring-boot

I'm trying to put together integration test for my newly created #Repository class, however not having any luck with running all tests together. If I run each test separately - they pass, however if I run the whole test class - two tests fail that attempt to find one row by id in H2 (separate db for testing) and find none.
This is my test class below:
package com.vaidas.development.gradecalculatorbackend.repositories;
import com.vaidas.development.gradecalculatorbackend.models.Module;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.assertEquals;
#RunWith(SpringRunner.class)
#SpringBootTest
#DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class ModuleRepositoryTest {
#Autowired
private ModuleRepository moduleRepository;
#Test
#Transactional
public void shouldInsertAndFindAll() {
// GIVEN
Module module = new Module("Test module", null);
int count = moduleRepository.findAll().size();
// WHEN
moduleRepository.insertModule(module);
int countAfter = moduleRepository.findAll().size();
// THEN
assertEquals(count,countAfter - 1);
}
#Test
#Transactional
public void shouldInsertAndFindOne() {
// GIVEN
Module module = new Module("Test module", null);
module = moduleRepository.insertModule(module);
// WHEN
Module storedModule = moduleRepository.findOne(module.getId());
// THEN
assertEquals(storedModule.toString(), module.toString());
}
#Test
#Transactional
public void shouldUpdate() {
// GIVEN
Module module = new Module("Test module", null);
module = moduleRepository.insertModule(module);
Module updatedModule = new Module("Test module updated", null);
updatedModule.setId(module.getId());
// WHEN
moduleRepository.updateModule(updatedModule);
// THEN
Module foundModule = moduleRepository.findOne(updatedModule.getId());
assertEquals(foundModule.getName(), updatedModule.getName());
}
#Test
#Transactional
public void shouldDelete() {
// GIVEN
Module module = new Module("Test module", null);
module = moduleRepository.insertModule(module);
// WHEN
moduleRepository.deleteModule(module);
// THEN
assertEquals(0, moduleRepository.findAll().size());
}
}
What I've tried:
using #DirtiesContext and #Transactional annotations which I expected to reset the DB content
using #Before and #After annotations for every test, however it seemed like they acted asynchronously and didn't wait for DB to finish adding/removing instances
The failing tests are:
shouldInsertAndFindOne() {
shouldUpdate() {
Each of the above throw the following error:
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
Can someone explain why this would happen and what's the correct way to reset my H2 testing db before each test?

I don't know if this is the correct approach, but I usually just add a repositoryName.deleteAll() in a #Before-annotated method.
#Before
public void before() {
moduleRepository.deleteAll();
moduleRepository.flush();
}
This method will be ran before each of your #Test-annotated methods and will ensure that the moduleRepository is empty.
Also, that #Transactional annotation might be the source of your problem. Have you tried the #Before approach without that annotation? Or how about adding a #Transactional at the class level?

Related

Micronaut-Data JDBC - Multiple Dialects for Test and Production

The Mirconaut docs on JDBC repositories clearly tells us we have to create a test repository to test against another dialect. I think this will be manageable (e.g. Postgres for production and H2 for test).
The problem is I have to repeat my methods (e.g. find()) in the test repository. I have a book repository and a test repository:
#JdbcRepository(dialect = Dialect.POSTGRES)
interface BookRepository extends CrudRepository<Book, Long> {
Optional<Book> find(String title);
}
#JdbcRepository(dialect = Dialect.H2)
#Replaces(bean = BookRepository)
#Requires(env = ["test"])
interface TestBookRepository extends BookRepository {
// Optional<Book> find(String title);
// Required to make the find() method appear in the TestBookRepository
}
To make the find() method available in the TestBookRepository, I had to repeat the method (see commented line above).
Is there a better way to avoid repeating myself? The methods from the CrudRepository interface are available in the TestBookRepository without problems. Why is the find() method not treated the same?
BTW, I don't want to mock the test repository. I want to test the repository 'logic' injected by Micronaut-Data against an SQL database.
This is for Micronaut Data 1.0.0.M5, using Groovy for the source.
To make the find() method available in the TestBookRepository, I had
to repeat the method (see commented line above).
I cannot reproduce that behavior. In order for that to be the case I think the java compiler would need to have a bug in it that caused that.
See the project at https://github.com/jeffbrown/mikehoustonrepository.
https://github.com/jeffbrown/mikehoustonrepository/blob/82b8af568042c762a86cef9965e52fdc61053421/src/main/java/mikehoustonrepository/BookRepository.java
// src/main/java/mikehoustonrepository/BookRepository.java
package mikehoustonrepository;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;
import java.util.Optional;
#JdbcRepository(dialect = Dialect.POSTGRES)
public interface BookRepository extends CrudRepository<Book, Long> {
Optional<Book> find(String title);
}
https://github.com/jeffbrown/mikehoustonrepository/blob/82b8af568042c762a86cef9965e52fdc61053421/src/test/java/mikehoustonrepository/TestBookRepository.java
// src/test/java/mikehoustonrepository/TestBookRepository.java
package mikehoustonrepository;
import io.micronaut.context.annotation.Replaces;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
#JdbcRepository(dialect = Dialect.H2)
#Replaces(BookRepository.class)
public interface TestBookRepository extends BookRepository{}
https://github.com/jeffbrown/mikehoustonrepository/blob/82b8af568042c762a86cef9965e52fdc61053421/src/main/java/mikehoustonrepository/BookController.java
package mikehoustonrepository;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import java.util.Optional;
#Controller("/books")
public class BookController {
private final BookRepository bookRepository;
public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
#Get("/")
public Iterable<Book> index() {
return bookRepository.findAll();
}
#Post("/{title}/{author}")
public Book create(String title, String author) {
return bookRepository.save(new Book(title, author));
}
#Get("/find/{title}")
public Optional<Book> findByTitle(String title) {
return bookRepository.find(title);
}
}
https://github.com/jeffbrown/mikehoustonrepository/blob/82b8af568042c762a86cef9965e52fdc61053421/src/test/java/mikehoustonrepository/BookControllerTest.java
package mikehoustonrepository;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import javax.inject.Inject;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
#MicronautTest
public class BookControllerTest {
#Inject
BookClient bookClient;
#Test
public void testFind() throws Exception {
Optional<Book> book = bookClient.find("The Nature Of Necessity");
assertFalse(book.isPresent());
bookClient.create("The Nature Of Necessity", "Alvin Plantinga");
book = bookClient.find("The Nature Of Necessity");
assertTrue(book.isPresent());
}
}
#Client(value="/", path = "/books")
interface BookClient {
#Post("/{title}/{author}")
Book create(String title, String author);
#Get("/")
List<Book> list();
#Get("/find/{title}")
Optional<Book> find(String title);
}
That test passes.
You can see that a different repository is being used for test (TestBookRepository) that is used for other environments (BookRepository).
I hope that helps.
You can utilise Micronaut environments to create different environment configuration for test and production
and configure respective datasource configuration in application-test.yml
and use that datasource for tests
Micronaut Environments from docs
After some more work, I found another way to solve the original problem. You can define a base interface class that just has the methods you need. Then implement concrete classes for the dialect(s) you need. This allows one type of DB for test and one for production.
interface OrderRepository extends BaseRepository, CrudRepository<Order, UUID> {
#Join(value = "product", type = Join.Type.LEFT_FETCH)
Optional<Order> findById(UUID uuid)
}
#JdbcRepository(dialect = Dialect.H2)
#Requires(env = ["test"])
interface OrderRepositoryH2 extends OrderRepository, CrudRepository<Order, UUID> {
}
#JdbcRepository(dialect = Dialect.POSTGRES)
#Requires(env = ["dev"])
interface OrderRepositoryPostgres extends OrderRepository, CrudRepository<Order, UUID> {
}
No methods are needed in the OrderRepositoryH2 interface. Micronaut-data uses the methods from the parent interface fine. The trick is to not use the #JdbcRepository annotation in the parent interface.
You can create any other dialects needed, but you have to make sure the #Requires annotation results in only one bean for any given mode.
I plan to use H2 for testing, with an option to use the Postgres dialect for special test runs when needed.
Sorry for any confusion on the question and comments.
(I decided to mark this as the answer since it solves the original problem).

Spring + QueryDsl + Mockito: How to write a unit test case for a simple function

I have the following function in my service.
public boolean checkNameUnique(String name) {
QEntity qEntity = QEntity.entity;
BooleanExpression nameUniquePredicate = qEntity.name.eq(name);
long count = entityReadRepository.count(nameUniquePredicate);
return count == 0;
}
It just checks if the name already exists in db. That needs to be unique, so it returns true if does not already exist and false if it does.
Now how do I write a mockito unit test case for this? I am new to Mockito and writing unit test cases, hence the question.
My reading on Mockito has lead me to write something on the lines of
when(entityReadRepository.count(nameUniquePredicate)).thenReturn(1);
and then call the function to be tested. But that doesn't make any sense.
Entity is Hibernate entity which corresponds to a table in the DB
entityReadRepository extends JpaRepository and QueryDslPredicateExecutor. QEntity is the Q object generated by QueryDsl's plugin.
A unit test would normally mock out any external dependencies, in your case entityReadRepository. If you want to do actual db call it would be classed as integration test.
Your method should return two different values depending on the entityReadRepository response and this is what you would stub in order to unit test it. You were on a good path trying:
when(entityReadRepository.count(any(BooleanExpression.class))).thenReturn(1l);
The problem you have is that you have a lot of static calls and objects in your method and that can't be handled gracefully. One option is to use tools like Powermockito where you can mock behaviour of static methods. If you prefer to stick with mockito you could extract static piece of code to a separate method and create a spy of your class under test:
package com.slavpilus;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
#RunWith(MockitoJUnitRunner.class)
public class TPresenterTest {
#InjectMocks
#Spy
private ClassUnderTest target = new ClassUnderTest();
#Before
public void setUp() {
doReturn(null).when(target).getUniqueNamePredicate();
}
#Mock
private YourRepositoryDependency entityReadRepository;
#Test
public void checkNameUniqueShouldBeTrueIfNameNotInDatabase() {
when(entityReadRepository.count(any())).thenReturn(0l);
boolean isUnique = target.checkNameUnique("anyName");
Assert.assertTrue(isUnique);
}
#Test
public void checkNameUniqueShouldBeFalseIfNameFoundInDatabase() {
when(entityReadRepository.count(any())).thenReturn(1l);
boolean isUnique = target.checkNameUnique("anyName");
Assert.assertFalse(isUnique);
}
}
and your production code would look something like that:
public boolean checkNameUnique(String name) {
BooleanExpression nameUniquePredicate = getUniqueNamePredicate();
long count = entityReadRepository.count(nameUniquePredicate);
return count == 0;
}
protected BooleanExpression getUniqueNamePredicate() {
QEntity qEntity = QEntity.entity;
return qEntity.name.eq(name);
}
This approach however leaves you with some code untested as getUniqueNamePredicate method is skipped entirely during the test execution.

Spring Data JPA - return Future

I tried to save my entity asynchronous and return future of the result, but I had following problems:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.Repository;
import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Repository;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
// I must extend Repository because when I extended JpaRepository an argument
// of save is ambigous when I try save(countryEntity) - compiler shows me
// save (CountryEntity) in AsyncCountryRepository and save (S) in CrudRepository
public interface AsyncCountryRepository extends Repository<CountryEntity, Long> {
// I don't know why I cannot return Future<CountryEntity>
#Async
<S extends CountryEntity> Future<S> save(CountryEntity countryEntity);
// I cannot test it - test is below
#Async
CompletableFuture<CountryEntity> findByCode(String code);
}
#RunWith(SpringRunner.class)
#DataJpaTest
public class CountryRepositoryTest {
#Autowired
private TestEntityManager entityManager;
#Autowired
private AsyncCountryRepository countryRepository;
#Test
public void findByCodeTest() throws Exception {
// given
final CountryEntity countryEntity = new CountryEntity("EN");
final CountryEntity persisted = entityManager.persist(countryEntity);
entityManager.flush();
// when
final Future<CountryEntity> byCode = countryRepository.findByCode(persisted.getCode());
// then
assertThat(byCode.get())
.isEqualTo(persisted);
}
Test returns Failed and I get expected:<CountryEntity(id=1, code=EN)> but was:<null>
How can I implement my repository as asynchronous repository and how to test it?
Which approach has better performance:
1. return Futures from repository
2. run repository methods as Callable in ExecutorService
?
If you use #Async the repository code runs in a separate thread within it's own transaction. Since your original transaction isn't committed, it won't see the changes (i.e. the inserted entity) hence the null result.
So in order to make it work, you need to commit the first transaction.
Regarding the second question about the performance (Please make separate questions next time): Performance of multithreaded code depends on so many things that really the only useful way to answer it: try it. But I would be surprised if the way how you schedule the execution has more effect on the performance than things like thread pool and connection pool size.

How to write annotation processor to raise a warning message if a java source is calling an annotated method

Here is my requirement in Java 6: I am using Eclipse JUNO.
Annotate a method with a custom annotation.
During compilation, raise warning message if a method is calling the
annotated method.
I am looking for something like #Deprecated annotation.
This is what I have done:
Wrote a custom annotation.
Wrote an annotation processor to read and process the methods with
the annotation.
Created a jar and added it in annotation processor path. My sample code (see below) raises the warning message in the annotated method. But it is not my requirement.
What I couldn’t do:
I could not get the calling methods. I want to raise the warning
message in those calling methods.
My sample code:
Custom annotation:
package tool.apichecks;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Retention(RetentionPolicy.SOURCE)
#Target({ ElementType.METHOD })
public #interface HighCostMethod {
String altMethod();
}
Annotation Processor:
package tool.apichecks;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
#SupportedAnnotationTypes({ "tool.apichecks.HighCostMethod" })
public class MethodProcessor extends AbstractProcessor {
private enum MethodType {
HIGH_COST(HighCostMethod.class.getName());
private String name;
private MethodType(String name) {
this.name = name;
}
private static MethodType getMethodType(String name) {
MethodType methodType = null;
for (MethodType methodType2 : MethodType.values()) {
if (methodType2.name.equals(name)) {
methodType = methodType2;
break;
}
}
return methodType;
}
}
private ProcessingEnvironment processingEnvironment;
#Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
this.processingEnvironment = processingEnvironment;
}
#Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnvironment) {
if (!roundEnvironment.processingOver()) {
for (TypeElement annotation : annotations) {
final Set<? extends Element> elements = roundEnvironment
.getElementsAnnotatedWith(annotation);
MethodType methodType = MethodType.getMethodType(annotation
.toString());
for (Element element : elements) {
switch (methodType) {
case HIGH_COST: {
processHighCostMethod(element);
break;
}
}
}
}
}
return true;
}
protected void processHighCostMethod(Element element) {
HighCostMethod highCostMethod = element
.getAnnotation(HighCostMethod.class);
/* TODO This warns the annotated method itself. I don't want this. I want to warn the methods that calls this method */
processingEnvironment
.getMessager()
.printMessage(
Kind.WARNING,
String.format(
"Do not use high cost method %s. Instead use %s method.",
element, highCostMethod.altMethod()), element);
}
}
Using an AnnotationProcessor will only work on the files containing the annotations or overriding methods, but not calling methods. Maybe there's a way around this, but then you will probably be limited by projects, because the processor only looks at one project at a time.
I guess you need to write an Eclipse plugin with a builder, that analyses code in all files and checks called methods for annotations.
That a lot more work than an annotation processor, but you also have more options. E.g. you could implement a quick fix for the error markers.

JUnit test: null pointer exception

I am using Spring with EXT JS and hibernate to populate a EXT JS form on a webpage using tomcat... I have the table populating using a method that accesses the database and returns my HQL statement
Now i am trying to execute a simple JUnit test to count the number of records returned but when i am calling the method in the JUint test that populates the EXT JS form, it returns this excception... I dont know why
JUnit Test class
package com.fexco.helloworld.web;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.fexco.helloworld.web.dao.CustomerDaoImpl;
import com.fexco.helloworld.web.model.Customer;
/**
* Unit test for simple App.
* /
public class CustomerServiceTest {
#Autowired
private CustomerDaoImpl customerDaoImpl;
#Test
public void findAllCustomersTest() {
List<Customer> list = customerDaoImpl.findAllCustomers();
int numberInDatabase = list.size();
assertEquals(5, numberInDatabase);
}
}
my method accessing the database
public List<Customer> findAllCustomers(){
List<Customer> list = getHibernateTemplate().find("from Customer");
return list;
}
and my method calling the method accessing the database
public List<Customer> returnAllCustomers(){
List<Customer> list = customerDaoImpl.findAllCustomers();
return list;
}
you can see here that the form is populated with items from the database using the same method as in junit(findAllCustomers())
Does anybody have any ideas?
Add following line before class CustomerServiceTest
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "file:src/main/resources/app-context.xml" })
public class CustomerServiceTest {....
Where src/main/resources/app-context.xml - is path to your application context. Its good, to create separate context for tests.
EDIT
You also need to have spring-test.jar in the classpath.
Dependency for maven:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
You did not load your Spring context in your unit test.
you can take a look at the Spring documentation here : http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/testing.html
And an example : Spring: JUnit-Test: applicationContext is not loaded

Resources