Skip to content

Commit

Permalink
修改单侧
Browse files Browse the repository at this point in the history
  • Loading branch information
fartherp committed Jun 20, 2019
1 parent b254389 commit 8a23f94
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
package com.github.fartherp.framework.spring.boot.autoconfigure.configuration;

import com.github.fartherp.framework.core.bean.ServiceLocator;
import com.github.fartherp.framework.core.proxy.PackageNameAutoProxyCreator;
import com.github.fartherp.framework.core.web.interceptor.MockInterceptor;
import com.github.fartherp.framework.spring.boot.autoconfigure.initializer.ServiceLocatorInitializer;
import com.github.fartherp.framework.spring.boot.autoconfigure.service.UserService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

Expand All @@ -24,56 +22,50 @@
* Author: CK
* Date: 2019/1/15
*/
class MockAutoConfigurationTest {
public class MockAutoConfigurationTest {

private AnnotationConfigApplicationContext context;
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withInitializer(new ServiceLocatorInitializer())
.withConfiguration(AutoConfigurations.of(MockAutoConfiguration.class))
.withUserConfiguration(EnableMockConfiguration.class)
.withPropertyValues("fartherpmock.beanNames:*Service",
"fartherpmock.packageNames:com.github.fartherp.framework.spring.boot.autoconfigure.service");

@BeforeEach
void setUp() {
this.context = new AnnotationConfigApplicationContext();
}
@Test
public void testDefaultFartherpMockInterceptor() {
this.contextRunner.run((context) -> {
assertThat(context.getBean(MockInterceptor.class)).isNotNull();
});
}

@AfterEach
void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void testDefaultMockBeanNameAutoProxyCreator() {
this.contextRunner.run((context) -> {
assertThat(context.getBean(BeanNameAutoProxyCreator.class)).isNotNull();
});
}

@Test
void testDefaultConfiguration() {
ServiceLocator.setFactory(this.context);
TestPropertyValues.of("fartherpmock.beanNames:*Service",
"fartherpmock.packageNames:com.github.fartherp.framework.spring.boot.autoconfigure.service").applyTo(this.context);
this.context.register(EnableMockConfiguration.class, MockAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(MockInterceptor.class)).isInstanceOf(MockInterceptor.class);
assertThat(this.context.getBean(BeanNameAutoProxyCreator.class)).isInstanceOf(PackageNameAutoProxyCreator.class);
}
@Test
public void testUserServiceConfiguration() {
this.contextRunner.withUserConfiguration(ServiceConfiguration.class)
.run((context) -> {
ServiceLocator.setFactory(context);
assertThat(context.getBean(BeanNameAutoProxyCreator.class)).isNotNull();
UserService userService = context.getBean("userService", UserService.class);
assertThat(userService.getName()).isEqualTo("UserTestMock");
});
}

@Test
void testUserServiceConfiguration() {
ServiceLocator.setFactory(this.context);
TestPropertyValues.of("fartherpmock.beanNames:*Service",
"fartherpmock.packageNames:com.github.fartherp.framework.spring.boot.autoconfigure.service").applyTo(this.context);
this.context.register(ServiceConfiguration.class, MockAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(MockInterceptor.class)).isInstanceOf(MockInterceptor.class);
assertThat(this.context.getBean(BeanNameAutoProxyCreator.class)).isInstanceOf(PackageNameAutoProxyCreator.class);
UserService userService = this.context.getBean("userService", UserService.class);
assertThat(userService.getName()).isEqualTo("UserTestMock");
}
@Configuration
@EnableMock
public static class EnableMockConfiguration {

@Configuration
@EnableMock
static class EnableMockConfiguration {
}

}
@Configuration
@EnableMock
@ComponentScan("com.github.fartherp.framework.spring.boot.autoconfigure.service")
public static class ServiceConfiguration {

@Configuration
@EnableMock
@ComponentScan("com.github.fartherp.framework.spring.boot.autoconfigure.service")
static class ServiceConfiguration {

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
package com.github.fartherp.framework.spring.boot.autoconfigure.initializer;

import com.github.fartherp.framework.core.bean.ServiceLocator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -20,24 +16,19 @@
* @author: CK
* @date: 2018/5/22
*/
@SpringBootApplication(scanBasePackages = {"com.github.fartherp.framework.spring.boot.autoconfigure.initializer"})
public class ServiceLocatorInitializerTest {
private ConfigurableApplicationContext context;

@BeforeEach
public void init() {
context = SpringApplication.run(ServiceLocatorInitializerTest.class);
}

@AfterEach
public void closeContext() {
SpringApplication.exit(context);
}
private ServiceLocatorInitializer serviceLocatorInitializer = new ServiceLocatorInitializer();

@Test
public void initialize() {
public void testInitialize() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.github.fartherp.framework.spring.boot.autoconfigure.initializer");
context.refresh();

serviceLocatorInitializer.initialize(context);
UserService service = ServiceLocator.getBean("userService");
assertThat(service.getName()).isEqualTo("UserTest");
}

}
}
8 changes: 1 addition & 7 deletions framework-spring-boot-test-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

package com.github.fartherp.framework.spring.boot.test.autoconfigure;

import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand All @@ -25,6 +28,9 @@
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
@OverrideAutoConfiguration(enabled = false)
@AutoConfigureMock
@ImportAutoConfiguration
public @interface FrameworkMockTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
*/

package com.github.fartherp.framework.spring.boot.test.autoconfigure;

import com.github.fartherp.framework.spring.boot.test.autoconfigure.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

Expand All @@ -18,19 +19,19 @@
* Author: CK
* Date: 2019/1/15
*/
@ExtendWith(SpringExtension.class)
@RunWith(SpringRunner.class)
@FrameworkMockTest
@TestPropertySource(properties = {
"fartherpmock.beanNames:*Service",
"fartherpmock.packageNames:com.github.fartherp.framework.spring.boot.test.autoconfigure.service"
})
class FrameworkMockTestIntegrationTest {
public class FrameworkMockTestIntegrationTest {

@Resource
private UserService userService;

@Test
void testUserServiceConfiguration() {
public void testUserServiceConfiguration() {
assertThat(userService.getName()).isEqualTo("UserTestMock");
}
}
10 changes: 2 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,13 @@
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
</dependency>
<dependency><!-- override version managed by spring-boot-dependencies for build on JDK 11 (Can remove when update to spring boot 2.1.x) -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
</dependencies>
</project>
</project>

0 comments on commit 8a23f94

Please sign in to comment.