面试题答案
一键面试1. 框架原理
- JUnit:是Java语言的单元测试框架,通过注解(如
@Test
)来标识测试方法,提供测试执行、断言等功能。 - Spring:一个强大的Java应用开发框架,其依赖注入(DI)机制允许将对象的创建和依赖关系管理交给框架,提高代码的可维护性和可测试性。
2. 整合步骤
引入依赖
在pom.xml
中添加Spring和JUnit相关依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
配置Spring上下文
创建Spring配置文件(如applicationContext.xml
),配置需要注入的bean:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="exampleBean" class="com.example.ExampleBean"/>
</beans>
使用SpringJUnit4ClassRunner
在测试类上使用@RunWith(SpringJUnit4ClassRunner.class)
和@ContextConfiguration
注解,将Spring上下文引入JUnit测试:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class ExampleTest {
@Autowired
private ExampleBean exampleBean;
@Test
public void testExampleBean() {
exampleBean.doSomething();
}
}
3. 扩展注解的设计
自定义注解
定义一个新的注解用于测试权限控制:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestPermission {
String[] roles() default {};
}
4. 权限控制的具体实现方式
自定义TestRule
创建一个实现TestRule
接口的类来进行权限验证:
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class PermissionTestRule implements TestRule {
private final String currentRole;
public PermissionTestRule(String currentRole) {
this.currentRole = currentRole;
}
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
TestPermission testPermission = description.getAnnotation(TestPermission.class);
if (testPermission != null) {
boolean hasPermission = false;
for (String role : testPermission.roles()) {
if (role.equals(currentRole)) {
hasPermission = true;
break;
}
}
if (!hasPermission) {
throw new RuntimeException("No permission to execute this test");
}
}
base.evaluate();
}
};
}
}
使用TestRule
在测试类中使用PermissionTestRule
:
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class ExampleTest {
@Autowired
private ExampleBean exampleBean;
@Rule
public PermissionTestRule rule = new PermissionTestRule("developer");
@Test
@TestPermission(roles = {"developer", "tester"})
public void testExampleBean() {
exampleBean.doSomething();
}
}
5. 整体架构图
graph TD;
A[JUnit Test Class] --> B[Spring Context];
A --> C[Custom Annotation - TestPermission];
C --> D[PermissionTestRule];
D --> A;
B --> E[ExampleBean];
A --> E;