P E N C I L
Login
Spring tests context

Spring tests

  1. Spring boot test

    1. Main test class
      @RunWith(SpringRunner.class)
      @SpringBootTes
      t(classes = MyConfigClass.class, webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
      @TestPropertySource({"/test-application.yml"}
      class MyTest {}

      1. @Configuration => @SpringBootConfiguration => @SpringBootApplication

      2. @SpringBootTest = @ContextConfiguration + Running webserver

      3. @SpringBootTest(classes={})
        then @ContextConfiguration is IGNORED
        Automatically searches for a @SpringBootConfiguration when and no explicit classes are specified.

    2. @SpringBootApplication
      @ComponentScan(basePackages = "com.ondeck.lynxcallbackapi.service")
      @TestConfiguration
      @Import(value = AuthnzConfig.class)
      class TestConfig{
      }

      1. @TestConfiguration
        When placed on a top-level class, @TestConfiguration indicates that classes in src/test/java should not be picked up by scanning. You can then import that class explicitly where it is required

  2. @LocalServerPort

    1. Must be on class with @SpringRunner

    2. That class is not specified as Configuration (ex: by @SpringBootTest(class={})

    3. We must configure as lazy bean load @Autowire Environemt env; env.getProperty(local.server.port)