hzdavid / jmockit.cn

JMockit中文网(jmockit.cn)上所有讲述的测试示例源代码
36 stars 15 forks source link

@Mocked修饰对象放在方法中报错 #38

Open aayii2015 opened 4 years ago

aayii2015 commented 4 years ago

站点中的学习示例 链接地址:http://jmockit.cn/showArticle.htm?channel=2&id=6

//@Mocked与@Injectable的不同 public class MockedAndInjectable {

@Test
public void testMocked(@Mocked Locale locale) {
    // 静态方法不起作用了,返回了null
    Assert.assertTrue(Locale.getDefault() == null);
    // 非静态方法(返回类型为String)也不起作用了,返回了null
    Assert.assertTrue(locale.getCountry() == null);
    // 自已new一个,也同样如此,方法都被mock了
    Locale chinaLocale = new Locale("zh", "CN");
    Assert.assertTrue(chinaLocale.getCountry() == null);
}

@Test
public void testInjectable(@Injectable Locale locale) {
    // 静态方法不mock
    Assert.assertTrue(Locale.getDefault() != null);
    // 非静态方法(返回类型为String)也不起作用了,返回了null,但仅仅限于locale这个对象
    Assert.assertTrue(locale.getCountry() == null);
    // 自已new一个,并不受影响
    Locale chinaLocale = new Locale("zh", "CN");
    Assert.assertTrue(chinaLocale.getCountry().equals("CN"));
}

}

报错如下: MockedAndInjectable.testInjectable(Locale) initializationError(org.junit.runner.manipulation.Filter) java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testInjectable(java.util.Locale)], {LeadingIdentifierMatcher:fClassName=java.util.Locale,fLeadingIdentifier=testInjectable]] from org.junit.internal.requests.ClassRequest@d2cc05a

at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:80)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:71)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)