首页 \ 问答 \ 编译时出错(Error during compile)

编译时出错(Error during compile)

我正在使用eclipse和maven2插件。

Maven创建了一个/ src文件夹,所以我在以下路径中创建了HomeController.java文件:

/src/main/java/web/HomeController.java

当我使用RunAs mavin构建编译时,目标设置为'compile',我得到错误:

E:\dev\eclipse\springmvc2\src\main\java\web\HomeController.java:[5,1] cannot find symbol
symbol: class Controller
@Controller

我的homecontroller.java看起来像:

package com.springmvc2.web;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {


    @RequestMapping("/")
    public String Index(){

        return "index";
    }


    @RequestMapping("/test")
    public String Index2(){

        return "index";
    }


}

I am using eclipse with maven2 plugin.

Maven created a /src folder, so I created my HomeController.java file in the following path:

/src/main/java/web/HomeController.java

When I compile using the RunAs mavin build, with a goal set to 'compile', I get the error:

E:\dev\eclipse\springmvc2\src\main\java\web\HomeController.java:[5,1] cannot find symbol
symbol: class Controller
@Controller

my homecontroller.java looks like:

package com.springmvc2.web;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {


    @RequestMapping("/")
    public String Index(){

        return "index";
    }


    @RequestMapping("/test")
    public String Index2(){

        return "index";
    }


}

原文:https://stackoverflow.com/questions/2233813
更新时间:2023-12-31 09:12

最满意答案

在技​​术上测试与数据库(nosql或其他)的通信不是单元测试 ,因为测试正在测试与外部系统的交互,而不仅仅是测试隔离的代码单元。 然而,与数据库通信的测试通常是非常有用的,并且通常足够快以与其他单元测试一起运行。

通常我有一个服务接口(例如UserService),它封装了处理数据库的所有逻辑。 依赖于UserService的代码可以使用嘲弄的UserService版本,并且易于测试。

当测试与Mongo谈话的服务(例如MongoUserService)的实现时,最简单的方法是编写一些将在本地机器上启动/停止mongo进程的java代码,并让您的MongoUserService与之连接,看到这个问题笔记 。

您可以尝试在测试MongoUserService时嘲笑数据库的功能,但通常这样做太容易出错,并且不会测试您真正想要测试的内容,即与实际数据库的交互。 所以在为MongoUserService编写测试时,您将为每个测试设置一个数据库状态。 看一下DbUnit,为一个使用数据库这样做的框架的例子。


As sbridges wrote in this post it is a bad idea not to have a dedicated service (sometimes also known as repository or DAO) which abstracts the data access from the logic. Then you could test the logic by providing a mock of the DAO.

Another approach which I do is to create a Mock of the Mongo object (e.g. PowerMockito) and then return the appropriate results. This because you don't have to test if the database works in unit tests but more over you should test if the right query was sent to the databse.

Mongo mongo = PowerMockito.mock(Mongo.class);
DB db = PowerMockito.mock(DB.class);
DBCollection dbCollection = PowerMockito.mock(DBCollection.class);

PowerMockito.when(mongo.getDB("foo")).thenReturn(db);
PowerMockito.when(db.getCollection("bar")).thenReturn(dbCollection);

MyService svc = new MyService(mongo); // Use some kind of dependency injection
svc.getObjectById(1);

PowerMockito.verify(dbCollection).findOne(new BasicDBObject("_id", 1));

That would also be an option. Of course the creation of the mocks and returning of the appropriate objects is just coded as an example above.

相关问答

