這個錯誤,在網上搜找了好久,說是什么jar包沖突,什么環境配置,我經過驗證均是正確的,javac java java -version 都沒問題,環境變量也OK,各種解釋均沒有能夠解決我的問題,最后好一頓折騰,解決了
shiro 的測試方法
1 package com.sharp.forward; 2 3 import org.apache.shiro.SecurityUtils; 4 import org.apache.shiro.authc.UsernamePasswordToken; 5 import org.apache.shiro.mgt.DefaultSecurityManager; 6 import org.apache.shiro.realm.Realm; 7 import org.apache.shiro.realm.SimpleAccountRealm; 8 import org.apache.shiro.subject.Subject; 9 import org.junit.Before; 10 import org.junit.Test; 11 12 public class JavaShiroTest extends ShiroApplicationTests{ 13 14 SimpleAccountRealm realm = new SimpleAccountRealm("simpleRealm"); 15 @Before 16 public void add() { 17 realm.addAccount("張三", "123456"); 18 } 19 20 @Test 21 public void contextLoads() { 22 } 23 @Test 24 public void testAuthenticate() { 25 // 三要素,SecurityManager,subject,realm 26 DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); 27 // SecurityManage設置realm與數據庫或緩存相連接 28 defaultSecurityManager.setRealm(realm); 29 // SecurityUtils設置SecurityManager可以與主題subject相連接 30 SecurityUtils.setSecurityManager(defaultSecurityManager); 31 // 獲取主題,並通過secutityUtil將securityManager和realm聯系起來, 32 Subject subject = SecurityUtils.getSubject(); 33 // 提交主題是通過token提交的 34 UsernamePasswordToken token = new UsernamePasswordToken("張三", "123456"); 35 subject.login(token); 36 System.out.println("subject.isAuthenticated(): "+subject.isAuthenticated()); 37 38 39 40 } 41 42 }
在測試類里面的測試方法報錯,
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testAuthenticate], {ExactMatcher:fDisplayName=testAuthenticate(com.sharp.forward.JavaShiroTest)], {LeadingIdentifierMatcher:fClassName=com.sharp.forward.JavaShiroTest,fLeadingIdentifier=testAuthenticate]] from org.junit.internal.requests.ClassRequest@73c6c3b2
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:80)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:71)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
然后在主類中寫了一個main查看是否報錯
然后找解決辦法,
說明首先,我是創建的springboot工程,創建的時候,因為之前選擇版本的問題,出來的項目總不是完整的項目,需要修改版本,可能是這個原因導致的一些配置發生了變化,現在先看一下我的創建過程,我的項目都是自己搭建的私服
當出現不完整的項目的時候,就把項目的版本修改為適應的版本就可以出來了,可能是由於記憶了,
之前出現題目中的問題是運行其他項目中突然出現的,於是我仍然在同樣的環境下建的項目但運行helloworld
就是main方法打印helloworld,控制台同樣出現了錯誤:找不到或無法加載主類的提示,
最終解決辦法是:先run build install 此時發現不能成功,提示用的是jre不是jdk,然后修改為jdk,再次運行一切都好了,
然后我再去創建項目,除了將https改為http外,其他的完整項目均能一次加載,不需要修改版本了。
這樣解決了,
但是我又重新復現了一下這個問題,發現單單修改了這里有時候還不行,maven install 仍然報jre而不是jdk,同樣又出現上述問題,於是我又修改如下地方
項目右鍵屬性,下方修改為jdk,
再次 install 然后再去運行helloworld和測試,完美解決,看來還是jdk在作怪。
測試運行結果
junit變為綠條了
控制台打印結果如下

1 16:41:35.128 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.sharp.forward.JavaShiroTest] 2 16:41:35.135 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 3 16:41:35.147 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 4 16:41:35.171 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.sharp.forward.JavaShiroTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper] 5 16:41:35.189 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.sharp.forward.JavaShiroTest], using SpringBootContextLoader 6 16:41:35.199 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.sharp.forward.JavaShiroTest]: class path resource [com/sharp/forward/JavaShiroTest-context.xml] does not exist 7 16:41:35.200 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.sharp.forward.JavaShiroTest]: class path resource [com/sharp/forward/JavaShiroTestContext.groovy] does not exist 8 16:41:35.200 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.sharp.forward.JavaShiroTest]: no resource found for suffixes {-context.xml, Context.groovy}. 9 16:41:35.201 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.sharp.forward.JavaShiroTest]: JavaShiroTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 10 16:41:35.284 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.sharp.forward.JavaShiroTest] 11 16:41:35.397 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\worksheet\shiro\target\classes\com\sharp\forward\ShiroApplication.class] 12 16:41:35.404 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.sharp.forward.ShiroApplication for test class com.sharp.forward.JavaShiroTest 13 16:41:35.538 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.sharp.forward.JavaShiroTest]: using defaults. 14 16:41:35.540 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] 15 16:41:35.592 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] 16 16:41:35.593 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute] 17 16:41:35.593 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6e171cd7, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@402bba4f, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@795cd85e, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@59fd97a8, org.springframework.test.context.support.DirtiesContextTestExecutionListener@f5ac9e4, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@123ef382, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@dbf57b3, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@384ad17b, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@61862a7f, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@441772e] 18 16:41:35.596 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 19 16:41:35.596 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 20 16:41:35.598 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 21 16:41:35.598 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 22 16:41:35.672 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 23 16:41:35.672 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 24 16:41:35.676 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 25 16:41:35.676 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 26 16:41:35.677 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 27 16:41:35.682 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 28 16:41:35.690 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@9f116cc testClass = JavaShiroTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@12468a38 testClass = JavaShiroTest, locations = '{}', classes = '{class com.sharp.forward.ShiroApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@76b0bfab, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2f943d71, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@3e57cd70, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5bcea91b], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null]. 29 16:41:35.691 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 30 16:41:35.691 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 31 16:41:35.734 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1} 32 33 . ____ _ __ _ _ 34 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 35 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 36 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 37 ' |____| .__|_| |_|_| |_\__, | / / / / 38 =========|_|==============|___/=/_/_/_/ 39 :: Spring Boot :: (v2.1.0.RELEASE) 40 41 2019-03-02 16:41:36.379 INFO 18776 --- [ main] com.sharp.forward.JavaShiroTest : Starting JavaShiroTest on litan with PID 18776 (started by litan in D:\worksheet\shiro) 42 2019-03-02 16:41:36.382 INFO 18776 --- [ main] com.sharp.forward.JavaShiroTest : No active profile set, falling back to default profiles: default 43 2019-03-02 16:41:39.311 INFO 18776 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 44 2019-03-02 16:41:39.904 INFO 18776 --- [ main] com.sharp.forward.JavaShiroTest : Started JavaShiroTest in 4.153 seconds (JVM running for 5.806) 45 2019-03-02 16:41:40.645 INFO 18776 --- [ main] a.s.s.m.AbstractValidatingSessionManager : Enabling session validation scheduler... 46 subject.isAuthenticated(): true 47 2019-03-02 16:41:41.163 INFO 18776 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
helloworld運行結果
參考https://www.cnblogs.com/fengan/p/5904445.html,重建project有時也可以解決