Tag: junit4

如何在多个测试类之间共享JUnit BeforeClass逻辑

目前,我的所有JUnit测试都是从提供用@BeforeClass和@AfterClass注释标记的方法的公共基类扩展而来的 – 所有这些都是为了要使用的测试设置一堆静态资源/服务。 由于以下几个原因,这对我来说似乎很尴尬: JUnit4的一部分(根据我的理解)是我们不再需要这种经典的测试inheritance。 当我将这些测试作为套件的一部分运行而不是单独运行时(我们经常这样做),多次调用@BeforeClass和@AfterClass ,减慢测试速度 – 我们真的应该只调用一次 我想要做的是以某种方式将当前的BeforeClass / AfterClass逻辑移出inheritance链,并将其转换为可以由各个测试和整个套件共享的内容。 可以这样做吗? 如果是这样,怎么样? (如果重要的话,我正在使用JUnit 4.7,更新到不同的版本可能很难卖)

将模拟注入Spring MockMvc WebApplicationContext

我正在使用Spring-boot测试(通过JUnit4和Spring MockMvc)一个REST服务适配器。 适配器只是将对其发出的请求传递给另一个REST服务(使用自定义RestTemplate ),并将其他数据附加到响应中。 我想运行MockMvc测试来执行控制器集成测试,但是想要使用模拟覆盖控制器中的RestTemplate ,以允许我预定义第三方REST响应并防止它在每次测试期间被命中。 我已经能够通过实例化MockMvcBuilders.standAloneSetup()并将其传递给要测试的控制器来完成此操作,并按照本文(以及我的设置)中所列的模拟注入,但是我无法使用MockMvcBuilders.webAppContextSetup() 。 我已经阅读了其他几篇文章,其中没有一篇文章回答了如何实现这一目标的问题。 我想使用实际的Spring应用程序上下文进行测试,而不是单独使用,以防止应用程序可能增长时出现任何差距。 编辑:我使用Mockito作为我的模拟框架,并试图将其中一个模拟注入上下文。 如果没有必要,那就更好了。 控制器: @RestController @RequestMapping(Constants.REQUEST_MAPPING_PATH) public class Controller{ @Autowired private DataProvider dp; @Autowired private RestTemplate template; @RequestMapping(value = Constants.REQUEST_MAPPING_RESOURCE, method = RequestMethod.GET) public Response getResponse( @RequestParam(required = true) String data, @RequestParam(required = false, defaultValue = “80”) String minScore ) throws Exception { Response resp = […]

JUnit中的注释列表

最近,我研究并实现了JUnit框架。 因此,我知道在JUnit中使用的注释很少: – @Parameters @SuiteClasses({}) , @Parameters @SuiteClasses({}) , @Parameters @SuiteClasses({}) , @Parameters @SuiteClasses({}) , @Parameters @SuiteClasses({}) , @Parameters @SuiteClasses({}) , @Parameters @Runwith(Suite.class) @SuiteClasses({}) , @Parameters @SuiteClasses({}) , @Parameters , @RunWith(Parameterized.class)和@Rule 。 我确信在JUnit中有更多的注释。 任何人都可以用一个可以使用的更多注释列表以及在什么情况下使用它们来指导我? 谢谢。

Junit 4.12问题测试exception

我有一个简单的方法试图获取一些文件。 我想测试文件何时不存在,这就是我的问题开始的地方。 测试一直在失败。 方法类似于: public Configuration populateConfigs(Configuration config) throws UnRetriableException { try { …. } catch (IOException | ConfigurationException e) { log.error(” getConfiguration : “, e); throw new UnRetriableException(e); } throw new UnRetriableException(“problem getting config files.”); } 在我的测试中,我尝试了两个单独的解决方 使用SO解决方案中建议的新样式 @Rule public ExpectedException exception = ExpectedException.none(); @Test public void testPopulateConfigurationMissing() throws Exception { exception.expect(UnRetriableException.class); DefaultConfigHandler configurationFactory […]

如何为创建新对象的void方法编写junit测试用例

public class SupportController{ public void disableUserAccount(String username) throws Exception { UserAccount userAccount = new UserAccount(Constants.SYSTEM, Constants.CONTAINER, username); UserAccount.disableAccount(); } } 我如何测试创建的useraccount是否被禁用?

如何检查Java类是否包含JUnit4测试?

我有一个Java类。 如何检查类是否包含JUnit4测试的方法? 我是否必须使用reflection对所有方法进行迭代,或者JUnit4是否提供此类检查? 编辑: 由于评论不能包含代码,我根据答案放置了我的代码: private static boolean containsUnitTests(Class clazz) { List methods= new TestClass(clazz).getAnnotatedMethods(Test.class); for (FrameworkMethod eachTestMethod : methods) { List errors = new ArrayList(); eachTestMethod.validatePublicVoidNoArg(false, errors); if (errors.isEmpty()) { return true; } else { throw ExceptionUtils.toUncheked(errors.get(0)); } } return false; }

对IO进行JUnit测试

我是新来的,也是junit测试的新手。 我有一个有两种方法的类,我想为它编写unit testing。 我不知道如何开始我阅读一些基本的教程,但我无法开始如何。 你们中的任何人都可以为我提供一些基本的骨架。 我的课是 public class CreateCSV { MyWriter csvOutput = null; public void createSortedSet ( final HashMap map, final long totalSize, final long totalSizewithHeader, File file ) { ArrayList messages = new ArrayList(); try { messages.addAll( map.values() ); map.clear(); for ( Signal signal : messages ) { signal.setBandwidth( ( signal.getSize() / ( […]

想要但不要援引:Mockito PrintWriter

嗨,我正在研究一个项目,并使用PrintWriter类在文件中打开和写入。 但是当我为相同的情况编写测试用例时,它会在第153行给出以下错误 Wanted but not invoked: mockPrintWriter.println(“ID url1 “); -> at xyzverify(ProcessImageDataTest.java:153) Actually, there were zero interactions with this mock. 代码:(使用龙目岛图书馆) ProcessImageData.java @Setter @RequiredArgsConstructor public class ProcessImageData implements T { private final File newImageDataTextFile; @Override public void execute() { LineIterator inputFileIterator = null; try { File filteredImageDataTextFile = new File(filteredImageDataTextFilepath); PrintWriter writer = new PrintWriter(newImageDataTextFile); […]

抛出Mockito和PowerMock MethodNotFoundException

使用Powermockito和Mockito为连接池构建一些简单的unit testing时遇到以下错误,我绕过了Hikari CP。 测试的设置如下。 令我感到困惑的是,我有一些未显示的unit testing,它们都使用相同的设置和方法。 只有这一个unit testing继续失败并出现该错误。 when我把它放在顶部的时候,它们都无法找到方法并不重要。 org.powermock.reflect.exceptions.MethodNotFoundException: No methods matching the name(s) getColumnCount were found in the class hierarchy of class java.lang.Object. at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1720) at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1745) at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:983) at org.powermock.core.MockGateway$MockInvocation.findMethodToInvoke(MockGateway.java:317) at org.powermock.core.MockGateway$MockInvocation.init(MockGateway.java:356) at org.powermock.core.MockGateway$MockInvocation.(MockGateway.java:307) at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:142) at org.powermock.core.MockGateway.methodCall(MockGateway.java:125) at com.datafiniti.utils.mysqlconnpool.MysqlConnPoolTests.executeStringQuery(MysqlConnPoolTests.java:149) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68) at […]

运行相同类但具有不同初始条件的测试套件

在JUnit 4中,我希望编写一个由同一测试用例的多种风格组成的测试套件,每种风格的初始条件都不同。 这是一个例子: import java.io.File; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({MultiInputClientServerIntegrationTest.NormalInput.class, MultiInputClientServerIntegrationTest.SimulationHashIssue.class}) public class MultiInputClientServerIntegrationTest { @RunWith(Suite.class) @SuiteClasses({TestClientServerIntegration.class}) public class NormalInput {} @RunWith(Suite.class) @SuiteClasses({TestClientServerIntegration.class}) public class SimulationHashIssue { public SimulationHashIssue() { TestClientServerIntegration.simulation = new File(“test\\BEECHA01\\sim2.zip”); TestClientServerIntegration.inputFile = “files\\config.in”; } } } 如您所见,两个内部类都具有TestClientServerIntegration.class SuiteClasses,但第二个内部类正在更改一些静态变量值。 我发现这个构造函数永远不会被调用,因此这些静态函数永远不会被更改。 我的最终目标是使用多种类型的输入反复运行此TestClientServerIntegration.class 。 如果我能以这种方式运行测试套件,那将是理想的 – 所以希望它是可能的。 我想尽可能少地攻击JUnit,但是需要完成的工作将会完成。