gpt4 book ai didi

java - 模拟 java.lang.Exception : Class should be public when I use inner classes in tests

转载 作者:搜寻专家 更新时间:2023-11-01 03:56:41 25 4
gpt4 key购买 nike

我有以下测试:

@RunWith(Enclosed.class)
public class ProductTest {

@RunWith(MockitoJUnitRunner.class)
@Ignore
public static abstract class Base1 {
@Before
public void setUpBase() {...}
}

public static class Test1 extends Base1{
@Test
public void foo(){...}
}
}

public static class Test2 extends Base1{
@Test
public void bar(){...}
}
}
}

为了避免@Ignore,我重构类如下:

@RunWith(Enclosed.class)
public class ProductTest {

@RunWith(MockitoJUnitRunner.class)
public static class Test1 extends Base1 {
@Test
public void foo() {...}
}

@RunWith(MockitoJUnitRunner.class)
public static class Test2 extends Base1 {
@Test
public void bar() {...}
}
}

abstract class Base1 {
@Before
public void setUpBase() {...}
}

但我看到错误:

java.lang.Exception: Class Base1 should be public
at org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:91)
at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:70)
at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:133)
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:165)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:104)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.<init>(JUnit45AndHigherRunnerImpl.java:23)

1.请说明问题原因
2.请告诉我避免以下错误的优雅方法。

我想将逻辑封装在一个文件中。

最佳答案

我最近遇到了同样的问题,我通过将类(class)改为公共(public)类(class)解决了这个问题。您可能需要检查您的 Class Base1 是否确实是一个公共(public)类。

关于java - 模拟 java.lang.Exception : Class should be public when I use inner classes in tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28475003/

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