How to do Spring Boot unit test insert data into Database?

Asked

Viewed 2,884 times

0

I created a simple system that is working perfectly, but I need to implement a unit test in Spring Boot, which happens that when I perform the test or I cannot enter the data in the database or generates error;

I tried this way, it did not generate error, but also failed to save in the bank;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.projeto.subium.api.model.Project;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SubiumApplicationTests {



    @Test
    public void testAdicionarEmployee() throws Exception {
        Project project1 = new Project();
        String nome1 = new String("Google");
        project1.setName(nome1);


    }
}

Then I tried this way, but it got an error, I updated the code;

   import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.projeto.subium.api.service.ProjectService;
import com.projeto.subium.api.model.Project;

@RunWith(SpringRunner.class)
@DataJpaTest
public class SubiumApplicationTests {

    @Autowired
    private ProjectService projectService;

    @Test
    public void testAdicionarEmployee(){
        Project project1 = new Project("Google");
        this.projectService.salvar(project1);
    }
}

What I need is to generate the test in such a way that it can save the data in the database.

This is the error message in the IDE;

2018-09-19 11:05:49.602 ERROR 1564 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@1a38c59b] to prepare test instance [com.projeto.subium.api.SubiumApplicationTests@36068727]

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.projeto.subium.api.SubiumApplicationTests': Unsatisfied dependency expressed through field 'projectService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.projeto.subium.api.service.ProjectService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1269) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:384) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) ~[spring-boot-test-autoconfigure-1.5.16.RELEASE.jar:1.5.16.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) ~[spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) [.cp/:na]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.projeto.subium.api.service.ProjectService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1491) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    ... 29 common frames omitted

2018-09-19 11:05:49.627  INFO 1564 --- [       Thread-3] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@4229bb3f: startup date [Wed Sep 19 11:05:45 GFT 2018]; root of context hierarchy
2018-09-19 11:05:49.646  INFO 1564 --- [       Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2018-09-19 11:05:49.647  INFO 1564 --- [       Thread-3] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: drop table employee if exists
Hibernate: drop table employeeproject if exists
Hibernate: drop table project if exists
2018-09-19 11:05:49.704  INFO 1564 --- [       Thread-3] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
  • Your first way didn’t save in the bank because you just arrow the name, don’t call any rescue method. In the second form, you call, but you need to know the error that gives. Could edit your question and include the stacktrace of this error?

  • @Statelessdev I reedited the question and put the error picture, could I have a look please.

  • i did an update on the entire post, so I would like you to read from the top, please.

  • This test will not work, since there is a container dependency running to perform transactions to the database, if it runs you cannot use the service methods as well, because of the injection. I would advise you to create a unit test so that you test, only the save() method, things like business rules for example, save itself, you can create a mock to return something by simulating a transaction with the database.

1 answer

1

I used @Springboottest instead of @Datajpatest and it worked ! Maybe that’s the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.