DB testing i java fjær MVC søknad

stemmer
0

Jeg har konfigurasjon i en prøve MVC applikasjon som kjøres av Tomcat server perfekt.

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(com.packt.webstore)
public class RootApplicationContextConfig {

    @Bean
    public DataSource mysqlDataSource() {

        DriverManagerDataSource dataSource = null;

        try {

            dataSource = new DriverManagerDataSource();

            dataSource.setDriverClassName(org.postgresql.Driver);

            dataSource.setUrl(jdbc:postgresql://localhost:5432/springmvctest);

            dataSource.setUsername(postgres);

            dataSource.setPassword(12345);

            if (dataSource.getConnection().isClosed()) throw new Exception(Соединение закрыто);

            // for test

        } catch (Throwable e) {

            final String msg = Error + :  + EOS + e;

            throw new RuntimeException(e);

        }

        return dataSource;

    } //END: mysqlDataSource()

    @Bean
    public NamedParameterJdbcTemplate getJdbcTemplate() {

        NamedParameterJdbcTemplate template = null;

        try {

            template = new NamedParameterJdbcTemplate(mysqlDataSource());

        } catch (Exception e) {

            System.err.println(Error + :  + EOS + e);

        }

        return template;

    } //END: getJdbcTemplate()

    @Component
    public class SqlUtil {

        @Autowired
        private ApplicationContext context;

        @Autowired
        private DataSource datasource;

        public void runSqlFile(String SQLFileName) {

            Resource resource = context.getResource(SQLFileName);

            EncodedResource encodedResource = new EncodedResource(resource, Charset.forName(UTF-8));

            try {

                ScriptUtils.executeSqlScript(datasource.getConnection(), encodedResource);

            } catch (SQLException ex) {

                throw new RuntimeException(Error execute sql file + :  + EOS + ex);

            }

        } //END: runSqlFile()

    } //END: class SqlUtil

} //END: class RootApplicationContextConfig

Og testen:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = RootApplicationContextConfig.class)
public class dbtest {

    @Autowired
    InMemoryProductRepository productRepository = null;

    /**
     * Test DB connecton and table creation
     *
     * @throws Exception
     */
    @Test
    void ProductTest() throws Exception {

        Assertions.assertNotEquals(null, productRepository);

        return;

    } //END: ProductTest()

} //END: class dbtest

Det feilmeldingen: java.lang.NoClassDefFoundError: org / Spring Framework / core / merknad / MergedAnnotations $ SearchStrategy

    at org.springframework.test.context.support.TestPropertySourceUtils.buildMergedTestPropertySources(TestPropertySourceUtils.java:74)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:386)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:294)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:137)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:122)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$0(ExtensionValuesStore.java:81)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:182)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:84)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:88)
    at org.junit.jupiter.engine.execution.NamespaceAwareStore.getOrComputeIfAbsent(NamespaceAwareStore.java:61)
    at org.springframework.test.context.junit.jupiter.SpringExtension.getTestContextManager(SpringExtension.java:213)
    at org.springframework.test.context.junit.jupiter.SpringExtension.beforeAll(SpringExtension.java:77)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$7(ClassBasedTestDescriptor.java:355)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:355)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:189)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:77)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:132)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Hvordan å gi en Dependency Injection med fjær + juint5? Jeg prøvde før:

  InMemoryProductRepository productRepository = null;

ApplicationContext context = new AnnotationConfigApplicationContext(RootApplicationContextConfig.class);

context.getBean(InMemoryProductRepository.class);

Assertions.assertNotEquals(null, productRepository);

Men det gjenopplivet en annen feil: java.lang.IllegalStateException: Nei ServletContext sett

For det jeg trenger ServletContext hvis jeg sipmle prøver å recive DB-leverandøren bønne i testmetoden?

Publisert på 02/12/2019 klokken 23:56
kilden bruker
På andre språk...                            


1 svar

DB testing i java fjær MVC søknad

stemmer
0

Jeg har konfigurasjon i en prøve MVC applikasjon som kjøres av Tomcat server perfekt.

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.packt.webstore")
public class RootApplicationContextConfig {

    @Bean
    public DataSource mysqlDataSource() {

        DriverManagerDataSource dataSource = null;

        try {

            dataSource = new DriverManagerDataSource();

            dataSource.setDriverClassName("org.postgresql.Driver");

            dataSource.setUrl("jdbc:postgresql://localhost:5432/springmvctest");

            dataSource.setUsername("postgres");

            dataSource.setPassword("12345");

            if (dataSource.getConnection().isClosed()) throw new Exception("Соединение закрыто");

            // for test

        } catch (Throwable e) {

            final String msg = "Error" + ": " + EOS + e;

            throw new RuntimeException(e);

        }

        return dataSource;

    } //END: mysqlDataSource()

    @Bean
    public NamedParameterJdbcTemplate getJdbcTemplate() {

        NamedParameterJdbcTemplate template = null;

        try {

            template = new NamedParameterJdbcTemplate(mysqlDataSource());

        } catch (Exception e) {

            System.err.println("Error" + ": " + EOS + e);

        }

        return template;

    } //END: getJdbcTemplate()

    @Component
    public class SqlUtil {

        @Autowired
        private ApplicationContext context;

        @Autowired
        private DataSource datasource;

        public void runSqlFile(String SQLFileName) {

            Resource resource = context.getResource(SQLFileName);

            EncodedResource encodedResource = new EncodedResource(resource, Charset.forName("UTF-8"));

            try {

                ScriptUtils.executeSqlScript(datasource.getConnection(), encodedResource);

            } catch (SQLException ex) {

                throw new RuntimeException("Error execute sql file" + ": " + EOS + ex);

            }

        } //END: runSqlFile()

    } //END: class SqlUtil

} //END: class RootApplicationContextConfig

Og testen:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = RootApplicationContextConfig.class)
public class dbtest {

    @Autowired
    InMemoryProductRepository productRepository = null;

    /**
     * Test DB connecton and table creation
     *
     * @throws Exception
     */
    @Test
    void ProductTest() throws Exception {

        Assertions.assertNotEquals(null, productRepository);

        return;

    } //END: ProductTest()

} //END: class dbtest

Det feilmeldingen: java.lang.NoClassDefFoundError: org / Spring Framework / core / merknad / MergedAnnotations $ SearchStrategy

    at org.springframework.test.context.support.TestPropertySourceUtils.buildMergedTestPropertySources(TestPropertySourceUtils.java:74)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:386)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:294)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:137)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:122)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$0(ExtensionValuesStore.java:81)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:182)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:84)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:88)
    at org.junit.jupiter.engine.execution.NamespaceAwareStore.getOrComputeIfAbsent(NamespaceAwareStore.java:61)
    at org.springframework.test.context.junit.jupiter.SpringExtension.getTestContextManager(SpringExtension.java:213)
    at org.springframework.test.context.junit.jupiter.SpringExtension.beforeAll(SpringExtension.java:77)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$7(ClassBasedTestDescriptor.java:355)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:355)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:189)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:77)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:132)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Hvordan å gi en Dependency Injection med fjær + juint5? Jeg prøvde før:

  InMemoryProductRepository productRepository = null;

ApplicationContext context = new AnnotationConfigApplicationContext(RootApplicationContextConfig.class);

context.getBean(InMemoryProductRepository.class);

Assertions.assertNotEquals(null, productRepository);

Men det gjenopplivet en annen feil: java.lang.IllegalStateException: Nei ServletContext sett

For det jeg trenger ServletContext hvis jeg sipmle prøver å recive DB-leverandøren bønne i testmetoden?

Svarte 02/12/2019 kl. 23:56
kilden bruker

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more