更多
  • 你几乎总是应该进行单元测试,并且应该用单元测试编写代码。 极端主义者甚至在编写代码之前编写测试(它被称为TDD - 测试驱动开发)。 我会给你一个真实的例子:我最近不得不编写一个支持“间隔”的排序NSArray。 意思是,数组应该知道如何插入一个时间间隔并保持排序。 例如,数组看起来像这样:[1-3,5-9,12-50]。 在这个例子中,数组中有3个区间,你可以看到它们被排序。 在我写了我的课之后(我称之为IntervalsArray),我必须编写测试以确保它能正常工作,并且如果我或其他人将来修改代码,我不 ...
  • 在技术上测试与数据库(nosql或其他)的通信不是单元测试 ,因为测试正在测试与外部系统的交互,而不仅仅是测试隔离的代码单元。 然而,与数据库通信的测试通常是非常有用的,并且通常足够快以与其他单元测试一起运行。 通常我有一个服务接口(例如UserService),它封装了处理数据库的所有逻辑。 依赖于UserService的代码可以使用嘲弄的UserService版本,并且易于测试。 当测试与Mongo谈话的服务(例如MongoUserService)的实现时,最简单的方法是编写一些将在本地机器上启动/停止 ...
  • 一般来说,单元测试不需要DI容器,因为单元测试是关于分离责任的。 考虑一个使用构造函数注入的类 public MyClass(IMyDependency dep) { } 在整个应用程序中,可能存在隐藏在IMyDependency后面的巨大的依赖关系图,但是在单元测试中,您可以将其全部缩小到单个“ 测试双” 。 您可以使用像Moq或RhinoMocks这样的动态模拟来生成测试双,但不是必需的。 var dep = new Mock().Object; var sut = ne ...
  • 是的,设置和拆除数据库中的所有集合对于确保单元测试之间没有副作用是必需的。 在实践中,这意味着您重新连接到数据库并删除所有集合的afterEach()以及与数据库断开连接的afterEach() 。 一些更深入的信息:你在这里试图做的是integration testing ,你正在测试你的代码和mongo之间的实际集成。 Unit tests是从不调用数据库或其他资源的测试。 有关此内容的更多信息: 单元测试和集成测试之间有什么区别? 对我而言,我将它们分为tests/unit和tests/integra ...
  • 如果要测试“排序”方法,则应对每个排序算法进行单独的单元测试。 例如 [TestMethod] public void MergeSort_SortUnderorderedList_ShouldSortListCorrectly() { // Arrange ISort sort = new MergeSort(); // Act int[] sortedList = sort.Sort(new int[] { 4, 2, 18, 5, 19 }); // Assert ...
  • 我在测试/查看数据库中的数据时发现有用的两个客户端是: MongoVue - 它是免费的,允许您一次最多查看3个文档; 并且有多种显示数据的方式(文本形式,表格形式等)相当漂亮,但很酷的功能在14天后过期,那么你必须付费。 MongoExplorer - 看起来不那么友好了MongoVue,但它使用非常简单,而且完全免费。 它还有一些更高级的选项 - 例如能够查看GridFS数据(这是MongoVue中的付费功能)。 Two clients I found useful when testing/looki ...
  • 在测试夹具setUp钩子中,您使用模拟对象初始化fileService : fileService = Mockito.mock(FileServiceImpl.class); 然后,您尝试使用模拟对象保存文件: fileService.storeFileGeneral(file); 由于fileService只是一个模拟,它在#storeFileGeneral调用上什么都不做,没有文件会像你期望的那样保存。 稍后,您尝试将持久化文件读回到使用正确ID创建的资产,而您获得的只是一个null引用: ass ...
  • 你可以使用rewire来模拟你需要的任何模块。 并重新定义你需要的方法。 You can use rewire to mock any module you require. And redefining the method you need.
  • 我使用JUnit 4和Spring Data的mongoTemplate。 看看我对类似问题的回答。 我希望它对你有所帮助。 I ended up using this Maven plugin. It allows populate/update your Mongo database before running your unit tests (it is a Maven plugin). It is probably not the best option but it did at least t ...
  • 问题在于加载夹具,如后面的问题所述 。 您可以通过使用适当的ObjectId替换灯具中的ID来修复它,如相应答案中所述 。 从你的追溯,问题是由django.contrib.auth提供的“authtestdata.json”装置。 由于这些与ObjectIds不兼容,您需要更新测试以使用不同的灯具,或者如果无法更新它们,则跳过失败的测试。 例如,Django的auth测试不会通过。 The issue is with loading fixtures as described in this later ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。