我想要一个JUnit 4测试类来实现与其测试类相同的接口.这样,随着接口的改变(我们正在进行早期开发),编译器保证将相应的方法添加到测试类中.例如:
public interface Service {
public String getFoo();
public String getBar();
}
public class ServiceImpl implements Service {
@Override public String getFoo() { return "FOO"; }
@Override public String getBar() { return "BAR"; }
}
public class ServiceTest implements Service {
@Override
@Test
public String getFoo() {
//test stuff
}
@Override
@Test
public String getBar() {
//test stuff
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我得到一个错误:"java.lang.Exception:Method getFoo()应该是void",大概是因为测试方法必须返回void.任何人都知道这方面的任何方式?
我想测试两个服务:
首先,我正在构建一些复杂的文件结构(仅举例{user}/{date}/{time}/{generatedId}.bin)
第二,我将数据写入第一服务传递的文件(第一服务调用第二服务)
如何使用模拟测试这两种服务而不进行任何真正的IO交互?
仅举例如:
第一服务:
public class DefaultLogService implements LogService
{
public void log(SomeComplexData data)
{
serializer.write(new FileOutputStream(buildComplexFileStructure()), data);
or
serializer.write(buildComplexFileStructure(), data);
or
serializer.write(new GenericInputEntity(buildComplexFileStructure()), data);
}
private ComplextDataSerializer serializer; // mocked in tests
}
Run Code Online (Sandbox Code Playgroud)
第二服务:
public class DefaultComplexDataSerializer implements ComplexDataSerializer
{
void write(InputStream stream, SomeComplexData data) {...}
or
void write(File file, SomeCompexData data) {...}
or
void write(GenericInputEntity entity, SomeComplexData data) {...}
}
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,我需要传递FileOutputStream,这将创建一个文件(即我无法测试第一个服务)
在第二种情况下,我需要传递文件.如果我需要测试将写入指定文件的数据,我在第二次服务测试中该怎么办?(我无法测试第二次服务)
在第三种情况下,我认为我需要一些通用IO对象,它将包装文件.也许为此目的有一些现成的解决方案?
我正在尝试在JUNIT4中编写参数化测试,但是我不知道如何制作多个参数,例如:
@ parameter1 {1,2,3,4}
@ test1使用@ parameter1运行测试
@ parameter2 {3,55,66,77}
@ test2使用@ parameters2运行测试
谁能为我提供示例代码片段,将不胜感激。
谢谢。
在JUnit 4中,您可以使用@Test(expected = SomeException.class)注释声明预期的异常.但是,使用Theories进行测试时,@Theory注释没有预期的属性.
在测试理论时声明预期异常的最佳方法是什么?
是的,这是0%覆盖范围内的另一个.我检查并确认了以下内容:
1)...程序确实正在生成相应的cobertura.ser文件,并且该文件和Instrumented目录的内容已经消失,根据http://cobertura.sourceforge.net/faq.html.
2)......"仪表化"是Cobertura测试中确定的第一个类路径.
3)......我的测试套件中确实正在测试线路,并且...
4)......确实正在创建和编写"已检测"目录 - 我看到了这一点.
有点奇怪的是,我注意到Ant生成的报告只是一系列消息:
Testcase: pluralizeIt[0] took 0.004 sec
Caused an ERROR
Instruction type does not match stack map in method StringUtil.main([Ljava/lang/String;)V at offset 50
java.lang.VerifyError: Instruction type does not match stack map in method StringUtil.main([Ljava/lang/String;)V at offset 50
at PluralTest.pluralizeIt(PluralTest.java:55)
Run Code Online (Sandbox Code Playgroud)
但是如果我注释掉所有Cobertura引用和目标,那么测试运行正常,Ant报告在成功和失败方面给了我100%的预期.
我的build.xml文件在下面...任何想法??? 谢谢....
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestPlurazliation" default="coverage" basedir=".">
<property name="src" location="src" />
<property name="build" location="bin" />
<property name="cobertura" location="/Users/Dauber/Desktop/cobertura-1.9.4.1" />
<property name="instrumented" location="instrumented" />
<property name="coverage.xml" location="coverage-xml" />
<property name="coverage.html" location="coverage-html" …Run Code Online (Sandbox Code Playgroud) 如何为私有构造函数编写@test类.我想用emma工具覆盖它.
public final class Product {
private Product() {
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以建议一个简单的方法?
谢谢.
我正在尝试对此方法进行单元测试:
/**
* finds all widget descriptions containing specified text
* @param searchText
* @return
*/
@Transactional
public List<Integer> returnWidgetIdsFromSearchWord(String searchText){
List<Integer> widgetIds = new ArrayList<Integer>();
MapSqlParameterSource args = new MapSqlParameterSource();
try{
widgetIds = (List<Integer>) jdbt.queryForList("SELECT idwidgets FROM descriptions "
+ "WHERE descriptiontext LIKE '%"+ searchText + "%'", args, Integer.class);
}catch(Exception e){
}
return widgetIds;
}
Run Code Online (Sandbox Code Playgroud)
使用此JUnit测试:
@Test
public void testReturnWidgetIdsFromSearchWord(){
List<Integer> widgetIds = null;
when(jdbt.queryForList(Matchers.anyString(),
Matchers.any(MapSqlParameterSource.class),
Matchers.any(Integer.class))).thenReturn(idList);
widgetIds = (List<Integer>) dDao.returnWidgetIdsFromSearchWord("someText");
assertEquals(widgetIds, idList);
}
Run Code Online (Sandbox Code Playgroud)
我试过在没有Matcher的情况下使用Integer.class - 没有运气,因为它抱怨需要3个匹配器.有什么建议?谢谢
有人知道为什么我不能使用MultiDexTestRunner吗?
我的build.gradle包含:
android {
...
defaultConfig {
...
multiDexEnabled true
testInstrumentationRunner 'com.android.test.runner.MultiDexTestRunner'
}
dependencies {
...
// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'junit:junit:4.12'
Run Code Online (Sandbox Code Playgroud)
Gradle的Android插件版本为2.0.0-alpha2(com.android.tools.build:gradle:2.0.0-alpha2)
//Runs all unit tests.
@RunWith(Suite.class)
@Suite.SuiteClasses({ACLTest.class})
public class UnitTestSuite {
public UnitTestSuite() {}
}
Run Code Online (Sandbox Code Playgroud)
测试类:
@RunWith(AndroidJUnit4.class)
@MediumTest
public class ACLTest {
static Context context;
public ACLTest() {
}
@BeforeClass
public static void setUpBeforeClass() {
...
context = InstrumentationRegistry.getTargetContext();
}
@Before
public void setUpBeforeTest() throws Exception {
...
}
@After
public void tearDown() throws Exception …Run Code Online (Sandbox Code Playgroud) android unit-testing junit4 android-multidex android-studio-2.0
任何人都可以建议我如何为下面的类编写JUnit:
@Controller
@RequestMapping(value = "/cutdata", consumes = "TEXT/XML")
public class CustController
{
Logger LOG = Logger.getLogger(CustController.class);
@Autowired
CustService custService;
@Autowired
MarCusService marCustService;
@Resource(name = "CustValidator")
private CusValidator validator;
@Resource(name = "cmsSiteService")
private CMSSiteService cmsSiteService;
protected CMSSiteService getCmsSiteService()
{
return cmsSiteService;
}
@RequestMapping(value = "/methodcall", method = RequestMethod.PUT)
public @ResponseBody ResponseEntity<?> methodCall(@RequestBody final CustDTO data)
throws WebServicesException
{
String statusCode = null;
try {
if (data.getGroup() != null && !data.getGroup().equals(String.valueOf(GroupEnum.ALL))) {
validator.validate(data);
}
} catch (WebServicesException e) {
return new ResponseEntity<>(e.getMessage(), …Run Code Online (Sandbox Code Playgroud) 我已经开始了一个新项目:PostfixSQLConfig。这是一个简单的Spring Boot应用程序,必须为4个简单的数据库表提供CRUD访问。我为第一个表编写了存储库,并为该存储库编写了一些基本的集成测试。由于此特定表不应提供更新功能,因此我将更新功能实现为:
@Override
public void update(@NonNull Domain domain) throws NotUpdatableException {
throw new NotUpdatableException("Domain entities are read-only");
}
Run Code Online (Sandbox Code Playgroud)
NotUpdatableException我的自定义异常类在哪里。
此代码的IT如下所示:
@Test(expected = NotUpdatableException.class)
public void testUpdate() throws NotUpdatableException {
val domain = Domain.of("test");
domainRepository.update(domain);
}
Run Code Online (Sandbox Code Playgroud)
如果从我的IDE(IntelliJ 2018.2 EAP)运行此测试,它将通过,但运行mvn verify失败并显示以下信息:
java.lang.NoClassDefFoundError: com/github/forinil/psc/exception/NotUpdatableException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at org.apache.maven.surefire.util.ReflectionUtils.tryGetMethod(ReflectionUtils.java:60)
at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isSuiteOnly(JUnit3TestChecker.java:65)
at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isValidJUnit3Test(JUnit3TestChecker.java:60)
at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.accept(JUnit3TestChecker.java:55)
at org.apache.maven.surefire.common.junit4.JUnit4TestChecker.accept(JUnit4TestChecker.java:53)
at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:102)
at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:309)
at org.apache.maven.surefire.junit4.JUnit4Provider.setTestsToRun(JUnit4Provider.java:189)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:132)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:379)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:340)
at …Run Code Online (Sandbox Code Playgroud)