小编Ste*_*ner的帖子

如何在单个注释中组合许多Spring测试注释?

我在测试类上使用Spring Boot的方便注释,用于集成测试.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Config.class)
@IntegrationTest
@Sql({"classpath:rollback.sql", "classpath:create-tables.sql"})
@Transactional
Run Code Online (Sandbox Code Playgroud)

我发现在每个测试类上复制/粘贴整个块非常难看,所以我创建了自己的@MyIntegrationTest注释

@SpringApplicationConfiguration(classes = Config.class)
@IntegrationTest
@Sql({"classpath:database-scripts/rollback.sql", "classpath:database-scripts/create-tables.sql", "classpath:database-scripts/insert-test-data.sql"})
@Transactional
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我@RunWith(SpringJUnit4ClassRunner.class)在我的新注释中添加,那么JUnit将使用其默认运行符运行 - 这是不可取的.所以现在我必须使用两个注释.

@RunWith(SpringJUnit4ClassRunner.class)
@MyIntegrationTest
Run Code Online (Sandbox Code Playgroud)

我想现在好了,但是有没有办法结合这些注释,所以我可以使用单个注释?

java junit annotations spring-boot

9
推荐指数
1
解决办法
2807
查看次数

PowerMockito.verifyStatic()问题

我需要使用PowerMockito来测试是否调用了特定的静态方法.我使用以下PowerMockito和JUnit库...

  • powermock-的Mockito-1.6.2-full.jar
  • 基于JUnit 4.12.jar

我在让PowerMockito.verifyStatic()方法正常工作时遇到问题.在下面的代码示例中,我尝试使用@PrepareForTest和mockStatic(),并尝试排除它们.在代码示例中,我包含它们.

测试类:

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Test1.class)
public class PowerMockTest {
    @Test
    public void staticVerifyTest() {
        PowerMockito.mockStatic(Test1.class);

        // Test
        PowerMockito.verifyStatic();
        //Test1.staticMethod();
    }
}
Run Code Online (Sandbox Code Playgroud)

被测试类:

public class Test1 {
    public static void staticMethod() {
        System.out.println("Static Method!");
    }
}
Run Code Online (Sandbox Code Playgroud)

测试在运行时通过,但它应该失败,因为从不调用Test1.staticMethod().任何有关这方面的帮助将不胜感激!

java junit powermockito

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

在IntelliJ中将特定测试从JUnit 4迁移到JUnit 5

我正在尝试使用IntelliJ重构代码 - > migrate - > junit 4到junit 5将测试文件从Junit 4迁移到Junit 5.

但它只运行导入优化并显示项目中的所有单元测试,并在重构时询问是或否,当我选择重构时没有任何反应.

任何人都知道另一个迁移工具/插件或使迁移工作的方式?

java junit intellij-idea junit5

8
推荐指数
3
解决办法
1456
查看次数

在testclass中使用JUnit临时文件夹时出现"尚未创建文件夹"错误

我收到错误"尚未创建临时文件夹",该错误来自方法IllegalStateException抛出TemporaryFolder.getRoot().看起来它没有初始化,但我的研究表明,在setUp() - 方法中初始化临时文件夹时通常就是这种情况.但是@Rule像我一样使用它应该在我看来是有用的.有任何想法吗?

考试班

public class FileReaderTest extends TestCase {

  @Rule
  public TemporaryFolder folder = new TemporaryFolder();

  public FileReaderTest(String testName) {
    super(testName);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
  }

  @Override
  protected void tearDown() throws Exception {
    super.tearDown();
  }

  public void testCSVWriterAndReader() throws Exception{
    testWriterAndReader(new CSVFileWriter(), new CSVFileReader());
  }

