有没有办法将以下字符串与任何 hamcrest 匹配器匹配。
"{\"messageType\":\"identify\",\"_id\":\"7de9a446-2ced-4bda-af35-81e95ad2dc32\",\"address\":\"192.168.0.0\",\"port\":7070}"
Run Code Online (Sandbox Code Playgroud)
这个字符串被传递给一个方法。我使用 JMock 期望来匹配它。
问题:“72e3a446-2fed-4bda-ac35-34e95ab3dc32”部分是随机生成的UUID,是在测试方法内部生成的。是否有一个 Hamcrest 字符串匹配器可以匹配类似的东西
new StringCompositeMatcher("{\"messageType\":\"identify\",\"_id\":\"", with(any(String.class)), "\"address\":\"192.168.0.0\",\"port\":7070}" )
Run Code Online (Sandbox Code Playgroud)
它必须匹配预期的字符串以"{\"messageType\":\"identify\",\"_id\":\"之后的任何字符串开始,并以",\"address\":\"192.168.0.0\",\"port\":7070}"
编辑:解决方案
with(allOf(new StringStartsWith("{\"messageType\":\"identify\",\"_id\":\""), new StringEndsWith("\",\"address\":\"192.168.0.0\",\"port\":7070}")))
Run Code Online (Sandbox Code Playgroud) 我有一个转发方法的类foo:
void foo( Concrete c, String s ) { c.bar( s ); }
Run Code Online (Sandbox Code Playgroud)
我想测试一下foo,事实上是否向前.对我来说不幸的是,它Concrete是第三方库中的一个类,并且是一个具体类型,而不是一个接口.因此我必须ClassImposteriser在JMock中使用模拟Concrete,所以在我的测试用例中,我这样做:
@Test
public final void testFoo() {
Mockery context = new JUnit4Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
final Concrete c = context.mock(Concrete.class);
final String s = "xxx" ;
// expectations
context.checking(new Expectations() {{
oneOf (c).bar(s); // exception gets thrown from here
}});
new ClassUnderTest.foo( c, s );
context.assertIsSatisfied();
Run Code Online (Sandbox Code Playgroud)
}
不幸的是,Concrete.bar反过来调用抛出的方法.那个方法是最终的,所以我无法覆盖它.此外,即使我注释掉该行new ClassUnderTest.foo( c, s );,当JMock设置异常时抛出异常,而不是在foo …
我知道解决方案是以某种方式确保在hamcrest之后加载Junit.我有一个intellij项目,我在其中设置了一个外部库,它包含JUnit和JMock以及hamcrest.如何确保此错误不会显示
在java中有没有办法检查某个方法是否在另一个方法中被调用?我正在测试一个类和我遇到播放声音问题的方法,实际上没有办法获取播放的音频文件(内部类中的私有属性)而不更改代码.然而,该方法播放声音的方式是调用播放单个声音的方法(playSadMusic,playHappyMusic等).这些方法在我必须为其创建模拟对象的接口中.我有点坚持我将如何测试这个.有什么想法吗?关于我如何测试这个以外的任何其他想法,而不是检查某个方法是否被调用是受欢迎的.
我正在使用JMock 2.6.0和JUnit 4
音频接口
public interface StockTickerAudioInterface {
public abstract void playHappyMusic();
public abstract void playSadMusic();
public abstract void playErrorMusic();
}
Run Code Online (Sandbox Code Playgroud)
花药界面我必须创建一个模拟
public interface StockQuoteGeneratorInterface {
public abstract StockQuoteInterface getCurrentQuote() throws Exception;
public abstract String getSymbol();
public abstract void setSymbol(String symbol);
public abstract StockQuoteGeneratorInterface createNewInstance(String symbol);
}
Run Code Online (Sandbox Code Playgroud)
正在测试的课程
public class StockQuoteAnalyzer {
private StockTickerAudioInterface audioPlayer = null;
private String symbol;
private StockQuoteGeneratorInterface stockQuoteSource = null;
private StockQuoteInterface lastQuote = null;
private StockQuoteInterface currentQuote = null;
public StockQuoteAnalyzer(String symbol,
StockQuoteGeneratorInterface …Run Code Online (Sandbox Code Playgroud) 我正在使用 Maven 构建一个简单的项目。我无法构建它,因为缺少传递依赖项,即objenesis 1.0.
我在调试模式下运行 Maven 并收到以下消息:
[DEBUG] =======================================================================
[WARNING] The POM for org.jmock:jmock-junit4:jar:2.6.0 is invalid, transitive dependencies (if any) will not be available: 1 problem was encountered while building the effective model for org.jmock:jmock-junit4:2.6.0
[ERROR] Invalid packaging for parent POM org.jmock:jmock-parent:2.6.0, must be "pom" but is "jar" @ org.jmock:jmock-parent:2.6.0
...
Run Code Online (Sandbox Code Playgroud)
当我查看 jmock-parent 时,我似乎找不到对 pom 或 jar 类型的引用。
我该如何解决这个问题?
注意:我们使用我们公司的 Nexus 来获取依赖项。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Poc</groupId>
<artifactId>Poc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<dependencies> …Run Code Online (Sandbox Code Playgroud) 我是jMock的新手,所以我试着用一个简单的例子.但我无法弄清楚为什么它不起作用.这是我正在测试的课程:
package com.application;
import com.domain.Coordinate;
import com.domain.Playable;
public class ChessFacade {
private final Playable board;
public ChessFacade(Playable aBoard) {
board = aBoard;
}
public void showPotentialMoves(Coordinate aCoordinate) {
board.getTileOccupancy(aCoordinate);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Mock对象测试:
package unit.application;
import application.ChessFacade;
import com.domain.Coordinate;
import com.domain.Playable;
import junit.framework.TestCase;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.runner.RunWith;
@RunWith(JMock.class)
public class ChessFacadeTest extends TestCase {
public void testFacadeGetsPotentialMovesFromBoard() {
Mockery context = new JUnit4Mockery();
final Playable mockBoard = context.mock(Playable.class);
ChessFacade facade = new ChessFacade(mockBoard);
final Coordinate …Run Code Online (Sandbox Code Playgroud) 我在这里关注这个例子:jMock - 入门
我收到此错误:无法解析类型junit.framework.TestCase.它是从所需的.class文件间接引用的
我已经进口了这4个罐子:
蚂蚁想法这个错误意味着什么?
package com.test;
import org.jmock.integration.junit3.MockObjectTestCase;
import org.jmock.Expectations;
class PublisherTest extends MockObjectTestCase {
public void testOneSubscriberReceivesAMessage() {
// set up
final Subscriber subscriber = mock(Subscriber.class);
Publisher publisher = new Publisher();
publisher.add(subscriber);
final String message = "message";
// expectations
checking(new Expectations() {
{
oneOf(subscriber).receive(message);
}
});
// execute
publisher.publish(message);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一些使用事务同步管理器的代码..但我似乎无法让它在模拟中工作..我在模拟实体管理器和事务管理器..这样我的上下文保存实体并调用提交...... TransactionSynchronizationManager 确实如此似乎没有被击中……在测试中?
this.transactionTemplate.execute(new TransactionCallback<E>() {
@Override
public E doInTransaction(TransactionStatus status) {
// update entities
TransactionSynchronizationManager.registerSynchronization(new NotificationTransactionSynchronization(){
@Override
public void afterCommit() {
// do some post commit work
int i = notifier.notifyAllListeners();
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
我的测试课:
@Test
public void testHappyPath() {
context.checking(new Expectations() {
{
allowing(platformTransactionManager).getTransaction(definition);
will((returnValue(status)));
oneOf(platformTransactionManager).commit(status);
//next line never gets hit... so the test fails...
//if i remove it will pass but i need to check that it works...
oneOf(mockNotifier).notifyAllListeners();
}
});
this.TestClass.process();
context.assertIsSatisfied();
}
Run Code Online (Sandbox Code Playgroud) 我有一个Handler.java类
它有2个公共方法update(),fetch()
在实际的update()实现中,我调用了public方法fetch()
fetch()依次调用服务.
所以现在我必须编写一个testUpdate()来模拟公共方法调用,即fetch()
由于它不是静态的,我尝试创建另一个Handler.java实例作为模拟,即,
private Handler handler;
@Mocked
private Handler mockedHandler;
Run Code Online (Sandbox Code Playgroud)
现在使用mockedHandler,我在testUpdate()中设置以下代码
new NonStrictExpectations(){
mockedHandler.fetch();
returns(response);
};
handler.update();
Run Code Online (Sandbox Code Playgroud)
现在,我希望mockedhandler用于调用fetch()和调用update()的处理程序实例.
但是当我运行实际的方法调用update()时也被嘲笑!!!
i.e. handler.update(); is not at all going to the update().
Run Code Online (Sandbox Code Playgroud)
帮我模拟我在update()中调用的第二个公共方法
谢谢