我正在尝试测试一个具有自动连线存储库字段的类。
@Component
public class DefaultRuleTester implements RuleTester {
private static final Logger LOG = LogManager.getLogger(DefaultRuleTester.class);
@Autowired
RuleChainRepository ruleChainRepository;
public DefaultRuleTester() {
}
@Override
public List<ResultChain> test(Entity entity) {
if (entity == null)
return Collections.emptyList();
try {
return selectAndTestAllRuleChains(entity);
} catch (InvalidClassException e) {
LOG.error("Unable to select RuleChains from database.", e);
return Collections.emptyList();
}
}
protected List<ResultChain> selectAndTestAllRuleChains(Entity entity) throws InvalidClassException {
Collection<RuleChain> ruleChains = selectRuleChainsFromConnection();
return testRuleChains(entity, ruleChains);
}
private Collection selectRuleChainsFromConnection() throws InvalidClassException {
Collection<RuleChain> ruleChains = ruleChainRepository.findAll();
if (ruleChains == null)
return Collections.emptyList();
return ruleChains;
}
protected List<ResultChain> testRuleChains(Entity entity, Collection<RuleChain> ruleChains) {
// business logic
}
}
规则链存储库:
public interface RuleChainRepository extends JpaRepository<RuleChain, Long> {
}
我正在使用 Mockito 将存储库注入 CUT 中:
@RunWith(MockitoJUnitRunner.class)
public class DefaultRuleTesterTest {
private final Incident entity = new Incident();
@InjectMocks
private DefaultRuleTester tester;
@Mock
private RuleChainRepository ruleChainRepository;
@Before
public void setUp() {
tester = new DefaultRuleTester();
}
@Test
public void whenEntityTested_shouldReturnGivenResultChains() {
Mockito.when(ruleChainRepository.findAll()).thenReturn(singletonList(new RuleChainSpy()));
List<ResultChain> resultChain = tester.test(entity);
assert(1, resultChains.size());
}
}
但是,运行测试会抛出一个 NullPointerException,指向@Autowired
ruleChainRepository
。
java.lang.NullPointerException
at com.remsdaq.resque.officersubscription.entity_controller.DefaultRuleTester.selectRuleChainsFromConnection(DefaultRuleTester.java:30)
at com.remsdaq.resque.officersubscription.entity_controller.DefaultRuleTester.selectAndTestAllRuleChains(DefaultRuleTester.java:25)
at com.remsdaq.resque.officersubscription.entity_controller.DefaultRuleTester.test(DefaultRuleTester.java:17)
at com.remsdaq.resque.officersubscription.entity_controller.DefaultRuleTesterTest.whenTested_shouldReturnGivenResultChains(DefaultRuleTesterTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我尝试在测试的设置中使用MockitoAnnotations.initMocks(this)
而不是(以及并排@RunWith(MockitoJUnitRunner.class)
(。我也尝试给RuleChainRepository
一个@Component
注释。
我在其他地方使用相同的注释集来模拟其他组件的同一存储库,并且我看不出它在这里不起作用的任何原因。我有什么明显的遗漏吗?
POM的相关部分:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
提前非常感谢。
这就是问题所在
public void setUp() {
tester = new DefaultRuleTester();
}
删除这段代码。
您正在执行@InjectMocks
,然后正在创建一个新对象。因此,测试器对象不会注入存储库模拟。