Spring给出了IllegalStateException:无法加载ApplicationContext,提示如何解决?

时间:2017-11-03 11:37:36

标签: java spring

NetBeans给出了以下错误消息:

无法加载ApplicationContext org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)org.springframework .test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager .java:242)org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)org.junit。 internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)org.springframework.test.context.junit4。 SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate( RunBeforeTestClassCallbacks.java:61)org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)org.junit.runners.ParentRunner.run(ParentRunner.java:309)org.springframework.test。 context.junit4.SpringJUnit4ClassRunner.run(基于SpringJUnit4ClassRunner。 java:190)fi.helsinki.cs.tmc.testrunner.TestRunner $ TestingRunnable.runTestCase(TestRunner.java:134)fi.helsinki.cs.tmc.testrunner.TestRunner $ TestingRunnable.doRun(TestRunner.java:89)fi。 helsinki.cs.tmc.testrunner.TestRunner $ TestingRunnable.run(TestRunner.java:70)java.lang.Thread.run(Thread.java:748)

这是我的代码:

HelloModelApplication:

package wad.hellomodel;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloModelApplication {

public static void main(String[] args) throws Exception {
    SpringApplication.run(HelloModelApplication.class, args);
}
}

HelloModelController:

    import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloModelController {

 @GetMapping("/")
public String title(Model model, @RequestParam String title) {
    model.addAttribute("title", title);
    return "index";
}

@GetMapping("/")
public String person(Model model, @RequestParam String person) {
    model.addAttribute("person", person);
    return "index";


   }

}

Localhost也不会显示。我真的很喜欢Spring,我想知道为什么这不起作用。

编辑:

这是我的pom:

    <?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>wad</groupId>
    <artifactId>Set01_06.HelloModel</artifactId>
    <name>Set01_06.HelloModel</name>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M5</version>
        <relativePath/>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- TMC -->
        <dependency>
            <groupId>fi.helsinki.cs.tmc</groupId>
            <artifactId>edu-test-utils</artifactId>
            <version>0.4.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>tmc</id>
            <name>TMC repo</name>
            <url>http://maven.testmycode.net/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>tmc</id>
            <name>TMC repo</name>
            <url>http://maven.testmycode.net/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

我认为我也找到了测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@Points("01-06")
public class HelloModelTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void bothShownOnPage() throws Exception {
        this.mockMvc.perform(get("/?title=HelloWorld&person=Robot")).andDo(print()).andExpect(status().isOk())
                .andExpect(content().string(containsString("Robot")))
                .andExpect(content().string(containsString("HelloWorld")))
                .andExpect(content().string(not(containsString("Girl"))))
                .andExpect(content().string(not(containsString("SuperStory"))));
        this.mockMvc.perform(get("/?title=SuperStory&person=Girl")).andDo(print()).andExpect(status().isOk())
                .andExpect(content().string(not(containsString("Robot"))))
                .andExpect(content().string(not(containsString("HelloWorld"))))
                .andExpect(content().string(containsString("Girl")))
                .andExpect(content().string(containsString("SuperStory")));

    }
}

0 个答案:

没有答案