标签: spring-test

java.lang.ClassNotFoundException: org.springframework.core.MethodClasskey

我写了一个 spring-test 类,它失败了java.lang.ClassNotFoundException: org.springframework.core.MethodClasskey exception

当我从 my 调用相应的 bean 时,相同的代码工作正常,ApplicationContext并产生预期的结果。但不知何故,测试方法失败了。我想我不包括一些测试相关的配置。

弹簧测试类

JUnit 错误

pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.gvs</groupId>
    <artifactId>cleanup</artifactId>
    <name>TrySpringLDAP</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.6</java-version>
        <org.springframework-version>4.0.5.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>

        <!-- Logging -->
        <dependency> …
Run Code Online (Sandbox Code Playgroud)

junit spring-test

0
推荐指数
1
解决办法
6597
查看次数

测试后清除 Spring 应用程序上下文

如何在每次测试执行后使用 Junit5 和 Spring Boot 清除应用程序上下文?我希望在测试中创建的所有 bean 在执行后都被销毁,因为我在多个测试中创建了相同的 bean。我不想为所有测试使用一个配置类,而是每个测试都有一个配置类,如下所示。

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = MyTest.ContextConfiguration.class)
public class MyTest{
   ...
   public static class ContextConfiguration {
     // beans defined here... 

   }
}
Run Code Online (Sandbox Code Playgroud)

Putting@DirtiesContext(classMode = BEFORE_CLASS)不适用于 Junit5。

junit spring spring-test applicationcontext junit5

0
推荐指数
1
解决办法
8744
查看次数

将 WebTestClient.BodyContentSpec 转换为 JSON 对象

我有测试用例

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@AutoConfigureWebTestClient
public class PaymentsITest {

    @Autowired
    private WebTestClient client1;


    @Test
    public void getPayment() {
        client1.get().uri("/v1/payments/123" )
                .accept(MediaType.APPLICATION_JSON)
                .exchange()
                .expectBody()
                .jsonPath("$.group_header.identification").exists()
                .jsonPath("$.group_header.date_time").exists()
                .jsonPath("$.response").exists()
        ;
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要检查对象内部的属性response。有没有办法将WebTestClient.BodyContentSpec方法返回的内容转换expectBody()为 JSON 对象或 JSON 字符串?

java spring unit-testing spring-test

0
推荐指数
1
解决办法
3900
查看次数

MockMvc 使用 application/json 返回 HttpMessageNotWritableException

我有一个 spring 2.3.4.RELEASE 的休息端点当我使用 MockMvc 运行控制器测试时,我收到了

wsmsDefaultHandlerExceptionResolver :已解决[org.springframework.http.converter.HttpMessageNotWritableException:没有带有预设内容类型“application/json”的[class com.example.myexample.model.User]转换器]

@SpringBootTest(classes = UserController.class)
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
public class UserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private UserRepository userRepository;

    private static final String GET_USERBYID_URL = "/users/{userId}";
    

    @Test
    public void shouldGetUserWhenValid() throws Exception {
        Address address = new Address();
        address.setStreet("1 Abc St.");
        address.setCity("Paris");

        User userMock = new User();
        userMock.setFirstname("Lary");
        userMock.setLastname("Pat");
        userMock.setAddress(address);

        when(userRepository.findById(1)).thenReturn(Optional.of(userMock));

        mockMvc.perform(get(GET_USERBYID_URL, "1").accept(MediaType.APPLICATION_JSON))
               .andDo(print())
               .andExpect(status().isOk());
    }
}


@RestController
@RequestMapping(path = "/users")
@Slf4j
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping(value = "/{userId}", …
Run Code Online (Sandbox Code Playgroud)

java spring spring-test java-8 spring-boot

0
推荐指数
1
解决办法
2345
查看次数

这里有什么区别 - @Autowired 和 @MockBean

我正在为 Spring Boot 项目中的服务类编写单元测试。当我自动装配正在测试的类时,测试可以正确运行,而当我使用 @MockBean 而不是 @Autowire 时,测试会失败。

@SpringBootTest
class SignupServiceTest {

  @Autowired SignupService signupService;

  @MockBean DSService dsService;

  @MockBean SignupHelper signupHelper;

  @MockBean SessionHelper sessionHelper;

  @MockBean CommonService commonService;
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决差异以及 @MockBean 失败的原因吗?还有一种方法可以在mockito中模拟自动装配类(当前类)的方法。

junit spring-test mockito spring-boot spring-boot-test

-1
推荐指数
1
解决办法
3456
查看次数