  private void testWriterAndReader(FileWriteService writer, FileReader reader) throws Exception {
    folder = new TemporaryFolder();
    File tempFile = folder.newFile("test.csv");
    DataSet initializedData = createMockData();
    writer.writeDataSetToFile(initializedData, tempFile.getPath()); …
Run Code Online (Sandbox Code Playgroud)

java junit temporary-directory

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

mockito如何验证使用methodname和反射

我有一个间谍或一个对象的模拟,我想验证一个方法已被调用问题,我在执行时收到methodname时间而不是编译时间

我想做的事情如下:

  SimpleObj mockObject= Mockito.mock(SimpleObj.class);
  Class myClass = SimpleObj.class;
  Method meth = myClass.getMethod("getGuid");

  Mockito.verify(meth.invoke(mockObject));
Run Code Online (Sandbox Code Playgroud)

我已经使用了一种解决方法

MockingDetails mockingDetails = Mockito.mockingDetails(mockObject);

Collection<Invocation> invocations = mockingDetails.getInvocations();

List<String> methodsCalled = new ArrayList<>();
for (Invocation anInvocation : invocations) {
  methodsCalled.add(anInvocation.getMethod().getName());
}
assertTrue(methodsCalled.contains("getGuid");
Run Code Online (Sandbox Code Playgroud)

问题一直有效,直到我使用PowerMockito:对于标准方法,它可以工作,但如果方法是最终的,那么该方法不存在mockingDetails.getInvocations() (但即使在mockingDetails.getInvocations() 实际verify(mock).getGuid()工作中没有很好地存在)

因此,如果您有任何想法/建议,那将很高兴

问候

reflection verify mockito powermock powermockito

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

100%分支覆盖的测试用例没有错误?

问题陈述是

一个零故障的方法,您可以编写一个测试套件,它具有100%的语句覆盖率,但没有找到故障,另一个具有100%分支覆盖率的测试套件确实显示了故障?

这是我为此写的方法

public  faultyMethod1(int x, int y) {
  int X =x;
  int Y = y;

  if (Y !=0){
    Z = X/Y;
  } else {
    System.out.println("Sorry. That's an DiviDeByZeroException");
  }
}

faultyMethod1 (1,2);
faultyMethod1 (2,0);
Run Code Online (Sandbox Code Playgroud)

上面的代码实现了具有100%分支覆盖率的测试套件,确实揭示了故障"

怎么样测试套件到具有100%的语句覆盖,但是没有找到故障

java junit testcase

6
推荐指数
1
解决办法
2895
查看次数

使用Spring的MockMvc框架,如何测试模型属性的属性值?

我正在使用Spring 3.2.11.RELEASE和JUnit 4.11.我正在使用SPring的org.springframework.test.web.servlet.MockMvc框架来测试控制器方法.在一个测试中,我有一个填充了以下对象的模型......

public class MyObjectForm 
{

    private List<MyObject> myobjects;

    public List<MyObject> getMyObjects() {
        return myobjects;
    }

    public void setMyObjects(List<MyObject> myobjects) {
        this.myobjects = myobjects;
    }

}
Run Code Online (Sandbox Code Playgroud)

"MyObject"对象依次具有以下字段......

public class MyObject
{
    …
    private Boolean myProperty;
Run Code Online (Sandbox Code Playgroud)

使用MockMvc框架,如何检查"myobjects"列表中的第一项是否具有等于true的属性"myProperty"?到目前为止我知道它是这样的......

    mockMvc.perform(get(“/my-path/get-page”)
            .param(“param1”, ids))
            .andExpect(status().isOk())
            .andExpect(model().attribute("MyObjectForm", hasProperty("myobjects[0].myProperty”, Matchers.equalTo(true))))
            .andExpect(view().name("assessment/upload"));
Run Code Online (Sandbox Code Playgroud)

但我对如何测试属性属性的值一无所知?

junit spring model mockmvc

6
推荐指数
1
解决办法
4988
查看次数

Jacoco 没有显示 PowerMockito 类的覆盖范围

我们正在使用Jacocoeclemma来进行测试用例覆盖。对于不使用 PowerMockRunner 的类,我们在两者中都获得了正确的覆盖率。对于使用 PowerMockRunner 的类,我们面临着覆盖率问题,例如它在 jacoco 中显示 0% 覆盖率,但在 eclemma 中显示正确的覆盖率。

PowerMockito version :1.7.1
Jdk 1.8
Jacoco:0.7.9
Run Code Online (Sandbox Code Playgroud)

我也尝试使用 PowerMockRunner 规则,但这会导致另一个与验证相关的错误stackframe,该错误很难修复。Eclemma用作eclipse插件,jacoco用作maven插件。任何遇到过这个问题并且能够解决这个问题的人都可以对此有所了解。我已经浏览了很多链接,但没有一个对我有用。

一些有用的参考:

PowerMock ECLemma 覆盖问题

java junit eclemma jacoco-maven-plugin powermockito

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

使用 Spring Boot 和 JUnit 从控制器类模拟存储库

我正在为我的控制器类编写测试用例,它是一个 Spring Boot 应用程序,我只想为调用服务和存储库服务的控制器类编写测试用例。我正在使用 SpringBootTest,它用于为我的所有 bean 创建实例。我只想模拟数据库调用和外部 api 调用。

MyController {

    @Autowired
    MyService service;

    //Api call for getDetails
    service.getDetails();
}


MyService {

   @Autowired
   MyRepository repo;

}

MyControllertest {
    @Autowired
    MyController controller;

    @Mock
    MyRepository repoMock;

    @Before
    public void setup(){
        // intit mocks
    }

    @Test
    public void myTest(){
        when(repoMock.getDetails()).thenReturn(null);
        controller.getdetails();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试用例时,它没有使用模拟存储库,而是使用@AutowiredService 类中提到的存储库。

谁能解释一下如何从控制器类模拟存储库。

在这个博客中发布了很多问题,但没有得到任何答复。

junit mocking mockito powermock spring-boot-test

6
推荐指数
2
解决办法
2万
查看次数

有没有办法在 Spock 中进行像 JUnit 5 这样的嵌套测试?

有没有办法在 Spock 中进行像 JUnit 5 这样的嵌套测试?

特定班级有很多测试,我想将它们分组以便更好地理解和方便查看。

JUnit 提供嵌套测试(https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested)。所以我想知道我是否可以在 Spock 中做类似的事情或者将 Spock 与 JUnit 5 结合使用。

java spock spring-boot junit5

6
推荐指数
0
解决办法
1036
查看次数