EasyMock或Unitils Mock(非Unitils支持的EasyMock)中是否有任何技术可以将模拟直接注入到被测试类中?
例如.在Mockito中,可以将mocks直接注入类的成员变量中,
public class TimeTrackerTest {
@InjectMocks // Used to create an instance the CUT
private TimeTrackerBean cut;
@Mock // Used to create a Mock instance
EntityManager em;
@Before
public void injectMockEntityManager() {
MockitoAnnotations.initMocks(this); // Injects Mocks into CUT
}
@Test
...
}
Run Code Online (Sandbox Code Playgroud)
这样的事情可以用EasyMock或Unitils Mock完成吗?在easymock中,我们需要在CUT中使用单独的setter方法来支持测试中的注入.我是对的还是方向注射在某种程度上是可能的?
-谢谢
我想为我上课写一个单元测试.这个类有一个公共方法,在public方法中,在同一个类中调用私有方法.我想模拟对这些私有方法的调用.该类与此类似:
public class SomeClass {
public int somePublicMethod(int num) {
int num2 = somePrivateMethod1(num);
int num3 = somePrivateMethod2(num);
return num2 + num3;
}
private int somePrivateMethod1(int num) {
return 2*num;
}
private int somePrivateMethod2(int num) {
return 3*num;
}
}
Run Code Online (Sandbox Code Playgroud)
对于我的单元测试,我试图将PowerMock与Mockito和TestNG一起使用.这是我尝试测试somePublicMethod的测试:
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.spy;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.Assert;
import org.testng.annotations.Test;
@PrepareForTest(SomeClass.class)
public class SomeClassTest {
@Test
public void testSomePublicMethod() throws Exception {
int num = 4;
SomeClass someClassSpy = spy(new SomeClass());
doReturn(8).when(someClassSpy, "somePrivateMethod1", num);
doReturn(12).when(someClassSpy, "somePrivateMethod2", num); …Run Code Online (Sandbox Code Playgroud) 我有一个 Web 服务实现,它验证请求并在响应中写入错误消息。响应的格式不符合要求。
错误消息模板位于资源包属性文件中。
...
key=Plant name {0} Plant Id {1}.
...
Run Code Online (Sandbox Code Playgroud)
这是我在验证后收到的错误消息。
Plant name PLANT1 Plant Id 9,905,005.
Run Code Online (Sandbox Code Playgroud)
但所需的输出没有数字变量的逗号。
Plant name WPLANT Plant Id 9905005.
Run Code Online (Sandbox Code Playgroud)
这是我的代码。
...
key=Plant name {0} Plant Id {1}.
...
Run Code Online (Sandbox Code Playgroud)
我使用以下键和变量调用此方法。
Plant name PLANT1 Plant Id 9,905,005.
Run Code Online (Sandbox Code Playgroud)
如何更改此代码以获得所需的输出?我更喜欢不包含 %s, %d 的方法,因为这样我就失去了更改属性文件中错误消息中变量顺序的能力。
我开始学习Java,几天来一直遇到这个错误。我安装了 eclipse 并且运行得很好。第二天,当我尝试打开该应用程序时,它打不开,并且收到一条消息:
\n“应用程序 \xe2\x80\x9cEclipse\xe2\x80\x9d 无法打开\xe2\x80\x99。”\n然后\n“eclipse 意外退出”
\n单击“显示详细信息”时:
\n-------------------------------------\nTranslated Report \n-------------------------------------\n\nIncident Identifier: FAF2D9F4-0509-4333-8A9B-7DD02068E9E1\nCrashReporter Key: F85AF088-28E6-8FB6-4EAB-F263B78EA434\nHardware Model: MacBookAir9,1\nProcess: eclipse [27160]\nPath: /Applications/Eclipse.app/Contents/MacOS/eclipse\nIdentifier: org.eclipse.platform.ide\nVersion: 4.24.0 (4.24.0.I20220607-0700)\nCode Type: X86-64 (Native)\nRole: Default\nParent Process: launchd [1]\nCoalition: org.eclipse.platform.ide [13823]\n\nDate/Time: 2022-06-22 21:02:08.7804 +0530\nLaunch Time: 2022-06-22 21:02:08.6956 +0530\nOS Version: macOS 12.3.1 (21E258)\nRelease Type: User\nReport Version: 104\n\nException Type: EXC_CRASH (SIGKILL (Code Signature Invalid))\nException Codes: 0x0000000000000000, 0x0000000000000000\nException Note: EXC_CORPSE_NOTIFY\nTermination Reason: CODESIGNING 1 \n\nTriggered by Thread: 0\n\nThread 0 Crashed:\n0 0x16854ad0 _dyld_start + 0\n1 ??? 0x7062000 ???\n\n\nThread 0 crashed with …Run Code Online (Sandbox Code Playgroud) 试图弄清楚如何为原始值编写自定义匹配器.说我有以下自定义匹配器:
class IsEven extends ArgumentMatcher<Integer> {
public boolean matches(Object i) {
return ((Integer) i) % 2 == 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我运行以下测试.'mocked'是一个已经被模拟的类的实例,它有一个方法'someMethod':
@Test
public void primatives() {
mocked.someMethod(2);
ArgumentMatcher<Integer> customMatcher = new IsEven();
// ! Throws NPE !
Mockito.verify(mocked).someMethod(Mockito.argThat(customMatcher));
}
Run Code Online (Sandbox Code Playgroud)
NullPointerException的原因是Mockio.argThat方法总是返回一个null,我猜测它不能被自动装回一个整数.
我觉得这可能是一个常见的用例 - 任何建议?
谢谢,罗伊
以下代码导致org.mockito.exceptions.misusing.MissingMethodInvocationException:
Level level = mock(Level.class);
IOException ioException = new IOException("test");
when(level.getSyslogEquivalent()).thenThrow(ioException);
Run Code Online (Sandbox Code Playgroud) 我从这里找到了设备检测的解决方案.
任何人都知道我的第二个要求.
谢谢你的帮助.欢迎任何建议/意见.
编辑:我可以从我的应用程序中获取此详细信息.
未安装在用户应用中.
因为这个应用程序用于酒店的Wi-Fi.每个客户都使用酒店的Wi-Fi,但他们没有app.酒店管理员收集客户使用数据的金额.在我的应用程序的帮助下.
我有以下课程:
class MyClass {
public void doIt() {
methodOne();
methodTwo();
methodThree();
}
private void methodOne() {
// ...
}
// rest of methods similar...
}
Run Code Online (Sandbox Code Playgroud)
我的目的是验证当我调用doIt()时,将按顺序调用方法metodOne(),methodTwo()和methodThree().
我正在使用mockito进行嘲弄.有谁知道我如何测试这种情况?
我正在使用mockito为已经通过集成测试测试的应用程序编写一些单元测试,但我们还需要开发单元测试.
这是测试的代码:
public class TestResourceB {
@Mock
ResourceB b;
@Mock
ResourceC c;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
TestObjects.InitializeObjects();
}
@Test
public void testMethodResourceA() {
when(b.callFuncA()).thenCallRealMethod();
when(b.callFuncB()).thenReturn(TestObjects.mockedListA);
when(b.callFuncC((B)anyVararg())).thenCallRealMethod();
when(c.callFuncB()).thenReturn(TestObjects.mockedListB);
when(c.callFuncA()).thenCallRealMethod
String output = b.callFuncA();
}
}
Run Code Online (Sandbox Code Playgroud)
这是类ResourceB
public class ResourceB {
ResourceC c = new ResourceC();
public String callFuncA(){
/*Calling callFuncB and doing some other stuff*/
String test = callFuncC(arg1);
}
public List<A> callFuncB() {
/*returns the mocked list A*/
}
public String callFuncC(B arg1) {
String test2 = c.callFuncA(); …Run Code Online (Sandbox Code Playgroud)