gpt4 book ai didi

java - 使用 JUnit 和 Mockito 测试类时出现异常

转载 作者:行者123 更新时间:2023-12-01 10:36:03 24 4
gpt4 key购买 nike

我正在尝试对写在 DAO 层之上的服务层类进行单元测试,但出现以下异常。

请指导。

异常

Running com.ministore.service.MasterDataServiceTest
actStates = null
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.941 sec <<< FAILURE!
testGetAllStates(com.ministore.service.MasterDataServiceTest) Time elapsed: 0.8 sec <<< FAILURE!
junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:48)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertNotNull(Assert.java:218)
at junit.framework.Assert.assertNotNull(Assert.java:211)
at com.ministore.service.MasterDataServiceTest.testGetAllStates(MasterDataServiceTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Failed tests: testGetAllStates(com.ministore.service.MasterDataServiceTest)

MasterDataServiceTest.java

public class MasterDataServiceTest {

@Mock
DataSource dataSource;
@Mock
MasterDao dao;
@Mock
Connection cn;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testGetAllStates() throws SQLException {
MasterDataService dataService = new MasterDataServiceImpl(dataSource);

when(dao.getAllStates()).thenReturn(new ArrayList<State>());
when(dataSource.getConnection()).thenReturn(cn);

List<State> actStates = dataService.getAllStates();
System.out.println("actStates = " + actStates);
assertNotNull(actStates);
}

}

MasterDataServiceImpl.java

public class MasterDataServiceImpl implements MasterDataService {

private final DataSource dataSource;

public MasterDataServiceImpl(DataSource dataSource) {
this.dataSource = dataSource;
}

@Override
public List<State> getAllStates() throws SQLException {
return new MasterDaoImpl(dataSource).getAllStates();
}

}

MasterDaoImpl.java

公共(public)类MasterDaoImpl扩展BaseDao实现MasterDao{

private static final Logger LOG = Logger.getLogger(MasterDaoImpl.class);

public MasterDaoImpl(DataSource dataSource) {
super(dataSource);
}

@Override
public List<State> getAllStates() throws SQLException {
String sql = "select * from states a order by a.name";
return QueryExecutor.executeStatesQuery(dataSource.getConnection(), sql);
}

}

BaseDao.java

public abstract class BaseDao {

protected DataSource dataSource;

public BaseDao(DataSource dataSource) {
this.dataSource = dataSource;
}

}

最佳答案

  @Override
public List<State> getAllStates() throws SQLException {
return new MasterDaoImpl(dataSource).getAllStates();
}

每次调用该方法时,服务都会创建一个新的 dao。您在测试中创建的模拟未被服务使用。

关于java - 使用 JUnit 和 Mockito 测试类时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34724487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com