今天使用junit测试时,明明使用了 ,但就是报异常如下:
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testDownload], {ExactMatcher:fDisplayName=testDownload(resource.DownLoadTest)], {LeadingIdentifierMatcher:fClassName=resource.DownLoadTest,fLeadingIdentifier=testDownload]] from org.junit.internal.requests.ClassRequest@515f550a at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
源代码如下:
@Test public static void testDownload(){ ClientConfig config = new ClientConfig(); config.register(MultiPartFeature.class); Client client = ClientBuilder.newClient(config); client.property("accept", "image/png"); WebTarget target = client.target( "http://localhost:8080/rest.jerseyUpload/rest/file").path("download"); Builder builder = target.request(); // local variables Response response = builder.get(); InputStream inputStream = null; OutputStream outputStream = null; String responseString = null; String filePath = "e:/test/upload/02.jpg"; try { // get response code int responseCode = response.getStatus(); System.out.println("Response code: " + responseCode); if (response.getStatus() != 200) { throw new RuntimeException("Failed with HTTP error code : " + responseCode); } // get response message String responseMessageFromServer = response.getStatusInfo().getReasonPhrase(); System.out.println("ResponseMessageFromServer: " + responseMessageFromServer); // read response string inputStream = response.readEntity(InputStream.class); outputStream = new FileOutputStream(filePath); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } // set download SUCCES message to return responseString = "downloaded successfully at " + filePath; System.out.println(responseString); } catch (Exception ex) { ex.printStackTrace(); } finally { // release resources, if any try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } response.close(); client.close(); } }
后来看了N遍,发现方法里有有static修饰,因为原来是用main()来测试,故static没去掉直接用来junit测试,然后试着把static去掉,果然测试正常了。
后来又试下下带返回值的,即
public String testDownload(){return "";}
发现运行时一样报这样的异常。
看来junit测试时,发现
No tests found matching
异常时,先检查方法是不是有static修饰,以及是否有返回值。无论哪个是肯定的时,都会报错。