如何使用单例实例模拟一个类

时间:2014-11-06 11:25:03

标签: java unit-testing powermock easymock

我正在使用Dao类,它是一个单例。我在我的服务类中使用这个dao类实例。我想单元测试我的服务类。所以我必须模拟dao类。我用EasyMock试了一下。

@RunWith(PowerMockRunner.class)

@PrepareForTest(MusicPlayerDaoImpl.class) 公共类MusicPlayerServiceTest {

private MusicPlayerDaoImpl dao;
private MusicPlayerServiceImpl service;

@Before
public void setUp() throws SQLException{
    this.service = MusicPlayerServiceImpl.getInstance();
    suppressConstructor(MusicPlayerDaoImpl.class);
    mockStatic(MusicPlayerDaoImpl.class);
    this.dao = createMock(MusicPlayerDaoImpl.class);
    this.service.setInstance(this.dao);
    Song song = new Song();
    Album album = new Album();
    Genre genre = new Genre();
    album.setAlbumName("some album);
    genre.setGenreName("some genre);
    song.setTitle("title");
    song.setRating(4);
    song.setAlbum(album);
    song.setGenre(genre);
    EasyMock.expect(this.dao.getSong("Bahara")).andReturn(song);
    EasyMock.replay(this.dao);
}


@Test
public void getSongTest() throws SQLException, IllegalStateException, SecurityException, SystemException {



    String title = "title"; 

Song returnSong = this.service.getSong(title);
assertTrue(returnSong.getTitle() == title);     

}

}

我收到错误“java.lang.AssertionError:   意外的方法调用“

     java.lang.AssertionError: 
  Unexpected method call MusicPlayerDaoImpl.getSong("title"):
    MusicPlayerDaoImpl.getSong("title"): expected: 1, actual: 2
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
    at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:97)
    at service.MusicPlayerDaoImpl$$EnhancerByCGLIB$$e2ddeccf.getSong(<generated>)
    at service.MusicPlayerServiceImpl.getSong(MusicPlayerServiceImpl.java:99)
    at MusicPlayerServiceTest.getSongTest(MusicPlayerServiceTest.java:64)
    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.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    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:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

1 个答案:

答案 0 :(得分:1)

该函数被调用两次而不是1的原因取决于您的服务实际工作方式。也许服务不止一次调用dao方法。

另外,请注意您正在使用

assertTrue(expected == actual)

你应该在哪里使用

assertEquals(expected, actual)

甚至更好

assertThat(actual, is(expected))