0

I have a test class that contains two method tests. These test methods are executed with a certain order using @Order annotaion which works well. The first method inserts a known value in h2 that will have id = 21 (trust me indeed, on startup 20 values are created by me when spring starts up, in the spring application configuration class annotated @SpringBootApplication), here is the inserted value in test layer :

User userStub = new User("prenom21", "Nom21"))

  • The second method has to read the inserted value but i get a null object. Maybe i did something wrong, but i would like to know what.
  • The script is available here for viewing or download (you can run it to see the error) : https://github.com/userInterview/LoizSpringRestPersistenceTests/blob/main/src/test/java/org/loiz/demo/LoizPersistenceTest.java

  • Below, At code line written in bold case (line 65), i should have an object in UserRead object created by the first test method but i have null object. Is this normal ?

    @Test
         @Order(2) 
         @DisplayName("Test de suppression du user \"prenom21 Nom21\"")
         public void readShouldMapCorrectly() throws Exception {
             User userStub = new User(idStub, "prenom21", "Nom21");          
             User UserRead  = this.testEntityManager.find(User.class, idStub) ;
             Assert.assertTrue(userStub.equals(UserRead));
         }
  • It should not be null because the first method inserts/creates that record in h2. See Below :

    package org.loiz.demo;
    
    import org.assertj.core.api.BDDAssertions;
    import org.junit.Assert;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.MethodOrderer;
    import org.junit.jupiter.api.Test ;
    import org.junit.jupiter.api.TestMethodOrder;
    import org.junit.jupiter.api.Order ;
    
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
    import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import demo.LoizBootSpringDemoApplication;
    import demo.crepository.UserRepositoryInterface;
    import demo.dmodel.User;
    
    //Test de la couche de persistence
    @RunWith(SpringRunner.class)
    @DataJpaTest
    @ContextConfiguration(classes = { LoizBootSpringDemoApplication.class})
    @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
    public class LoizPersistenceTest 
    {
        
        @Autowired
        private TestEntityManager testEntityManager;
    
        @Autowired
        private UserRepositoryInterface repository; 
    
        private static Long idStub ;
        
        @Test
        @Order(1)   
        @DisplayName("Test de sauvegarde d\'un user \"prenom21 Nom21\"")
        public void saveShouldMapCorrectly() throws Exception {
            
            User userStub = new User("prenom21", "Nom21");                       
            
            User UserSaved = this.testEntityManager.persistFlushFind(userStub);
            
            BDDAssertions.then(UserSaved.getId()).isNotNull();                    
            idStub = UserSaved.getId() ;
            
            User UserRead = this.testEntityManager.find(User.class, idStub) ;       
            
            BDDAssertions.then(UserSaved.getFirstName()).isNotBlank();
            BDDAssertions.then(UserSaved.getFirstName()).isEqualToIgnoringCase("prenom21");
            
    
            BDDAssertions.then(UserSaved.getLastName()).isEqualToIgnoringCase("Nom21");
            BDDAssertions.then(UserSaved.getLastName()).isNotBlank();
        }
    
        @Test
        @Order(2) 
        @DisplayName("Test de suppression du user \"prenom21 Nom21\"")
        public void readShouldMapCorrectly() throws Exception {
            User userStub = new User(idStub, "prenom21", "Nom21");          
            User UserRead  = this.testEntityManager.find(User.class, idStub) ;
            Assert.assertTrue(userStub.equals(UserRead));
        }
    
    }

I would appreciate some light about that problem :)

1 Answer 1

1

When running these tests, spring performs a full rollback after each test method. To your test that performs the write, add

@Rollback(false)

from org.springframework.test.annotation.Rollback;

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Lane for that quick answering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.