鉴于我在部署时不知道我的代码将运行什么类型的系统,我如何编写一个使用系统潜力作为衡量标准的性能基准测试.
我的意思是,如果一个系统能够每秒运行1000次代码,我希望测试确保尽可能接近1000.如果它只能做500,那就是我想比较它的速度.
如果它有助于使答案更具体,我正在使用JUnit4.
谢谢.
如何在Spring Data MongoDB中使用存储库方法构建一些测试?我想为我的测试设置测试数据库,因为我不想为此目的使用生产数据库.应该可能,但我不知道.这是我的应用程序上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">
<!-- Default bean name is 'mongo' -->
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
<mongo:options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="${mongo.connect-timeout}"
max-wait-time="${mongo.max-wait-time}"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="${mongo.socket-timeout}"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="${mongo.db}" />
</bean>
<context:component-scan base-package="domain.company.group.project.data.repositories"/>
<!-- MongoDB repositories -->
<mongo:repositories base-package="domain.company.group.project.data.repositories.mongodb"/>
<!-- some other stuff -->
</beans>
Run Code Online (Sandbox Code Playgroud)
让我们说我有一个简单的存储库如下:
public interface LocationRepository extends MongoRepository<Location, String>, LocationRepositoryCustom {
}
Run Code Online (Sandbox Code Playgroud)
其中LocationRepositoryImpl是实现某个Location(域对象)类的所有自定义方法的类.我的测试类看起来像: …
我用
assertEquals(String, String)
Run Code Online (Sandbox Code Playgroud)
在JUnit测试中。Netbeans(或者是Maven?)建议我添加以下项之一的导入:
org.junit.assert.assertEquals
junit.framework.assert.assertEquals
junit.framework.testcase.assertEquals
Run Code Online (Sandbox Code Playgroud)
我应该使用哪一个?为什么?
编辑:
与2个JUnit声明类之间的问题区别相比,此问题列出了三个可能的导入,而另一个问题则列出了两个。
是否可以使用可在多个测试类中使用的通用@Before和@After固定装置?
我已根据模块(库存,销售,采购等)将测试(分类)分开.对于所有这些测试,用户登录是一个先决条件,目前我正在@Before为每个类使用它.问题是当我需要更改用户ID或密码时,我需要更改每个类.有没有办法编写@Before/ @After可以在所有测试类中使用?在这种情况下,测试套件是否可以派上用场?
I keep on getting WanterButNotInvoked exception on my UT with the below code:
@RunWith( PowerMockRunner.class )
@PrepareForTest( { CounterHandler.class,
TrHandlerProtocolsCounterHandler.class, IProtocolCounterHandler.class } )
@Test
public void testInitialize()
{
IProtocolCounterHandler counter = new TrHandlerProtocolsCounterHandler();
CounterHandler mockCounter = PowerMockito.spy( CounterHandler.getInstance() );
PowerMockito.doNothing().when( mockCounter ).initialize();
counter.initialize();
Mockito.verify( mockCounter ).initialize();
Run Code Online (Sandbox Code Playgroud)
}
======================
CounterHandler class
public void initialize() {
//do something
}
Run Code Online (Sandbox Code Playgroud)
======================
TrHandlerProtocolsCounterHandler class
@Override
public void initialize() {
CounterHandler.getInstance().initialize();
}
Run Code Online (Sandbox Code Playgroud)
I have debugged that Counterhandler.getInstance.initialize was executed but when im trying to verify …
我正在使用Spring Boot Starter Test编写JUnit测试用例。我很想使用JunitParamrunner,它可以方便地传递文件以进行参数化测试。基本上,它逐行从文件中读取数据,并且每行都调用一个测试用例。问题是同时使用SpringJUnit4ClassRunner和JUnitParamsRunner都需要传递@RunWith。我不知道该怎么做。谁能提供一些线索。
参数化测试很好,可以将不同的数据输入到测试中。但是,我创建了一个示例计算器,希望为其创建参数化测试。但是,我发现您只能为一个测试创建1组参数化数据。
我创建了参数化测试,以将2个数字与预期结果相加。此数据将无法与减法一起使用,因为预期结果将有所不同。
是否可以为每个测试的加,减,乘和除设置参数化数据?
非常感谢您的任何建议,
@RunWith(Parameterized.class)
public class CalculatorModelPresenterTest {
private CalculatorModel mCalculatorModel;
/* Array of tests */
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{3.0, 4.0, 7.0},
{4.0, 3.0, 7.0},
{8.0, 2.0, 10.0},
{-1.0, 4.0, 3.0},
{3256.0, 4.0, 3260.0}
});
}
private double mNumberOne;
private double mNumberTwo;
private double mExpectedResult;
/* CONSTRUCTOR THAT ASSIGNS THE FIELDS WITH THE TEST DATA */
public CalculatorModelPresenterTest(double numberOne, double numberTwo, double expectedResult) {
mNumberOne = numberOne;
mNumberTwo = numberTwo;
mExpectedResult = expectedResult; …Run Code Online (Sandbox Code Playgroud) 我正在做类似于在泽西岛使用 StreamingOutput 作为响应实体的示例中提到的事情
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response streamExample(@Context UriInfo uriInfo) {
StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream os) throws IOException,WebApplicationException {
try{
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
//Read resource from jar
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("public/" + uriInfo.getPath());
...//manipulate the inputstream and build string with StringBuilder here//.......
String inputData = builder.toString();
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
writer.write(inputData);
writer.flush();
} catch (ExceptionE1) {
throw new WebApplicationException();
}
}
};
return Response.ok(stream,MediaType.APPLICATION_OCTET_STREAM).build();
}
Run Code Online (Sandbox Code Playgroud)
我试图通过模拟 URIInfo …
我发现在JUnit测试中没有检查代码覆盖率的唯一方法是右键单击包,选择Coverage as ...,然后选择配置.然后我可以取消我的测试包.我曾尝试各种可能的组合,排除测试的常规首/ JAVA /代码覆盖下/排除并没有看到任何变化.我总是把我的测试放在一个单独的测试源文件夹中,其包名与我的src代码相同.
我是否真的必须配置每个项目以忽略我的JUnit测试?这似乎是多余的.为什么有人想检查测试的覆盖范围?
如何将测试写入当前方法?我使用 jUnit 4。
public void setImage() {
if(conditionOne){
myView.setImageOne();
} else {
myView.setImageTwo();
}
}
Run Code Online (Sandbox Code Playgroud) junit4 ×10
java ×6
junit ×3
mockito ×2
unit-testing ×2
eclemma ×1
eclipse ×1
jar ×1
jersey ×1
junitparams ×1
powermock ×1
singleton ×1
spring-boot ×1
spring-data ×1
testcase ×1
testing ×1