小编Pan*_*vas的帖子

迁移到JDK 11后,Spring Boot测试中的Mockito错误

我刚刚迁移到JDK 11以使用最新的Java LTS版本.如果我将Eclipse中的执行JRE从10更改为11(并且只有那时),当我尝试运行我的测试时,我得到以下异常堆栈跟踪.

请注意,如果我切换回jdk-10,一切都按预期工作.我使用Spring Boot,我的测试也使用Mockito.我的pom.xml也在这里显示.

Spring Boot和/或Mockito与jdk-11之间是否存在已知的不兼容性?也许pom.xml中缺少另一个引用?

pom.xml中:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-model</artifactId>
            <version>3.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <!-- update Hibernate dependency on Javassist to 3.23.1 for Java 11 compatibility -->
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.23.1-GA</version>
        </dependency>
    </dependencies> …
Run Code Online (Sandbox Code Playgroud)

java mockito spring-boot

18
推荐指数
1
解决办法
7549
查看次数

迁移到JDK 11后,Spring Boot因Hibernate错误而失败

我刚刚迁移到JDK 11以使用最新的Java LTS版本.如果我将Eclipse中的执行JRE从10更改为11(并且只有那时),当我尝试运行我的测试时,我得到以下异常堆栈跟踪(我删除了一些部分以方便阅读).

请注意,如果我切换回jdk-10,一切都按预期工作.显然我使用Spring启动和Hibernate作为我的ORM.我已经在http://in.relation.to/2018/09/13/using-hibernate-orm-with-jdk11/中检查了该文章,并尝试解决这些依赖性问题.我的pom.xml也在这里显示.

Spring Boot和/或Hibernate与jdk-11之间是否存在已知的不兼容性?也许pom.xml中缺少另一个引用?

编辑:正如@nullpointer正确指出的那样,Spring 5.1支持java 11.但是,我的问题显然是指Spring Boot.

pom.xml中:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
</parent>


<dependencies>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.0.1</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-model</artifactId>
        <version>3.5.4</version>
    </dependency>
    <dependency>
        <groupId>org.modelmapper</groupId>
        <artifactId>modelmapper</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <skipTests>true</skipTests>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal> …
Run Code Online (Sandbox Code Playgroud)

java hibernate spring-boot java-11

11
推荐指数
1
解决办法
4325
查看次数

Mockito间谍 - 部分嘲笑不起作用?

我的场景非常简单.根据对此的最后答案和Mockito本身的文档,尝试使用部分嘲笑.我的测试是:

@Test
public void test() {
    ClassUnderTest realObject = new ClassUnderTest();
    ClassUnderTest spy = spy(realObject);
    when(spy.methodB()).thenThrow(new Exception("Testing"));

    spy.methodA();
}
Run Code Online (Sandbox Code Playgroud)

被测试的课程是:

import org.apache.commons.lang3.NotImplementedException;

public class ClassUnderTest {

    int methodB(){
        throw new NotImplementedException("Not implemented");
    }

    public int methodA(){
        methodB();
        return 0;
    }

}
Run Code Online (Sandbox Code Playgroud)

我希望我的间谍对象会调用方法B引发"测试"异常,而实际上这个方法被称为抛出"未实现"异常.它表现得像我没有部分模拟行为

这是为什么?我错过了什么?

编辑:正如RPresle建议的那样,我尝试使用语法

doThrow(new Exception("Testing")).when(spy.methodB());
Run Code Online (Sandbox Code Playgroud)

但是,我得到一个UnfinishedStubbingException:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
 -> at SimpleTest.test(SimpleTest.java:15)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mockito

7
推荐指数
1
解决办法
6048
查看次数

ModernHttpClient与AndroidClientHandler

我的团队正在改进我们的网络堆栈,并尝试在iOS和Android中默认为ModernHttpClient与"Native"项目设置之间做出决定.

我可以看到一些差异,我想知道是否有人对哪种方法是最佳默认解决方案有意见:

  • ModernHttpClient似乎使用OkHttp而不是AndroidClientHandler.您认为哪个更强大?
  • AndroidClientHandler似乎有一种特定的方法来处理网络错误,将它们包装在Exception中.ModernHttpClient似乎使用了不同的异常.
  • ModernHttpClient似乎具有预认证机制的开端.AndroidClientHandler似乎缺乏它.
  • AndroidClientHandler为自签名Certs提供免费支持.

关于该主题的任何和所有指导都将非常值得赞赏.

.net http xamarin

5
推荐指数
0
解决办法
511
查看次数

如何在内存数据库中使用 H2 调试 Spring Boot 存储库?

我正在研究 Spring Boot 并尝试使用 H2 数据库(在内存中)进行单元测试。我的 Spring 数据存储库实现无法正常工作,我需要帮助来调试它。

我已将我的项目(不使用原始项目)简化为以下示例来隔离和演示问题。该项目可以在GitHub中找到。

Eclipse项目结构:

项目结构

pom.xml:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.0.1</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-model</artifactId>
        <version>3.5.4</version>
    </dependency>
    <dependency>
        <groupId>org.modelmapper</groupId>
        <artifactId>modelmapper</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <skipTests>true</skipTests>
            </configuration>
            <executions>
                <execution>
                    <goals> …
Run Code Online (Sandbox Code Playgroud)

hibernate h2 spring-boot spring-repositories

3
推荐指数
1
解决办法
1万
查看次数