Monthly Archives: December 2016

Install Oracle Java 8 on Ubuntu Linux

java8_logo
To install Oracle JDK 8 on Ubuntu you can choose one of two methods:
A. Automatic installation:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

sudo apt-get install oracle-java8-installer

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws

B. Manual installation:

Download oracle jdk at http://www.oracle.com/technetwork/java/javase/downloads/index.html

Extract archive into /usr/local/jdk1.8.0_65

Setup
sudo update-alternatives --install  /usr/bin/java java /usr/local/jdk1.8.0_65/bin/java 1
sudo update-alternatives --install  /usr/bin/javac javac /usr/local/jdk1.8.0_65/bin/javac 1
sudo update-alternatives --install  /usr/bin/javaws javaws /usr/local/jdk1.8.0_65/bin/javaws 1

sudo update-alternatives --set  java /usr/local/jdk1.8.0_65/bin/java
sudo update-alternatives --set  javac /usr/local/jdk1.8.0_65/bin/javac
sudo update-alternatives --set  javaws /usr/local/jdk1.8.0_65/bin/javaws

Thanx to http://stackoverflow.com/a/31869659

Exclude beans from tests in Spring Boot

springboot
Running unit tests bootstraps, for example, scheduling tasks (EnableScheduling, Scheduled).
To exclude automatic start up auto-configuration beans with annotation you can follow next instruction:

1. Create annotation:

public @interface ExcludeFromTests {
}

2. Annotate with it Application and Scheduled beans (or what you want to be skipper during tests):

@SpringBootApplication
@EnableAsync
@EnableScheduling
@ExcludeFromTests
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }
}
@Component
@ExcludeFromTests
public class AppScheduler {

    @Scheduled(fixedRateString = "1000000")
    public void do() {
      //...
    }
}

3. Create TestApplication class for tests and add ComponentScan.Filter:

@SpringBootApplication
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeFromTests.class))
public class TestBackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestBackendApplication.class, args);
    }
}

4. Say Spring boot to use TestApplication class in tests:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestBackendApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SomeTest {
  
  @Test
  public void test1() throws Exception {
    //...
  }
}

5. Profit! -)

See also:
- http://www.concretepage.com/spring/spring-component-scan-include-and-exclude-filter-example-using-javaconfig-and-xml-with-annotation-assignable-aspect-and-regex-filter-types
- http://stackoverflow.com/questions/18992880/exclude-component-from-componentscan
- http://stackoverflow.com/questions/10725192/exclude-subpackages-from-spring-autowiring
- http://stackoverflow.com/questions/25550494/componentscan-excludefilters-not-working-in-spring-4-0-6-release
- http://farenda.com/spring/spring-component-scan-exclude/